虹軟離線人臉識別 ArcFace 2.0 Demo [C++]

環境: win10(10.0.16299.0)+ VS2017 sdk版本:ArcFace v2.0 OPENCV3.43版本 x64平臺Debug、Release配置都已經過編譯ios

下載地址:https://download.csdn.net/download/cngwj/10763108 在這裏插入圖片描述 配置過程c++

->0x01 下載sdk:ide

虹安sdk https://ai.arcsoft.com.cn測試

在這裏插入圖片描述 ->0x02 工程配置:ui

一、 添加工程的頭文件目錄:spa

a) 右鍵單擊工程名, 選擇屬性---配置屬性---c/c++---常規---附加包含目錄.net

b) 添加頭文件存放目錄 在這裏插入圖片描述 二、 添加文件引用的 lib 靜態庫路徑:3d

a) 右鍵單擊工程名,選擇屬性---配置屬性---連接器---常規---附加庫目錄code

b) 添加 lib 文件存放 在這裏插入圖片描述 三、 添加工程引用的 lib 庫: a) 右鍵單擊工程名,選擇屬性---配置屬性---連接器---輸入---附加依賴項 b) 添加依賴的 lib 庫名稱 在這裏插入圖片描述 四、自定義可執行文件輸出目錄 在這裏插入圖片描述 五、 添加工程引用的 dll 動態庫:blog

a) 把引用的 dll 放到工程的可執行文件所在的目錄下(複製到Build目錄)

在這裏插入圖片描述 六、添加本身申請的APPID 在這裏插入圖片描述

/************************************************************************
* Copyright(c) 2018
* All rights reserved.
* File: samplecode.cpp
* Brief: Powered by ArcSoft
	環境: win10(10.0.16299.0)+ VS2017
	sdk版本:ArcFace v2.0
	x64平臺Debug、Release配置都已經過編譯
* Version: 0.1
* Author: 一念無明
* Email: cngwj@outlook.com
* Date: 2018.11.3
* History:
2018.11.3 創建項目 
************************************************************************/
#include "pch.h"
#include "arcsoft_face_sdk.h"//接口文件
#include "amcomdef.h"//平臺文件
#include "asvloffscreen.h"//平臺文件
#include "merror.h"//錯誤碼文件
#include <direct.h> //目錄操做
#include <iostream>  
#include <stdarg.h>
#include <string>
#include <opencv.hpp>
 
using namespace std;
using namespace cv;
 
#pragma comment(lib, "libarcsoft_face_engine.lib")
#define APPID ""
#define SDKKey ""
#define MERR_ASF_BASE_ALREADY_ACTIVATED 90114 //SDK已激活 	
#define SafeFree(p) { if ((p)) free(p); (p) = NULL; }
#define SafeArrayDelete(p) { if ((p)) delete [] (p); (p) = NULL; } 
#define SafeDelete(p) { if ((p)) delete (p); (p) = NULL; } 
 
