用虹软sdk提取人脸jpg照片的人脸特征
这个功能写成一个函数
//从图片中抽取所有的人头像特征,最多FACENUM个
//int width 输入数据的宽度
//int height 输入数据的高度
//MUInt8* rgb888_data 输入数据
//ASF_FaceFeature *pout_feature 输出特征数列的地址
int arcsorft_face_process3(int width, int height, MUInt8* rgb888_data, ASF_FaceFeature *pout_feature) {
pthread_mutex_lock(&g_mutex);
MRESULT res = MOK;
//图像空间变换
ASVLOFFSCREEN offscreen = { 0 };
ColorSpaceConversion(width, height, ASVL_PAF_RGB24_B8G8R8, rgb888_data, offscreen);
ASF_MultiFaceInfo detectedFaces = { 0 };
ASF_SingleFaceInfo SingleDetectedFaces = { 0 };
//人脸检测
res = ASFDetectFacesEx(handle, &offscreen, &detectedFaces);
//错误
if (res != MOK) {
printf("ASFDetectFaces fail: %ld\n", res);
//getchar();
pthread_mutex_unlock(&g_mutex);
return -1;
}
//没有发现人脸,直接返回0
else if(detectedFaces.faceNum == 0) {
//printf("faceNum: %d, not 1\n", detectedFaces.faceNum);
//getchar();
pthread_mutex_unlock(&g_mutex);
return 0;
}
//发现人脸
else
{
int face_index = 0;
int max_width = 0;
int face_width = 0;
int i = 0;
for(i = 0; i < detectedFaces.faceNum; i++) {
//打印头像的位置
printf("face: %d, left: %d, top: %d, width: %d, height: %d \n",
i,
detectedFaces.faceRect[i].left,
detectedFaces.faceRect[i].top,
detectedFaces.faceRect[i].right - detectedFaces.faceRect[i].left,
detectedFaces.faceRect[i].bottom - detectedFaces.faceRect[i].top);
SingleDetectedFaces.faceRect.left = detectedFaces.faceRect[i].left;
SingleDetectedFaces.faceRect.top = detectedFaces.faceRect[i].top;
SingleDetectedFaces.faceRect.right = detectedFaces.faceRect[i].right;
SingleDetectedFaces.faceRect.bottom = detectedFaces.faceRect[i].bottom;
SingleDetectedFaces.faceOrient = detectedFaces.faceOrient[i];
// 单人脸特征提取
res = ASFFaceFeatureExtractEx(handle, &offscreen, &SingleDetectedFaces, &pout_feature[i]);
if(res != MOK) {
pthread_mutex_unlock(&g_mutex);
return -1;
}
}
}
pthread_mutex_unlock(&g_mutex);
return detectedFaces.faceNum;
}
主文件调用
把上次jpg相片解码的bgr数据传进去。
//最多识别图片中10个人
ASF_FaceFeature feature[10];
ret = arcsorft_face_process3(width, height, pbgr_data, feature);
printf("find %d person\n", ret);
编译测试
编译
face/samplecode/ASFTestDemo/build$ make
Scanning dependencies of target arcsoft_face_engine_test
[ 20%] Building CXX object CMakeFiles/arcsoft_face_engine_test.dir/arcsoft.cpp.o
[ 40%] Linking CXX executable arcsoft_face_engine_test
[100%] Built target arcsoft_face_engine_test
测试
face/samplecode/ASFTestDemo/build$ ./arcsoft_face_engine_test
************* ArcFace SDK Info *****************
startTime: 2024-09-09 00:00:00
endTime: 2025-09-09 00:00:00
Version:3.0.12402010101.3
BuildDate:07/29/2020
CopyRight:Copyright 2020 ArcSoft Corporation Limited. All rights reserved.
************* Face Recognition *****************
ASFInitEngine sucess: 0
pic ../jpg/100000000000000001.jpg width: 1200, height: 901
face: 0, left: 897, top: 253, width: 75, height: 75
face: 1, left: 747, top: 216, width: 76, height: 75
face: 2, left: 350, top: 236, width: 71, height: 71
face: 3, left: 611, top: 252, width: 70, height: 70
face: 4, left: 240, top: 226, width: 64, height: 65
face: 5, left: 493, top: 229, width: 65, height: 66
find 6 person