注意,使用时请替换申请的APPID SDKKEY,并设置好文件路径和图像尺寸
#include "stdafx.h"
#include <stdlib.h>
#include <stdint.h>
#include <Windows.h>
#include "arcsoft_fsdk_face_tracking.h"
#include "merror.h"
#pragma comment(lib,"libarcsoft_fsdk_face_tracking.lib")
#define WORKBUF_SIZE (40*1024*1024)
#define INPUT_IMAGE_PATH "sample.bmp"
bool readBmp(const char* path, uint8_t **imageData, int *pWidth, int *pHeight)
{
if (path == NULL || imageData == NULL || pWidth == NULL || pHeight == NULL)
{
fprintf(stderr, "ReadBmp para error\r\n");
return false;
}
FILE *fp = fopen(path, "rb");
if (fp == 0)
{
fprintf(stderr, "Bmp file open failed\r\n");
return false;
}
fseek(fp, sizeof(BITMAPFILEHEADER), 0); BITMAPINFOHEADER head;
fread(&head, sizeof(BITMAPINFOHEADER), 1, fp);
*pWidth = head.biWidth; *pHeight = head.biHeight; int biBitCount = head.biBitCount; int lineByte = ((*pWidth) * biBitCount / 8 + 3) / 4 * 4; *imageData = (uint8_t *)malloc(lineByte * (*pHeight)); for (int i = 0; i < *pHeight; i++)
{
fseek(fp, (*pHeight - 1 - i) * lineByte + 54, SEEK_SET); fread(*imageData + i * (*pWidth) * 3, 1, (*pWidth) * 3, fp);
}
fclose(fp);
return true;
} int _tmain(int argc, _TCHAR* argv[])
{
//初始化
MRESULT nRet = MERR_UNKNOWN; MHandle hEngine = nullptr; char APPID[256] = "APPID"; char SDKKey[256] = "SDKKey";
MInt32 nScale = 16;
MInt32 nMaxFace = 10;
LPAFT_FSDK_FACERES FaceRes = nullptr; MByte *pWorkMem = (MByte *)malloc(WORKBUF_SIZE);
if (pWorkMem == nullptr)
{
fprintf(stderr, "fail to malloc workbuf\r\n");
return -1;
}
nRet = AFT_FSDK_InitialFaceEngine(APPID, SDKKey, pWorkMem, WORKBUF_SIZE,
&hEngine, AFT_FSDK_OPF_0_HIGHER_EXT, nScale, nMaxFace);
if (nRet != MOK || hEngine == nullptr)
{
fprintf(stderr, "InitialFaceEngine failed , errorcode is %d \r\n", nRet);
return -1;
}
//获取版本信息
const AFT_FSDK_Version * pVersionInfo = nullptr; pVersionInfo = AFT_FSDK_GetVersion(hEngine);
fprintf(stdout, "%d %d %d %d\r\n", pVersionInfo->lCodebase, pVersionInfo-
>lMajor, pVersionInfo->lMinor, pVersionInfo->lBuild); fprintf(stdout, "%s\r\n", pVersionInfo->Version); fprintf(stdout, "%s\r\n", pVersionInfo->BuildDate); fprintf(stdout, "%s\r\n", pVersionInfo->CopyRight);
//读取bmp图片数据
ASVLOFFSCREEN offInput = { 0 };
offInput.u32PixelArrayFormat = ASVL_PAF_RGB24_B8G8R8; offInput.ppu8Plane[0] = nullptr;
readBmp(INPUT_IMAGE_PATH, (uint8_t**)&offInput.ppu8Plane[0],
&offInput.i32Width, &offInput.i32Height);
if (!offInput.ppu8Plane[0])
{
fprintf(stderr, "fail to ReadBmp(%s)\r\n", INPUT_IMAGE_PATH);
AFT_FSDK_UninitialFaceEngine(hEngine); free(pWorkMem);
return -1;
}
else
{
fprintf(stdout, "Picture width : %d , height : %d \r\n", offInput.i32Width, offInput.i32Height);
}
offInput.pi32Pitch[0] = offInput.i32Width * 3;
//人脸跟踪
int nRepeat = 0;
while (nRepeat++ <10)
{
nRet = AFT_FSDK_FaceFeatureDetect(hEngine, &offInput, &FaceRes); if (nRet == MOK)
{
fprintf(stdout, "The number of face: %d\r\n", FaceRes->nFace);
for (int i = 0; i < FaceRes->nFace; ++i)
{
fprintf(stdout, "Repeat [%d] ,Face[%d]:
rect[%d,%d,%d,%d]\r\n", nRepeat, i, FaceRes->rcFace[i].left, FaceRes-
>rcFace[i].top, FaceRes->rcFace[i].right, FaceRes->rcFace[i].bottom);
}
}
else
{
nRet); fprintf(stderr, "Face Detection failed, error code: %d\r\n",
} }
//反初始化
free(offInput.ppu8Plane[0]); nRet = AFT_FSDK_UninitialFaceEngine(hEngine);
if (nRet != MOK)
{
fprintf(stderr, "UninitialFaceEngine failed , errorcode is %d \r\n", nRet);
}
free(pWorkMem);
return 0;
}