int main()
{
	//激活SDK
	MRESULT res = ASFActivation(APPID, SDKKey);
	if (MOK != res && MERR_ASF_BASE_ALREADY_ACTIVATED != res)
		printf("ALActivation fail: %d\n", res);
	else
		printf("ALActivation sucess: %d\n", res);
 
	//初始化引擎
	MHandle handle = NULL;
	MInt32 mask = ASF_FACE_DETECT | ASF_FACERECOGNITION | ASF_AGE | ASF_GENDER | ASF_FACE3DANGLE;
	res = ASFInitEngine(ASF_DETECT_MODE_IMAGE, ASF_OP_0_ONLY, 16, 5, mask, &handle);
	if (res != MOK)
		printf("ALInitEngine fail: %d\n", res);
	else
		printf("ALInitEngine sucess: %d\n", res);
 
	// 人臉檢測
	IplImage* img = cvLoadImage("../Build\\1.bmp");//圖片寬度需符合4的倍數
	IplImage* img1 = cvLoadImage("../Build\\2.bmp");
 
	if (img && img1)
	{
		ASF_MultiFaceInfo detectedFaces1 = { 0 };//多人臉信息;
		ASF_SingleFaceInfo SingleDetectedFaces1 = { 0 };
		ASF_FaceFeature feature1 = { 0 };
		ASF_FaceFeature copyfeature1 = { 0 };
		res = ASFDetectFaces(handle, img->width, img->height, ASVL_PAF_RGB24_B8G8R8, (MUInt8*)img->imageData, &detectedFaces1);
		if (MOK == res)
		{
			SingleDetectedFaces1.faceRect.left = detectedFaces1.faceRect[0].left;
			SingleDetectedFaces1.faceRect.top = detectedFaces1.faceRect[0].top;
			SingleDetectedFaces1.faceRect.right = detectedFaces1.faceRect[0].right;
			SingleDetectedFaces1.faceRect.bottom = detectedFaces1.faceRect[0].bottom;
			SingleDetectedFaces1.faceOrient = detectedFaces1.faceOrient[0];
			//單人臉特徵提取
			res = ASFFaceFeatureExtract(handle, img->width, img->height, ASVL_PAF_RGB24_B8G8R8, (MUInt8*)img->imageData, &SingleDetectedFaces1, &feature1);
			if (res == MOK)
			{
				//拷貝feature
				copyfeature1.featureSize = feature1.featureSize;
				copyfeature1.feature = (MByte *)malloc(feature1.featureSize);
				memset(copyfeature1.feature, 0, feature1.featureSize);
				memcpy(copyfeature1.feature, feature1.feature, feature1.featureSize);
			}
			else
				printf("ASFFaceFeatureExtract 1 fail: %d\n", res);
		}
		else
			printf("ASFDetectFaces 1 fail: %d\n", res);
 
		//第二張人臉提取特徵
		ASF_MultiFaceInfo	detectedFaces2 = { 0 };
		ASF_SingleFaceInfo SingleDetectedFaces2 = { 0 };
		ASF_FaceFeature feature2 = { 0 };
		res = ASFDetectFaces(handle, img1->width, img1->height, ASVL_PAF_RGB24_B8G8R8, (MUInt8*)img1->imageData, &detectedFaces2);
		if (MOK == res)
		{
			SingleDetectedFaces2.faceRect.left = detectedFaces2.faceRect[0].left;
			SingleDetectedFaces2.faceRect.top = detectedFaces2.faceRect[0].top;
			SingleDetectedFaces2.faceRect.right = detectedFaces2.faceRect[0].right;
			SingleDetectedFaces2.faceRect.bottom = detectedFaces2.faceRect[0].bottom;
			SingleDetectedFaces2.faceOrient = detectedFaces2.faceOrient[0];
 
			res = ASFFaceFeatureExtract(handle, img1->width, img1->height, ASVL_PAF_RGB24_B8G8R8, (MUInt8*)img1->imageData, &SingleDetectedFaces2, &feature2);
			if (MOK != res)
				printf("ASFFaceFeatureExtract 2 fail: %d\n", res);
		}
		else
			printf("ASFDetectFaces 2 fail: %d\n", res);
 
		// 單人臉特徵比對
		MFloat confidenceLevel;
		res = ASFFaceFeatureCompare(handle, &copyfeature1, &feature2, &confidenceLevel);
		if (res != MOK)
			printf("ASFFaceFeatureCompare fail: %d\n", res);
		else
			printf("ASFFaceFeatureCompare sucess: %lf\n", confidenceLevel);
 
		// 人臉信息檢測
		MInt32 processMask = ASF_AGE | ASF_GENDER | ASF_FACE3DANGLE;
		res = ASFProcess(handle, img1->width, img1->height, ASVL_PAF_RGB24_B8G8R8, (MUInt8*)img1->imageData, &detectedFaces1, processMask);
		if (res != MOK)
			printf("ASFProcess fail: %d\n", res);
		else
			printf("ASFProcess sucess: %d\n", res);
 
		// 獲取年齡
		ASF_AgeInfo ageInfo = { 0 };
		res = ASFGetAge(handle, &ageInfo);
		//printf("年齡: %d\n", ageInfo);
		if (res != MOK)
			printf("ASFGetAge fail: %d\n", res);
		else
			printf("ASFGetAge sucess: %d\n", res);
 
		// 獲取性別
		ASF_GenderInfo genderInfo = { 0 };
		res = ASFGetGender(handle, &genderInfo);
		if (res != MOK)
			printf("ASFGetGender fail: %d\n", res);
		else
			printf("ASFGetGender sucess: %d\n", res);
 
		// 獲取3D角度
		ASF_Face3DAngle angleInfo = { 0 };
		res = ASFGetFace3DAngle(handle, &angleInfo);
		if (res != MOK)
			printf("ASFGetFace3DAngle fail: %d\n", res);
		else
			printf("ASFGetFace3DAngle sucess: %d\n", res);
 
		SafeFree(copyfeature1.feature);		//釋放內存
	}
 
	//獲取版本信息
	const ASF_VERSION* pVersionInfo = ASFGetVersion(handle);
	printf("版本號: %s\n", pVersionInfo->Version);
 
	//反初始化
	res = ASFUninitEngine(handle);
	if (res != MOK)
		printf("ALUninitEngine fail: %d\n", res);
	else
		printf("ALUninitEngine sucess: %d\n", res);
 
	getchar();
	return 0;
}

用其它照片測試須要注意圖片的寬度 在這裏插入圖片描述

相關文章
相關標籤/搜索