Пример #1
0
        private static List <(OutputLabels <Matrix <float> >, Rectangle[])> GetData(List <Bitmap> bitmaps, bool isAFace = false)
        {
            var datas = new List <(OutputLabels <Matrix <float> >, Rectangle[])>();

            try
            {
                foreach (var bitmap in bitmaps)
                {
                    var faces = new List <Matrix <RgbPixel> >();
                    var dets  = new Rectangle[0];
                    //在图像中寻找人脸我们需要一个人脸检测器:
                    using (var detector = Dlib.GetFrontalFaceDetector())
                    {
                        using (var img = bitmap.ToMatrix <RgbPixel>())
                        {
                            // 人脸 面积从大到小排序
                            dets = detector.Operator(img).OrderByDescending(x => x.Area).ToArray();
                            // 是否只检测面积最大的人脸
                            if (isAFace)
                            {
                                var shape          = _SP.Detect(img, dets[0]);
                                var faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                                var faceChip       = Dlib.ExtractImageChip <RgbPixel>(img, faceChipDetail);
                                faces.Add(faceChip);
                            }
                            else
                            {
                                foreach (var face in dets)
                                {
                                    var shape          = _SP.Detect(img, face);
                                    var faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                                    var faceChip       = Dlib.ExtractImageChip <RgbPixel>(img, faceChipDetail);
                                    faces.Add(faceChip);
                                }
                            }
                            if (!faces.Any())
                            {
                                datas.Add((null, null));
                            }
                            else
                            {
                                //此调用要求DNN将每个人脸图像转换为128D矢量。
                                //在这个128D的矢量空间中,来自同一个人的图像会彼此接近
                                //但是来自不同人的载体将会非常不同。所以我们可以用这些向量
                                //辨别一对图片是来自同一个人还是不同的人。
                                datas.Add((_NET.Operator(faces), dets));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex);
            }
            return(datas);
        }
Пример #2
0
        /// <summary>
        /// 人脸删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ActionResult> RemoveFace(FaceDatabaseModel.RemoveFaceModel 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);
            }


            // 判断此人相同数据是否已经存在
            var group_user = await Service_user_face_storage.GetUser_By_UGS(model.user_id, group.id, model.face_token, model.secret_id);

            if (group_user == null)
            {
                request.Enum = RequestEnum.数据不存在;
                return Ok(request);
            }

            group_user.is_delete = true;
            group.updated_at = DateTime.Now;

            try
            {
                // 数据库删除,增加日志
                await Service_user_face_storage.Remove_Storage_By_ApiUserIDFaceToken(group_user);
                request.Enum = RequestEnum.Succeed;
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex, "操作日志添加失败");
                request.Enum = RequestEnum.操作日志添加失败;
            }

            return Ok(request);


        }
Пример #3
0
        /// <summary>
        /// 获取用户列表
        /// </summary>
        public async Task<ActionResult> ReadtGroupUserList(FaceDatabaseModel.GroupUserListModel 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);
            }

            try
            {


                var userLists = await Service_user_face_storage.GetUser_Face_Storage_By_UGS_Asunc(model.secret_id, group.id, model.start, model.length);


                List<string> userIDList = new List<string>();
                userLists.ForEach(x =>
                {
                    userIDList.Add(x.api_user_id);
                });

                request.UserIdList = userIDList;

                request.Enum = RequestEnum.Succeed;
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex);
                request.Enum = RequestEnum.服务端请求失败;
            }
            return Ok(request);

        }
