百度人脸识别Windows C++离线sdk C#接入
目录
说明
自己根据SDK封装了动态库,然后C#调用。
功能接口
设计背景
• 场景特点:
-- 网络:对于无网、局域网等情况,无法连接公网,API 方式无法运作。如政府单
位、金融保险、教育机构等,其中内网情况最为常见,私有化部署是项目开展的前提
条件。
-- 安全:即使可以连接外网,因为人脸数据的敏感性,许多客户不希望将人脸数据传
入百度服务器,如大学学生照片、部分企业员工数据等,API 形式也往往不被接受。
-- 速度:由于各地网络线路、机房部署、图片采集方式等诸多原因,API 形式往往耗
时较高,容易存在部分请求耗时过长的情况,容易影响业务正常运转。
-- 稳定:API 形式容易受网络抖动、机房故障、线上连带 bug 等影响,存在一定的不
稳定因素,可用性保障,往往成为在线调用最容易出现问题的地方。
• 客户特点:
-- 1:N-小型人脸库检索:多为通道通行、固定区域人群验证等需求,如写字楼闸机
门禁、企业考勤打卡等,人脸库范围较小,且不易经常变动。
-- 1:1-自有数据源对比:将当前采集的人脸,与其他数据源中的人脸进行对比,如
身份证芯片照、教务系统图片、档案图片等,进行快速的 1:1 对比验证。
• 核心需求:
-- 基础的人脸采集:包含人脸检测、跟踪、捕获、质量校验等基础功能,获取符合识
别条件的人脸。为之前的客户端 SDK 的标准功能,离线版本 SDK 保留以上所有能
力。
-- 本地特征抽取:所有在 SDK 中运行的人脸图片,都可以完成本地特征抽取,以便
进行对比或识别操作。
-- 1:1 对比:支持两张图片的相似度对比,可直接传入图片,也可调用本地某个人
脸特征;
-- 1:N 搜索:支持一定库大小的人脸查找,在指定的人脸集合中查找最相似的人
脸,并返回相似度分值;
SDK 包结构
效果
代码
头文件
#pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
// Windows 头文件:
#include <windows.h>
#include "baidu_face_api.h"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
//创建实例
extern "C" _declspec(dllexport) void* __cdecl create();
//实例初始化
extern "C" _declspec(dllexport) int __cdecl init(void* engine, char* model_path, char* code);
//人脸检测
extern "C" _declspec(dllexport) int __cdecl face_detect(void* engine, Mat* image, char* json_result);
// 通过图片人脸特征值提取
extern "C" _declspec(dllexport) int __cdecl face_feature(void* engine, Mat* image, char* json_result);
// 人脸比对(通过传图片)
extern "C" _declspec(dllexport) float __cdecl face_match_by_img(void* engine, Mat* image, Mat* image2);
源文件
#include "stdafx.h"
BaiduFaceApi *api;
//创建实例
void* __cdecl create() {
api = new BaiduFaceApi();
// 获取设备指纹
std::string device_id;
api->get_device_id(device_id);
std::cout << "device id is:" << device_id << std::endl;
// 获取sdk版本号
std::string version;
api->sdk_version(version);
std::cout << "sdk version:" << version << std::endl;
return api;
}
//实例初始化
int __cdecl init(void* engine, char* model_path, char* code) {
BaiduFaceApi* _api = (BaiduFaceApi*)engine;
return (int)_api->sdk_init(model_path);
}
//人脸检测
int __cdecl face_detect(void* engine, Mat* image, char* json_result) {
BaiduFaceApi* _api = (BaiduFaceApi*)engine;
std::vector<FaceBox> box_list;
// type 0: 表示rgb 人脸检测 1:表示nir人脸检测
int type = 0;
int face_num = 0;
face_num = _api->detect(box_list, image, 0);
std::cout << "-----face_num is-------" << face_num << std::endl;
std::string res_str("");
if (face_num > 0)
{
for (int i = 0; i < face_num; i++)
{
FaceBox info = box_list.at(i);
// 人脸置信度
std::cout << "detect score is:" << info.score << std::endl;
// 人脸宽度
std::cout << "detect mWidth is:" << info.width << std::endl;
// 中心点X,Y坐标
std::cout << "detect mCenter_x is:" << info.center_x << std::endl;
std::cout << "detect mCenter_y is:" << info.center_y << std::endl;
std::string str = ("{\"center_x\":\"") + std::to_string(info.center_x) + ("\",") +
"\"center_y\":\"" + std::to_string(info.center_y) + ("\",") +
"\"width\":\"" + std::to_string(info.width) + ("\",") +
"\"height\":\"" + std::to_string(info.height) + ("\",") +
"\"score\":\"" + std::to_string(info.score) + "\"}";
res_str = res_str + str.c_str();
res_str += ",";
}
}
if (res_str.length()>0)
{
res_str.erase(res_str.length() - 1);
}
res_str = "[" + res_str + "]";
strcpy(json_result, res_str.c_str());
return face_num;
}
// 通过图片人脸特征值提取
int __cdecl face_feature(void* engine, Mat* image, char* json_result) {
BaiduFaceApi* _api = (BaiduFaceApi*)engine;
std::vector<Feature> out_fea1;
std::vector<FaceBox> out_box1;
// 提取第一个人的特征值
// 特征值的type:传 0 可见光生活照特征值, 1、表示近红外特征值
int type = 0;
int face_num = 0;
face_num = _api->face_feature(out_fea1, out_box1, image, type);
std::cout << "face num is:" << face_num << std::endl;
int size = out_fea1.size();
std::string res_str("");
for (int i = 0; i < size; i++) {
Feature feature = out_fea1.at(i);
FaceBox info = out_box1.at(i);
std::string str_feature = "";
for (size_t i = 0; i < feature.size; i++)
{
str_feature = str_feature + std::to_string(feature.data[i])+" ";
}
if (str_feature.length()>0)
{
str_feature.erase(str_feature.length() - 1);
}
std::string str = ("{\"center_x\":\"") + std::to_string(info.center_x) + ("\",") +
"\"center_y\":\"" + std::to_string(info.center_y) + ("\",") +
"\"width\":\"" + std::to_string(info.width) + ("\",") +
"\"height\":\"" + std::to_string(info.height) + ("\",") +
"\"score\":\"" + std::to_string(info.score) + "\","+
"\"feature\":\"" + str_feature + "\"" +
"}";
res_str = res_str + str.c_str();
res_str += ",";
}
if (res_str.length()>0)
{
res_str.erase(res_str.length() - 1);
}
res_str = "[" + res_str + "]";
strcpy(json_result, res_str.c_str());
return face_num;
}
// 人脸比对(通过传图片)
float __cdecl face_match_by_img(void* engine, Mat* image, Mat* image2) {
BaiduFaceApi* _api = (BaiduFaceApi*)engine;
// type 0: 表示rgb 人脸特征值 1:表示nir人脸特征值
int type = 0;
float score = _api->match(*image, *image2, type);
std::cout << "face match score is:" << score << std::endl;
return score;
}
调用代码
using FaceService.Common;
using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FaceService
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static IntPtr engine;
string image_path = "";
string image_path2 = "";
private void button1_Click(object sender, EventArgs e)
{
engine = Native.create();
string model_path = Application.StartupPath + "\\face";
textBox1.Text = "正在初始化,请稍后……";
Application.DoEvents();
int res = Native.init(engine, model_path, "");
if (res == 0)
{
textBox1.Text = "初始化成功!";
MessageBox.Show("初始化成功");
}
else
{
textBox1.Text = "初始化失败,code:" + res;
MessageBox.Show("初始化失败,code:" + res);
}
}
/// <summary>
/// 人脸检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (engine == IntPtr.Zero)
{
MessageBox.Show("请先初始化!");
return;
}
if (image_path == "")
{
MessageBox.Show("请先选择图片!");
return;
}
textBox1.Text = "";
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = null;
button2.Enabled = false;
Application.DoEvents();
Mat img = Cv2.ImRead(image_path);
StringBuilder json_result = new StringBuilder(1024);
int faceNum = Native.face_detect(engine, img.CvPtr, json_result);
if (faceNum > 0)
{
textBox1.Text = json_result.ToString();
// 将JSON字符串反序列化为对象
FaceBox[] FaceBoxs = JsonConvert.DeserializeObject<FaceBox[]>(json_result.ToString());
for (int i = 0; i < FaceBoxs.Length; i++)
{
FaceBox faceBox = FaceBoxs[i];
int x = (int)(faceBox.center_x - faceBox.width / 2);
int y = (int)(faceBox.center_y - faceBox.height / 2);
Rect rect = new Rect(x, y, (int)faceBox.width, (int)faceBox.height);
Cv2.PutText(img, faceBox.score.ToString("F2"), new OpenCvSharp.Point(x, y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red);
Cv2.Rectangle(img, rect, Scalar.Red);
}
pictureBox2.Image = new Bitmap(img.ToMemoryStream());
}
else
{
MessageBox.Show("未检测到人脸!");
}
button2.Enabled = true;
}
string fileFilter = "选择图片|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
/// <summary>
/// 选择图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Application.StartupPath + "\\test_img\\";
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
pictureBox2.Image = null;
}
private void Form1_Load(object sender, EventArgs e)
{
image_path = "test_img\\2.jpg";
pictureBox1.Image = new Bitmap(image_path);
}
/// <summary>
/// 获取人脸特征值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (engine == IntPtr.Zero)
{
MessageBox.Show("请先初始化!");
return;
}
if (image_path == "")
{
MessageBox.Show("请先选择图片!");
return;
}
textBox1.Text = "";
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = null;
button4.Enabled = false;
Application.DoEvents();
Mat img = Cv2.ImRead(image_path);
StringBuilder json_result = new StringBuilder(2048);
int faceNum = Native.face_feature(engine, img.CvPtr, json_result);
if (faceNum > 0)
{
textBox1.Text = json_result.ToString();
}
else
{
MessageBox.Show("未检测到人脸!");
}
button4.Enabled = true;
}
/// <summary>
/// 人脸比对
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
if (engine == IntPtr.Zero)
{
MessageBox.Show("请先初始化!");
return;
}
if (image_path == "")
{
MessageBox.Show("请先选择图片!");
return;
}
if (image_path2 == "")
{
MessageBox.Show("请先选择图片!");
return;
}
textBox1.Text = "";
button5.Enabled = false;
Application.DoEvents();
Mat img = Cv2.ImRead(image_path);
Mat img2 = Cv2.ImRead(image_path2);
float match_score = Native.face_match_by_img(engine, img.CvPtr, img2.CvPtr);
textBox1.Text = "match score:" + match_score.ToString("F2");
button5.Enabled = true;
}
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Application.StartupPath + "\\test_img\\";
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
image_path2 = ofd.FileName;
pictureBox2.Image = null;
pictureBox2.Image = new Bitmap(image_path2);
}
}
}
/*
各接口返回结果 error_code 及 msg 信息如下:
错误码 错误内容 错误描述
0 SUCCESS 成功
-1 ILLEGAL_PARAMS 失败或非法参数
-2 MEMORY_ALLOCATION_FAILED 内存分配失败
-3 INSTANCE_IS_EMPTY 实例对象为空
-4 MODEL_IS_EMPTY 模型内容为空
-5 UNSUPPORT_ABILITY_TYPE 不支持的能力类型
-6 UNSUPPORT_INFER_TYPE 不支持的预测库类型
-7 NN_CREATE_FAILED 预测库对象创建失败
-8 NN_INIT_FAILED 预测库对象初始化失败
-9 IMAGE_IS_EMPTY 图像数据为空
-10 ABILITY_INIT_FAILED 人脸能力初始化失败
-11 ABILITY_UNLOAD 人脸能力未加载
-12 ABILITY_ALREADY_LOADED 人脸能力已加载
-13 NOT_AUTHORIZED 未授权
-14 ABILITY_RUN_EXCEPTION 人脸能力运行异常
-15 UNSUPPORT_IMAGE_TYPE 不支持的图像类型
-16 IMAGE_TRANSFORM_FAILED 图像转换失败
-1001 SYSTEM_ERROR 系统错误
-1002 PARARM_ERROR 参数错误
-1003 DB_OP_FAILED 数据库操作失败
-1004 NO_DATA 没有数据
-1005 RECORD_UNEXIST 记录不存在
-1006 RECORD_ALREADY_EXIST 记录已经存在
-1007 FILE_NOT_EXIST 文件不存在
-1008 GET_FEATURE_FAIL 提取特征值失败
-1009 FILE_TOO_BIG 文件太大
-1010 FACE_RESOURCE_NOT_EXIST 人脸资源文件不存在
-1011 FEATURE_LEN_ERROR 特征值长度错误
-1012 DETECT_NO_FACE 未检测到人脸
-1013 CAMERA_ERROR 摄像头错误或不存在
-1014 FACE_INSTANCE_ERROR 人脸引擎初始化错误
-1015 LICENSE_FILE_NOT_EXIST 授权文件不存在
-1016 LICENSE_KEY_EMPTY 授权序列号为空
-1017 LICENSE_KEY_INVALID 授权序列号无效
-1018 LICENSE_KEY_EXPIRE 授权序序列号过期
-1019 LICENSE_ALREADY_USED 授权序列号已被使用
-1020 DEVICE_ID_EMPTY 设备指纹为空
-1021 NETWORK_TIMEOUT 网络超时
-1022 NETWORK_ERROR 网络错误
-1023 CONF_INI_UNEXIST 配置 ini 文件不存在
-1024 WINDOWS_SERVER_ERROR 禁用在 Windows Server
*/
using FaceService.Common;
using Newtonsoft.Json;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FaceService
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static IntPtr engine;
string image_path = "";
string image_path2 = "";
private void button1_Click(object sender, EventArgs e)
{
engine = Native.create();
string model_path = Application.StartupPath + "\\face";
textBox1.Text = "正在初始化,请稍后……";
Application.DoEvents();
int res = Native.init(engine, model_path, "");
if (res == 0)
{
textBox1.Text = "初始化成功!";
MessageBox.Show("初始化成功");
}
else
{
textBox1.Text = "初始化失败,code:" + res;
MessageBox.Show("初始化失败,code:" + res);
}
}
/// <summary>
/// 人脸检测
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if (engine == IntPtr.Zero)
{
MessageBox.Show("请先初始化!");
return;
}
if (image_path == "")
{
MessageBox.Show("请先选择图片!");
return;
}
textBox1.Text = "";
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = null;
button2.Enabled = false;
Application.DoEvents();
Mat img = Cv2.ImRead(image_path);
StringBuilder json_result = new StringBuilder(1024);
int faceNum = Native.face_detect(engine, img.CvPtr, json_result);
if (faceNum > 0)
{
textBox1.Text = json_result.ToString();
// 将JSON字符串反序列化为对象
FaceBox[] FaceBoxs = JsonConvert.DeserializeObject<FaceBox[]>(json_result.ToString());
for (int i = 0; i < FaceBoxs.Length; i++)
{
FaceBox faceBox = FaceBoxs[i];
int x = (int)(faceBox.center_x - faceBox.width / 2);
int y = (int)(faceBox.center_y - faceBox.height / 2);
Rect rect = new Rect(x, y, (int)faceBox.width, (int)faceBox.height);
Cv2.PutText(img, faceBox.score.ToString("F2"), new OpenCvSharp.Point(x, y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red);
Cv2.Rectangle(img, rect, Scalar.Red);
}
pictureBox2.Image = new Bitmap(img.ToMemoryStream());
}
else
{
MessageBox.Show("未检测到人脸!");
}
button2.Enabled = true;
}
string fileFilter = "选择图片|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
/// <summary>
/// 选择图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Application.StartupPath + "\\test_img\\";
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
pictureBox2.Image = null;
}
private void Form1_Load(object sender, EventArgs e)
{
image_path = "test_img\\2.jpg";
pictureBox1.Image = new Bitmap(image_path);
}
/// <summary>
/// 获取人脸特征值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (engine == IntPtr.Zero)
{
MessageBox.Show("请先初始化!");
return;
}
if (image_path == "")
{
MessageBox.Show("请先选择图片!");
return;
}
textBox1.Text = "";
if (pictureBox2.Image != null)
{
pictureBox2.Image.Dispose();
}
pictureBox2.Image = null;
button4.Enabled = false;
Application.DoEvents();
Mat img = Cv2.ImRead(image_path);
StringBuilder json_result = new StringBuilder(2048);
int faceNum = Native.face_feature(engine, img.CvPtr, json_result);
if (faceNum > 0)
{
textBox1.Text = json_result.ToString();
}
else
{
MessageBox.Show("未检测到人脸!");
}
button4.Enabled = true;
}
/// <summary>
/// 人脸比对
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
if (engine == IntPtr.Zero)
{
MessageBox.Show("请先初始化!");
return;
}
if (image_path == "")
{
MessageBox.Show("请先选择图片!");
return;
}
if (image_path2 == "")
{
MessageBox.Show("请先选择图片!");
return;
}
textBox1.Text = "";
button5.Enabled = false;
Application.DoEvents();
Mat img = Cv2.ImRead(image_path);
Mat img2 = Cv2.ImRead(image_path2);
float match_score = Native.face_match_by_img(engine, img.CvPtr, img2.CvPtr);
textBox1.Text = "match score:" + match_score.ToString("F2");
button5.Enabled = true;
}
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = Application.StartupPath + "\\test_img\\";
ofd.Filter = fileFilter;
if (ofd.ShowDialog() != DialogResult.OK) return;
image_path2 = ofd.FileName;
pictureBox2.Image = null;
pictureBox2.Image = new Bitmap(image_path2);
}
}
}
/*
各接口返回结果 error_code 及 msg 信息如下:
错误码 错误内容 错误描述
0 SUCCESS 成功
-1 ILLEGAL_PARAMS 失败或非法参数
-2 MEMORY_ALLOCATION_FAILED 内存分配失败
-3 INSTANCE_IS_EMPTY 实例对象为空
-4 MODEL_IS_EMPTY 模型内容为空
-5 UNSUPPORT_ABILITY_TYPE 不支持的能力类型
-6 UNSUPPORT_INFER_TYPE 不支持的预测库类型
-7 NN_CREATE_FAILED 预测库对象创建失败
-8 NN_INIT_FAILED 预测库对象初始化失败
-9 IMAGE_IS_EMPTY 图像数据为空
-10 ABILITY_INIT_FAILED 人脸能力初始化失败
-11 ABILITY_UNLOAD 人脸能力未加载
-12 ABILITY_ALREADY_LOADED 人脸能力已加载
-13 NOT_AUTHORIZED 未授权
-14 ABILITY_RUN_EXCEPTION 人脸能力运行异常
-15 UNSUPPORT_IMAGE_TYPE 不支持的图像类型
-16 IMAGE_TRANSFORM_FAILED 图像转换失败
-1001 SYSTEM_ERROR 系统错误
-1002 PARARM_ERROR 参数错误
-1003 DB_OP_FAILED 数据库操作失败
-1004 NO_DATA 没有数据
-1005 RECORD_UNEXIST 记录不存在
-1006 RECORD_ALREADY_EXIST 记录已经存在
-1007 FILE_NOT_EXIST 文件不存在
-1008 GET_FEATURE_FAIL 提取特征值失败
-1009 FILE_TOO_BIG 文件太大
-1010 FACE_RESOURCE_NOT_EXIST 人脸资源文件不存在
-1011 FEATURE_LEN_ERROR 特征值长度错误
-1012 DETECT_NO_FACE 未检测到人脸
-1013 CAMERA_ERROR 摄像头错误或不存在
-1014 FACE_INSTANCE_ERROR 人脸引擎初始化错误
-1015 LICENSE_FILE_NOT_EXIST 授权文件不存在
-1016 LICENSE_KEY_EMPTY 授权序列号为空
-1017 LICENSE_KEY_INVALID 授权序列号无效
-1018 LICENSE_KEY_EXPIRE 授权序序列号过期
-1019 LICENSE_ALREADY_USED 授权序列号已被使用
-1020 DEVICE_ID_EMPTY 设备指纹为空
-1021 NETWORK_TIMEOUT 网络超时
-1022 NETWORK_ERROR 网络错误
-1023 CONF_INI_UNEXIST 配置 ini 文件不存在
-1024 WINDOWS_SERVER_ERROR 禁用在 Windows Server
*/
标签:engine,人脸识别,img,Windows,image,离线,int,人脸,path
From: https://blog.csdn.net/weixin_46771779/article/details/140520043