Exemplo n.º 1
0
        /// <summary>
        /// 根据locate数据,从图片中提供人脸特征,先执行 FaceLocateResult() 获得图片的人脸特征
        /// </summary>
        /// <param name="bitmap">图片</param>
        /// <param name="locate">图片中的人脸数据</param>
        /// <returns>返回一个人脸特征,适用于图片中只有一个人脸的情况</returns>
        public Feature GetFaceFeature(Bitmap bitmap, LocateResult locate)
        {
            Feature feature = null;

            var result = _detection.Detect(bitmap, out var locateResult);

            if (result == Stepon.FaceRecognization.Common.ErrorCode.Ok && locateResult.HasFace)
            {
                using (_recognize = new FaceRecognize(AppConfigurations.AppId, AppConfigurations.FrKey))
                {
                    feature = _recognize.ExtractFeature(locate.OffInput, locate.Faces[0], locate.FacesOrient[0]);
                }
            }

            return(feature);
        }
Exemplo n.º 2
0
        private void WriteFeature(Bitmap bitmap)
        {
            var code = _detection.Detect(bitmap, out var locate);

            try
            {
                if (code == ErrorCode.Ok)
                {
                    if (locate.HasFace)
                    {
                        if (locate.FaceCount > 1)
                        {
                            MessageBox.Show("选定的图片检测到多余一张的人脸,请重新选择");
                            return;
                        }

                        var name = userIdentity.Text;

                        using (var feature =
                                   _recognize.ExtractFeature(locate.OffInput, locate.Faces[0], locate.FacesOrient[0]))
                        {
                            if (!Directory.Exists(FaceLibraryPath))
                            {
                                Directory.CreateDirectory(FaceLibraryPath);
                            }

                            File.WriteAllBytes(Path.Combine(FaceLibraryPath, $"{name}.dat"), feature.FeatureData);

                            try
                            {
                                _cacheLock.EnterWriteLock();
                                _cache.Add(name, feature.FeatureData);
                            }
                            finally
                            {
                                _cacheLock.ExitWriteLock();
                            }
                        }

                        userIdentity.Text = "";
                    }
                }
                else
                {
                    MessageBox.Show(code.ToString());
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
            finally
            {
                locate.Dispose();
            }

            MessageBox.Show("建立特征成功");
        }