/// <summary> /// 人脸更新 /// </summary> /// <returns></returns> public async Task<ActionResult> UpdateFace(FaceDatabaseModel.UserFaceModel model) { // 一系列检查 RequestFaceModel request = new RequestFaceModel() { Status = 500, Message = null, FaceList = null }; var userFaceSet = Verify(ref request, model); // 基本验证 if (userFaceSet == null) { return Ok(request); } // 判断是否存在此用户,是否存在此分组 // 判断是否有同样的人,判断是否有此分组 var group = await Service_user_face_storage_group.Get_storage_group_BySecretGroupAsync(model.secret_id, model.group_id); if (group == null) { request.Enum = RequestEnum.数据不存在; return Ok(request); } user_face_storage group_user = null; if (model.group_id == "@ALL") { group_user = await Service_user_face_storage.GetUser_By_UGS_ALL(model.user_id, model.secret_id); } else { group_user = await Service_user_face_storage.GetUser_By_UGS(model.user_id, group.id, model.secret_id); } if (group_user == null) { request.Enum = RequestEnum.数据不存在; return Ok(request); } // 将以前的人脸状态变为删除 user_face_storage user = new user_face_storage() { is_delete = true, updated_at = DateTime.Now, secret_id = model.secret_id }; // 增加一个人脸 #region 获取图片 var face_token = string.Empty; (Bitmap, string) bitmap = (null, null); // 获取图片文件 bitmap = model.image_type.ToUpper() == "BASE64" ? FileCommon.Base64ToBitmap(model.image) : FileCommon.UrlToBitmap(model.image); if (bitmap.Item1 == null && !string.IsNullOrEmpty(bitmap.Item2)) { if (model.image_type.ToUpper() == "BASE64") { request.Enum = RequestEnum.Base64图片格式错误; } else { request.Enum = RequestEnum.从图片的url下载图片失败; } return Ok(request); } #endregion #region 图片以及数据处理 // 查找是否有相同的图片 face_token = model.image_type.ToUpper() == "BASE64" ? EncryptProvider.Md5(model.image) : EncryptProvider.Md5(FileCommon.BitmapToBase64(bitmap.Item1)); // 查询 图片库 var user_images = await Service_user_face_image_data.Getuser_face_image_data_By_Token_Async(face_token); if (user_images != null) { bitmap.Item2 = user_images.url; } else { try { // 存入本地 bitmap.Item2 = FileCommon.SaveBitmap(bitmap.Item1, bitmap.Item2); } catch (Exception ex) { LogHelperNLog.Error(ex); request.Enum = RequestEnum.数据存储处理失败; return Ok(request); } } // 获取人脸数据 //var faceDate = FaceDetectionCompute.GetRectangle(bitmap.Item1); DlibDotNet.Rectangle[] faceDate = new DlibDotNet.Rectangle[0]; try { // 截取人像,面积排序 faceDate = FaceDetectionCompute.GetRectangle(bitmap.Item1).OrderByDescending(d => d.Area).Take(1).ToArray(); } catch (Exception ex) { LogHelperNLog.Error(ex, "FaceDetectionCompute.GetRectangles:人脸获取坐标出错"); request.Enum = RequestEnum.无法解析人脸; return Ok(request); } #endregion request.Enum = RequestEnum.Succeed; request.Location = faceDate; #region 日志&&扣费 try { DateTime dateTime = DateTime.Now; // 图片保存到数据库 user_images = new user_face_image_data() { token = face_token, url = bitmap.Item2, created_at = dateTime, updated_at = dateTime }; //查找分组信息 user.api_group_id = group.id; //用户信息 user_face_storage face_Storage = new user_face_storage() { user_id = userFaceSet.user_id, secret_id = model.secret_id, face_token = face_token, image = bitmap.Item2, image_type = model.image_type, api_group_id = group.id, api_user_id = model.user_id, api_user_info = model.user_info, quality_control = model.quality_control, liveness_control = model.quality_control, sign = model.sign, timestamp = model.timestamp, is_delete = false, api_respone = JsonConvert.SerializeObject(request, UnderlineSplitContractResolver.GetSettings()), created_at = dateTime, updated_at = dateTime }; using (var tra = SqlDapperHelper.GetOpenConnection().BeginTransaction()) { await Service_user_face_storage.Remove_Storage_By_ApiUserID(user, tra); if (user_images == null) { await Service_user_face_image_data.Creatuser_face_image_data(user_images, tra); } await SqlDapperHelper.ExecuteInsertAsync(face_Storage, tra); tra.Commit(); } } catch (Exception ex) { LogHelperNLog.Error(ex, "操作日志添加失败"); request.Enum = RequestEnum.操作日志添加失败; } #endregion return Ok(request); }
/// <summary> /// 获取人脸坐标(单个文件) /// </summary> /// <param name="files"></param> /// <returns></returns> public async Task <ActionResult> GetFaceLocation(FaceLocationsViewModel model) { RequestFaceModel request = new RequestFaceModel() { Status = 500, Message = null, FaceList = null }; #region 基本验证 // 模型验证 if (!ModelState.IsValid) { request.Message = ModelState.Where(e => e.Value.Errors.Count > 0).FirstOrDefault().Value.Errors[0].ErrorMessage; request.Status = (int)RequestEnum.必要参数未传入; return(Ok(request)); } // 根据 secretkey 获取set var userFaceSet = await Service_user_face_set.GetUser_Face_Set_By_Secret_key_Async(model.secret_id); if (userFaceSet == null) { // 您未开通此项目 request.Enum = RequestEnum.没有接口权限; return(Ok(request)); } // 签名判断 var dic = ModleToDictionary.GetProperties(model); var key = userFaceSet.secret_key; dic.Remove("sign"); var sign = Signature.GetSign(dic, key); if (sign != model.sign) { // 签名错误 request.Enum = RequestEnum.签名错误; return(Ok(request)); } #endregion #region 余额逻辑判断 // 根据 userFaceSet.user_id 获取用户信息 var user = await Service_user.GetUser_By_User_id_Async(userFaceSet.user_id); // 根据user_id 先查预付款,未查询到则扣费使用 var prepayment = await Service_user_face_detect_prepayment.GetUser_Face_Detect_Prepayment_By_User_id_Asynv(user.id); user_face_detect_price derectPrice = null; if (prepayment == null || prepayment.number <= 0) { prepayment = null; derectPrice = await Service_user_face_detect_price.GetUser_Face_Detect_Price_By_User_idAsync(user.id); if (derectPrice == null) { // 用户没有预付费次数 request.Enum = RequestEnum.没有预付费次数未开通余额付款; } else { // 查询用户余额 derectPrice.price>余额 if (user.use_amount < derectPrice.price) { // 预付费次数为0,余额不足 request.Enum = RequestEnum.预付费次数为0且余额不足; } } } // 用户余额验证 if (!string.IsNullOrEmpty(request.Message)) { return(Ok(request)); } #endregion #region 获取图片 // 变量初始化 var face_token = string.Empty; (Bitmap, string)bitmap = (null, null); if (model.image_type.ToUpper() == "FACE_TOKEN") { if (model.image.Length > 50) { request.Enum = RequestEnum.face_token格式错误; return(Ok(request)); } var detect = await Service_user_face_detect_log.GetUser_Face_Detect_Log_By_Face_token(model.image); if (detect == null) { request.Enum = RequestEnum.face_token不存在; return(Ok(request)); } bitmap.Item2 = detect.image; var obj = JsonConvert.DeserializeObject <dynamic>(detect.api_respone); request.FaceList = obj.face_list; request.FaceNum = obj.face_num; face_token = model.image; } else { // 获取图片文件 bitmap = model.image_type.ToUpper() == "BASE64" ? FileCommon.Base64ToBitmap(model.image) : FileCommon.UrlToBitmap(model.image); if (bitmap.Item1 == null && !string.IsNullOrEmpty(bitmap.Item2)) { if (model.image_type.ToUpper() == "BASE64") { request.Enum = RequestEnum.Base64图片格式错误; } else { request.Enum = RequestEnum.从图片的url下载图片失败; } return(Ok(request)); } } #endregion #region 图片以及数据处理 // 不是 FACE_TOKEN 的处理方式 if (model.image_type.ToUpper() != "FACE_TOKEN") { // 查找是否有相同的图片 face_token = model.image_type.ToUpper() == "BASE64" ? EncryptProvider.Md5(model.image) : EncryptProvider.Md5(FileCommon.BitmapToBase64(bitmap.Item1)); var detect_Log = await Service_user_face_detect_log.GetUser_Face_Detect_Log_By_Face_token(face_token); if (detect_Log != null) { bitmap.Item2 = detect_Log.image; } else { try { // 存入本地 bitmap.Item2 = FileCommon.SaveBitmap(bitmap.Item1, bitmap.Item2); } catch (Exception ex) { LogHelperNLog.Error(ex); request.Enum = RequestEnum.数据存储处理失败; return(Ok(request)); } } // 获取人脸数据 //var faceDate = FaceDetectionCompute.GetRectangle(bitmap.Item1); DlibDotNet.Rectangle[] faceDate = new DlibDotNet.Rectangle[0]; try { // 截取人像,面积排序 faceDate = FaceDetectionCompute.GetRectangle(bitmap.Item1).OrderByDescending(d => d.Area).Take(model.max_face_num).ToArray(); //faceDate = FaceDetectionCompute.GetResult(bitmap.Item2); } catch (Exception ex) { LogHelperNLog.Error(ex, "FaceDetectionCompute.GetRectangles:人脸获取坐标出错"); request.Enum = RequestEnum.无法解析人脸; return(Ok(request)); } // 人脸关键点识别 var landmark68 = FaceLandmarkDetectionCompute.DetsTolandmark68(bitmap.Item1, faceDate); if (model.face_field.ToUpper().Contains("LANDMARK")) { request.LandmarkList = landmark68.Select(x => x.Item1); } // 人脸旋转角度 var angles = FaceDetectionCompute.GetRotationAngle(landmark68.Select(x => x.Item2)); // 人脸之态预测 var gesture = FaceDetectionCompute.GetFaceGesture(landmark68.Select(x => x.Item2), bitmap.Item1.Width, bitmap.Item1.Height); if (faceDate != null) { // 返回数据格式处理 var json = JsonConvert.SerializeObject(faceDate); var data = JsonConvert.DeserializeObject <List <FaceLocationsOutMdole> >(json); //data = data.OrderByDescending(d => d.Area).Take(model.max_face_num).ToList(); data.ForEach(x => { x.FaceToken = face_token; }); for (int i = 0; i < data.Count; i++) { data[i].FaceToken = face_token; data[i].Angle = angles[i]; data[i].Gesture = new Gesture() { Pitch = gesture[i].Item1, Yaw = gesture[i].Item2, Roll = gesture[i].Item3 }; } request.FaceList = data; request.FaceNum = data.Count(); } } #endregion request.Enum = RequestEnum.Succeed; #region 日志&&扣费 try { // 预付费/扣费 log // 时间戳 DateTime dateTime = DateTime.Now; user_face_detect_prepayment_detail prepayment_Detail = null; user_amount_detail amount_Detail = null; if (prepayment != null) { prepayment_Detail = new user_face_detect_prepayment_detail() { user_id = user.id, change_number = -1, now_number = prepayment.number - 1, info = string.Format("应用ID:{0},图片名称:{1}", model.secret_id, bitmap.Item2), created_at = dateTime, updated_at = dateTime }; } else { amount_Detail = new user_amount_detail() { user_id = user.id, change_amount = -derectPrice.price, now_amount = user.use_amount - derectPrice.price, info = string.Format("应用ID:{0},图片名称:{1},费用:{2}", model.secret_id, bitmap.Item2, derectPrice.price), created_at = dateTime, updated_at = dateTime, type = 0 }; } // 操作信息 user_face_detect_log face_Detect_Log = new user_face_detect_log() { user_id = user.id, secret_id = model.secret_id, face_token = face_token, image = bitmap.Item2, image_type = model.image_type, face_field = model.face_field, max_face_num = model.max_face_num, face_type = model.face_type, is_deductions_success = true, api_respone = JsonConvert.SerializeObject(request, UnderlineSplitContractResolver.GetSettings()), timestamp = Timelong.GetUnixDateTime(model.timestamp), sign = sign, created_at = dateTime, updated_at = dateTime, is_search = 1, }; // 修改余额,存库,日志 using (var tra = SqlDapperHelper.GetOpenConnection().BeginTransaction()) { if (prepayment != null) { await SqlDapperHelper.ExecuteInsertAsync(prepayment_Detail, tra); await Service_user_face_detect_prepayment.MinusUsernumber_By_User_id(prepayment_Detail, tra); } else { await SqlDapperHelper.ExecuteInsertAsync(amount_Detail, tra); await Service_user.MinusUserAmount_By_User_id(amount_Detail); } await SqlDapperHelper.ExecuteInsertAsync(face_Detect_Log, tra); tra.Commit(); } } catch (Exception ex) { LogHelperNLog.Error(ex, "操作日志添加失败"); request.Enum = RequestEnum.操作日志添加失败; return(Ok(request)); } #endregion return(Ok(request)); }
/// <summary> /// 创建用户组 /// </summary> /// <returns></returns> public async Task<ActionResult> CreatGroup(FaceDatabaseModel.GroupModel model) { RequestFaceModel request = new RequestFaceModel() { Status = 500, Message = null, FaceList = null }; //FaceDatabaseModel.CreatGroupModel creatGroup = new FaceDatabaseModel.CreatGroupModel(); // 基本验证 var userFaceSet = Verify(ref request, model); if (userFaceSet == null) { return Ok(request); } // 是否重复验证 var group = await Service_user_face_storage_group.Get_storage_group_BySecretGroupAsync(model.secret_id, model.group_id); if (group != null) { request.Enum = RequestEnum.数据重复; return Ok(request); } // 扣费等验证 // 保存到数据库 request.Enum = RequestEnum.Succeed; DateTime dateTime_Now = DateTime.Now; try { using (var tra = SqlDapperHelper.GetOpenConnection().BeginTransaction()) { long num = 0; user_face_storage_group storage_Group = new user_face_storage_group() { user_id = userFaceSet.user_id, group_name = model.group_id, remake = "", created_at = dateTime_Now, secret_id = model.secret_id, updated_at = dateTime_Now, api_respone = JsonConvert.SerializeObject(request, UnderlineSplitContractResolver.GetSettings()), is_delete = false }; num += await SqlDapperHelper.ExecuteInsertAsync(storage_Group); if (num >= 0) { tra.Commit(); } else { request.Enum = RequestEnum.数据存储处理失败; } } } catch (Exception ex) { LogHelperNLog.Error(ex, "操作日志添加失败"); request.Enum = RequestEnum.操作日志添加失败; return Ok(request); } return Ok(request); }
public async Task <ActionResult> FaceContrast([FromBody] FaceContrastViewModel model) { RequestFaceModel request = new RequestFaceModel() { Status = 500, Message = null, FaceList = null }; #region 基本验证 // 模型验证 if (!ModelState.IsValid) { request.Message = ModelState.Where(e => e.Value.Errors.Count > 0).FirstOrDefault().Value.Errors[0].ErrorMessage; request.Status = (int)RequestEnum.必要参数未传入; return(Ok(request)); } // 根据 secretkey 获取set var userFaceSet = await Service_user_face_set.GetUser_Face_Set_By_Secret_key_Async(model.secret_id); if (userFaceSet == null) { // 您未开通此项目 request.Enum = RequestEnum.没有接口权限; return(Ok(request)); } // 签名判断 var dic = ModleToDictionary.GetProperties(model); var key = userFaceSet.secret_key; dic.Remove("face_field"); dic.Remove("sign"); var sign = Signature.GetSign(dic, key); if (sign != model.sign) { // 签名错误 request.Enum = RequestEnum.签名错误; return(Ok(request)); } #endregion #region 余额逻辑判断 // 根据 userFaceSet.user_id 获取用户信息 var user = await Service_user.GetUser_By_User_id_Async(userFaceSet.user_id); //Service_user_face_compare_prepayment // 根据user_id 先查预付款,未查询到则扣费使用 var prepayment = await Service_user_face_compare_prepayment.GetUser_Face_Compare_Prepayment_By_User_id(user.id); user_face_compare_price comparePrice = null; if (prepayment == null || prepayment.number <= 0) { prepayment = null; comparePrice = await Service_user_face_compare_price.GetUser_Face_Compare_Price_By_User_idAsync(user.id); if (comparePrice == null) { // 用户没有预付费次数 request.Enum = RequestEnum.没有预付费次数未开通余额付款; } else { // 查询用户余额 derectPrice.price>余额 if (user.use_amount < comparePrice.price) { // 预付费次数为0,余额不足 request.Enum = RequestEnum.预付费次数为0且余额不足; } } } // 用户余额验证 if (!string.IsNullOrEmpty(request.Message)) { return(Ok(request)); } #endregion #region 获取图片 List <(Bitmap, string)> bitmaps = new List <(Bitmap, string)>(); List <ContrastData> face_tokens = new List <ContrastData>(); for (int i = 0; i < model.images.Count; i++) { var image = model.images[i]; var face_token = string.Empty; (Bitmap, string)bitmap = (null, null); // 获取图片文件 bitmap = image.image_type.ToUpper() == "BASE64" ? FileCommon.Base64ToBitmap(image.image) : FileCommon.UrlToBitmap(image.image); if (bitmap.Item1 == null && !string.IsNullOrEmpty(bitmap.Item2)) { if (image.image_type.ToUpper() == "BASE64") { request.Enum = RequestEnum.Base64图片格式错误; } else { request.Enum = RequestEnum.从图片的url下载图片失败; } return(Ok(request)); } #endregion #region 图片以及数据处理 // 查找是否有相同的图片 face_token = image.image_type.ToUpper() == "BASE64" ? EncryptProvider.Md5(image.image) : EncryptProvider.Md5(FileCommon.BitmapToBase64(bitmap.Item1)); // 查询 图片库 // 判断是否还需要进行图片存储 var user_images = await Service_user_face_image_data.Getuser_face_image_data_By_Token_Async(face_token); if (user_images != null) { bitmap.Item2 = user_images.url; } else { try { // 存入本地 bitmap.Item2 = FileCommon.SaveBitmap(bitmap.Item1, bitmap.Item2); } catch (Exception ex) { LogHelperNLog.Error(ex); request.Enum = RequestEnum.数据存储处理失败; return(Ok(request)); } } bitmaps.Add(bitmap); face_tokens.Add(new ContrastData() { face_token = face_token }); } #region old Code //foreach (var image in model.images) //{ // // 变量初始化 // var face_token = string.Empty; // (Bitmap, string) bitmap = (null, null); // bitmap = image.image_type.ToUpper() == "BASE64" ? FileCommon.Base64ToBitmap(image.image) : FileCommon.UrlToBitmap(image.image); // if (bitmap.Item1 == null && !string.IsNullOrEmpty(bitmap.Item2)) // { // if (image.image_type.ToUpper() == "BASE64") // { // request.Enum = RequestEnum.Base64图片格式错误; // request.Message = string.IsNullOrEmpty(request.Message) ? num + RequestEnum.Base64图片格式错误.ToString() : request.Message + num + RequestEnum.Base64图片格式错误.ToString(); // } // else // { // request.Enum = RequestEnum.从图片的url下载图片失败; // request.Message = string.IsNullOrEmpty(request.Message) ? num + RequestEnum.从图片的url下载图片失败.ToString() : request.Message + num + RequestEnum.从图片的url下载图片失败.ToString(); // } // return Ok(request); // } // bitmaps.Add(bitmap); //} //#endregion //#region 图片以及数据处理 //List<ContrastData> face_tokens = new List<ContrastData>(); //for (int i = 0; i < bitmaps.Count; i++) //{ // var face_token = string.Empty; // face_token = EncryptProvider.Md5(FileCommon.BitmapToBase64(bitmaps[i].Item1)); // //此处后期要更改为人脸库的图片 // var detect_Log = await Service_user_face_detect_log.GetUser_Face_Detect_Log_By_Face_token(face_token); // if (detect_Log != null) // { // bitmaps[i] = (bitmaps[i].Item1, detect_Log.image); // //bitmaps[i].Item2 = detect_Log.image; // } // else // { // try // { // // 存入本地 // bitmaps[i] = (bitmaps[i].Item1, FileCommon.SaveBitmap(bitmaps[i].Item1, bitmaps[i].Item2)); // } // catch (Exception ex) // { // LogHelperNLog.Error(ex); // request.Enum = RequestEnum.数据存储处理失败; // return Ok(request); // } // } // face_tokens.Add(new ContrastData() { // face_token=face_token // }); //} #endregion var difference = DnnFaceRecognitionCompute.GetOutputLabels(bitmaps[0].Item1, bitmaps[1].Item1); // 是否需要人脸位置 if (model.face_field.ToUpper().Contains("FACE_SHAPE")) { for (int i = 0; i < face_tokens.Count; i++) { face_tokens[i].rectangles = difference.Item2[i]; } } request.FaceList = face_tokens; request.Score = difference.Item1; #endregion request.Enum = RequestEnum.Succeed; //日志&&扣费 try { // 预付费/扣费 log // 时间戳 DateTime dateTime = DateTime.Now; user_face_compare_prepayment_detail prepayment_Detail = null; user_amount_detail amount_Detail = null; if (prepayment != null) { prepayment_Detail = new user_face_compare_prepayment_detail() { user_id = user.id, change_number = -1, now_number = prepayment.number - 1, info = string.Format("应用ID:{0}", model.secret_id), created_at = dateTime, updated_at = dateTime }; } else { amount_Detail = new user_amount_detail() { user_id = user.id, change_amount = -comparePrice.price, now_amount = user.use_amount - comparePrice.price, info = string.Format("应用ID:{0},费用:{1}", model.secret_id, comparePrice.price), created_at = dateTime, updated_at = dateTime, type = 0 }; } // 操作信息 user_face_compare_log face_Compare_Log = new user_face_compare_log() { user_id = user.id, secret_id = model.secret_id, image = JsonConvert.SerializeObject(bitmaps.Select(x => x.Item2)), is_deductions_success = true, api_respone = JsonConvert.SerializeObject(request, UnderlineSplitContractResolver.GetSettings()), timestamp = Timelong.GetUnixDateTime(model.timestamp), sign = sign, created_at = dateTime, updated_at = dateTime, }; // 修改余额,存库,日志 using (var tra = SqlDapperHelper.GetOpenConnection().BeginTransaction()) { if (prepayment != null) { await SqlDapperHelper.ExecuteInsertAsync(prepayment_Detail, tra); await Service_user_face_compare_prepayment.MinusUsernumber_By_User_id(prepayment_Detail, tra); } else { await SqlDapperHelper.ExecuteInsertAsync(amount_Detail, tra); await Service_user.MinusUserAmount_By_User_id(amount_Detail); } await SqlDapperHelper.ExecuteInsertAsync(face_Compare_Log, tra); tra.Commit(); } } catch (Exception ex) { LogHelperNLog.Error(ex, "操作日志添加失败"); request.Enum = RequestEnum.操作日志添加失败; return(Ok(request)); } return(Ok(request)); }