Пример #4
0
        /// <summary>
        /// 删除用户组
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> RemoveGroup(FaceDatabaseModel.GroupModel 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);
            }

            group.is_delete = true;//是否移除
            group.updated_at = DateTime.Now;
            try
            {
                using (var tra = SqlDapperHelper.GetOpenConnection().BeginTransaction())
                {
                    // 修改用户的删除状态
                    await Service_user_face_storage.Remove_Storage_By_Group(group, tra);
                    // 修改用户组的删除状态
                    await Service_user_face_storage_group.Remove_storage_group(group, tra);
                    tra.Commit();
                    request.Enum = RequestEnum.Succeed;
                }
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex, "操作日志添加失败");
                request.Enum = RequestEnum.操作日志添加失败;
            }



            return Ok(request);

        }
Пример #5
0
        /// <summary>
        /// 多张图片
        /// </summary>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        public static List <Rectangle[]> GetRectangle(List <Bitmap> bitmaps)
        {
            // 要返回的数据
            List <Rectangle[]> rectangles = new List <Rectangle[]>();

            try
            {
                rectangles = GetDate(bitmaps);
            }
            catch (Exception ex)
            {
                // 出错日志打印
                LogHelperNLog.Error(ex, "FaceDetectionCompute.GetRectangles:人脸获取坐标出错");
            }
            return(rectangles.Count > 0 ? rectangles : null);
        }
Пример #6
0
        /// <summary>
        /// 获取用户人脸列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ActionResult> ReadtFaceList(FaceDatabaseModel.GroupUserModel 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);
            }

            try
            {
                var userLists = await Service_user_face_storage.GetUserList_By_UGS(model.user_id, group.id, model.secret_id);

                request.Enum = RequestEnum.Succeed;
                request.FaceList = userLists.Select(x => new
                {
                    x.face_token,
                    ctime = x.created_at
                });
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex);
                request.Enum = RequestEnum.服务端请求失败;
            }

            return Ok(request);

        }
Пример #7
0
        /// <summary>
        /// 逻辑处理
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public async Task <List <Rectangle[]> > GetRectanglesAsync(List <IFormFile> files)
        {
            var filePath = Path.GetTempFileName();
            List <Rectangle[]> rectangles = new List <Rectangle[]>();
            var bitmaps = await FileToBitmapAsync(files);

            try
            {
                foreach (var bitmap in bitmaps)
                {
                    rectangles.Add(Face(bitmap));
                }
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex, "FaceDetectionController.GetRectanglesAsync:人脸获取坐标出错");
            }
            return(rectangles);
        }
Пример #8
0
        /// <summary>
        /// 查找分组
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> ReadtGroupList(FaceDatabaseModel.GroupListModel model)
        {

            RequestFaceModel request = new RequestFaceModel()
            {
                Status = 500,
                Message = null,
                FaceList = null
            };

            // 基本验证
            var userFaceSet = Verify(ref request, model);
            if (userFaceSet == null)
            {
                return Ok(request);
            }

            try
            {
                List<string> groupIDList = new List<string>();


                var groupList = await Service_user_face_storage_group.GetUser_Face_Storage_Groups_BySecretIdAsunc(model.secret_id, model.start, model.length);

                groupList.ForEach(x =>
                {
                    groupIDList.Add(x.group_name);
                });

                request.GroupIdList = groupIDList;

                request.Enum = RequestEnum.Succeed;
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex, "查找数据出错");
                request.Enum = RequestEnum.服务端请求失败;
            }

            return Ok(request);

        }
