183 lines
6.0 KiB
C#
183 lines
6.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Drawing;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using HZQR.Common;
|
||
using Personnel.BaiduAI.Model.FaceModel;
|
||
|
||
namespace Personnel.BaiduAI
|
||
{
|
||
public class BaiduFaceAPI
|
||
{
|
||
private static Baidu.Aip.Face.Face getClient()
|
||
{
|
||
var client = new Baidu.Aip.Face.Face(BaiduAIConfig.clientId, BaiduAIConfig.clientSecret);
|
||
client.Timeout = 60000; // 修改超时时间
|
||
return client;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 人脸检测
|
||
/// </summary>
|
||
public static FaceDetectionModel FaceDetect(Image tempImage)
|
||
{
|
||
try
|
||
{
|
||
var client = getClient();
|
||
var image = ImageHelper.ImageToBase64(tempImage);
|
||
var imageType = "BASE64";
|
||
var options = new Dictionary<string, object>
|
||
{
|
||
{"face_field", "age,beauty,expression,gender,glasses,race,quality"},
|
||
{"max_face_num", 1},
|
||
{"face_type", "LIVE"}
|
||
};
|
||
var result = client.Detect(image, imageType, options);
|
||
return result.ToObject<FaceDetectionModel>();
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 人脸注册+
|
||
/// </summary>
|
||
/// <param name="imagePath">图片地址</param>
|
||
/// <param name="uid">人员Uid</param>
|
||
/// <param name="userInfo">人员信息</param>
|
||
/// <param name="groupId">组别编号</param>
|
||
/// <returns></returns>
|
||
public static FaceRegisterModel FaceRegister(Image tempImage, string userId, string userInfo, string groupId)
|
||
{
|
||
try
|
||
{
|
||
var client = getClient();
|
||
var imageType = "BASE64";
|
||
// 如果有可选参数
|
||
var options = new Dictionary<string, object>
|
||
{
|
||
{"user_info", userInfo},
|
||
{"quality_control", "NORMAL"},
|
||
{"liveness_control", "LOW"}
|
||
};
|
||
var image1 = ImageHelper.ImageToBase64(tempImage);
|
||
// 带参数调用人脸注册
|
||
var result = client.UserAdd(image1, imageType, groupId, userId, options);
|
||
return result.ToObject<FaceRegisterModel>();
|
||
}
|
||
catch
|
||
{
|
||
return default(FaceRegisterModel);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 人脸更新
|
||
/// </summary>
|
||
/// <param name="imagePath"></param>
|
||
/// <param name="userId"></param>
|
||
/// <param name="userInfo"></param>
|
||
/// <param name="groupId"></param>
|
||
/// <returns></returns>
|
||
public static FaceUpdateModel FaceUpdate(Image tempImage, string userId, string userInfo, string groupId)
|
||
{
|
||
try
|
||
{
|
||
var client = getClient();
|
||
var imageType = "BASE64";
|
||
var image = ImageHelper.ImageToBase64(tempImage);
|
||
// 如果有可选参数
|
||
var options = new Dictionary<string, object>
|
||
{
|
||
{"user_info", userInfo},
|
||
{"quality_control", "NORMAL"},
|
||
{"liveness_control", "LOW"}
|
||
};
|
||
// 带参数调用人脸更新
|
||
var result = client.UserUpdate(image, imageType, groupId, userId, options);
|
||
return result.ToObject<FaceUpdateModel>();
|
||
}
|
||
catch
|
||
{
|
||
return default(FaceUpdateModel);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 人脸删除
|
||
/// </summary>
|
||
/// <param name="uid"></param>
|
||
/// <param name="groupId"></param>
|
||
/// <returns></returns>
|
||
public static string FaceDelete(string uid, string groupId)
|
||
{
|
||
|
||
return "";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 人脸搜索
|
||
/// </summary>
|
||
/// <param name="tempImage"></param>
|
||
/// <param name="groupId"></param>
|
||
public static FaceIdentifyModel FaceIdentify(Image tempImage, string groupId, int userTopNum = 1, int faceTopNum = 1)
|
||
{
|
||
try
|
||
{
|
||
var client = getClient();
|
||
var image = ImageHelper.ImageToBase64(tempImage);
|
||
var imageType = "BASE64";
|
||
var groupIdList = BaiduAIConfig.GROUP_ID;
|
||
|
||
// 如果有可选参数
|
||
var options = new Dictionary<string, object>{
|
||
{"quality_control", "NORMAL"},
|
||
{"liveness_control", "LOW"},
|
||
{"max_user_num", 1}
|
||
};
|
||
// 带参数调用人脸搜索
|
||
var result = client.Search(image, imageType, groupIdList, options);
|
||
return result.ToObject<FaceIdentifyModel>();
|
||
}
|
||
catch
|
||
{
|
||
return default(FaceIdentifyModel);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回指定的用户信息
|
||
/// </summary>
|
||
/// <param name="uid"></param>
|
||
public static FaceUserInfoModel UserInfo(string userId, string groupId)
|
||
{
|
||
try
|
||
{
|
||
var client = getClient();
|
||
var result = client.UserGet(userId, groupId);
|
||
return result.ToObject<FaceUserInfoModel>();
|
||
}
|
||
catch
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
public static UserDeleteModel UserDelete(string userId, string groupId)
|
||
{
|
||
try
|
||
{
|
||
var client = getClient();
|
||
// 调用删除用户,可能会抛出网络等异常,请使用try/catch捕获
|
||
var result = client.UserDelete(groupId, userId);
|
||
return result.ToObject<UserDeleteModel>();
|
||
}
|
||
catch
|
||
{
|
||
return default(UserDeleteModel);
|
||
}
|
||
}
|
||
}
|
||
} |