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; } /// /// 人脸检测 /// public static FaceDetectionModel FaceDetect(Image tempImage) { try { var client = getClient(); var image = ImageHelper.ImageToBase64(tempImage); var imageType = "BASE64"; var options = new Dictionary { {"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(); } catch { return null; } } /// /// 人脸注册+ /// /// 图片地址 /// 人员Uid /// 人员信息 /// 组别编号 /// public static FaceRegisterModel FaceRegister(Image tempImage, string userId, string userInfo, string groupId) { try { var client = getClient(); var imageType = "BASE64"; // 如果有可选参数 var options = new Dictionary { {"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(); } catch { return default(FaceRegisterModel); } } /// /// 人脸更新 /// /// /// /// /// /// 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 { {"user_info", userInfo}, {"quality_control", "NORMAL"}, {"liveness_control", "LOW"} }; // 带参数调用人脸更新 var result = client.UserUpdate(image, imageType, groupId, userId, options); return result.ToObject(); } catch { return default(FaceUpdateModel); } } /// /// 人脸删除 /// /// /// /// public static string FaceDelete(string uid, string groupId) { return ""; } /// /// 人脸搜索 /// /// /// 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{ {"quality_control", "NORMAL"}, {"liveness_control", "LOW"}, {"max_user_num", 1} }; // 带参数调用人脸搜索 var result = client.Search(image, imageType, groupIdList, options); return result.ToObject(); } catch { return default(FaceIdentifyModel); } } /// /// 返回指定的用户信息 /// /// public static FaceUserInfoModel UserInfo(string userId, string groupId) { try { var client = getClient(); var result = client.UserGet(userId, groupId); return result.ToObject(); } 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(); } catch { return default(UserDeleteModel); } } } }