Пример #9
0
        /// <summary>
        /// 文件转化为图片(多个文件)
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public static async Task <List <Bitmap> > FileToBitmapAsync(List <IFormFile> files)
        {
            List <Bitmap> bitmaps  = new List <Bitmap>();
            var           filePath = AppDomain.CurrentDomain.BaseDirectory;

            foreach (var file in files)
            {
                try
                {
                    if (file.Length > 0)
                    {
                        var fileExtension = Path.GetExtension(file.FileName);
                        filePath += file.FileName;
                        using (var stream = new FileStream(filePath, FileMode.OpenOrCreate))
                        {
                            await file.CopyToAsync(stream);

                            //stream.Flush();
                            Bitmap bitmap = new Bitmap(stream);
                            if (fileExtension == ".png")
                            {
                                //Bitmap bitmap24 = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format24bppRgb);
                                //Graphics g = Graphics.FromImage(bitmap24);
                                //g.DrawImageUnscaled(bitmap, 0, 0);
                                //bitmap = bitmap24;
                                bitmap = bitmap.Clone(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb);
                            }
                            bitmaps.Add(bitmap);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelperNLog.Error(ex, "FileCommon.FileToBitmapAsync 文件转换图片出错");
                }
            }
            return(bitmaps);
        }
Пример #10
0
        /// <summary>
        /// 复制用户
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> CopyGroupUser(FaceDatabaseModel.CopyUserListModel 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.src_group_id);
            if (group == null)
            {
                request.Enum = RequestEnum.数据不存在;
                return Ok(request);
            }

            var 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);
            }

            var newgroup = await Service_user_face_storage_group.Get_storage_group_BySecretGroupAsync(model.secret_id, model.dst_group_id);
            if (newgroup == null)
            {
                request.Enum = RequestEnum.数据不存在;
                return Ok(request);
            }

            var newgroup_user = await Service_user_face_storage.GetUser_By_UGS(model.user_id, newgroup.id, model.secret_id);
            if (newgroup_user != null)
            {
                request.Enum = RequestEnum.数据重复;
                return Ok(request);
            }



            group_user.api_group_id = newgroup.id;

            try
            {
                await SqlDapperHelper.ExecuteInsertAsync(group_user);
                request.Enum = RequestEnum.Succeed;
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex, "操作日志添加失败");
                request.Enum = RequestEnum.操作日志添加失败;
            }

            return Ok(request);

        }
Пример #11
0
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> RemoveUser(FaceDatabaseModel.GroupUserModel 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
            };

            try
            {
                // 数据库删除,增加日志
                if (model.group_id == "@ALL")
                {
                    await Service_user_face_storage.Remove_Storage_By_ApiUserID_All(user);
                }
                else
                {
                    user.api_group_id = group.id;
                    await Service_user_face_storage.Remove_Storage_By_ApiUserID(user);
                }
                request.Enum = RequestEnum.Succeed;
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex, "操作日志添加失败");
                request.Enum = RequestEnum.操作日志添加失败;
            }

            return Ok(request);

        }
Пример #12
0
        /// <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);

        }
Пример #13
0
        /// <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);
        }
Пример #14
0
        public async Task <ActionResult> Search(FaceFindModel.Search 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 获取图片

            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));
                }
            }

            #endregion



            //var bitmaps = "从数据库查询,然后加载Bitmap";

            // 查询分组
            var groupList = await Service_user_face_storage_group.Get_storage_group_BySecretGroupAsync(model.secret_id, model.group_id_list.Split(','));

            // 查询到用户信息
            var users = await Service_user_face_storage.GetUserList_By_UGS(model.user_id, groupList.Select(x => x.id).ToList(), model.secret_id);



            var bitmaps = FileCommon.SrcToBitmap(users.Select(x => x.image));

            var difference = DnnFaceRecognitionCompute.GetOutputLabels(bitmap.Item1, bitmaps);

            // 过滤相识度
            List <FaceFindOutModel.User> list = new List <FaceFindOutModel.User>();

            for (int i = 0; i < bitmaps.Count; i++)
            {
                var group = groupList.FirstOrDefault(x => x.id == users[i].api_group_id);
                list.Add(new FaceFindOutModel.User
                {
                    GroupId  = group != null ? group.group_name : null,
                    UserId   = users[i].api_user_id,
                    UserInfo = users[i].api_user_info,
                    Score    = difference[i].Item1
                });
            }

            request.FaceToken = face_token;
            request.FaceList  = list;


            // 数据处理,存库,日志

            return(Ok(request));
        }
Пример #15
0
        /// <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));
        }
Пример #16
0
        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));
        }