Exemple #1
0
 public FaceCategory(string countryCode, FaceGender gender)
 {
     Guard.Argument(countryCode, nameof(countryCode)).NotNull().NotWhiteSpace();
     this.CountryCode           = countryCode;
     this.countryCodeNormalized = countryCode.ToUpperInvariant();
     this.Gender = gender;
 }
Exemple #2
0
 public static bool IsSameAs(this Gender detectedGender, FaceGender statedGender)
 {
     return((detectedGender == Gender.Male && statedGender == FaceGender.Male) ||
            (detectedGender == Gender.Female && statedGender == FaceGender.Female) ||
            (detectedGender != Gender.Male && detectedGender != Gender.Female &&
             statedGender != FaceGender.Male && statedGender != FaceGender.Female));
 }
Exemple #3
0
 private static bool IsSameGender(Gender detectedGender, FaceGender statedGender)
 {
     return((detectedGender == Gender.Male && statedGender == FaceGender.Male) ||
            (detectedGender == Gender.Female && statedGender == FaceGender.Female) ||
            (detectedGender != Gender.Male && detectedGender != Gender.Female &&
             statedGender != FaceGender.Male && statedGender != FaceGender.Female));
 }
Exemple #4
0
        private void Init()
        {
            _age       = new FaceAge(AppId, AgeKey);
            _gender    = new FaceGender(AppId, GenderKey);
            _traking   = LocatorFactory.GetTrackingLocator(AppId, FtKey, _age, _gender) as FaceTracking;
            _detection = LocatorFactory.GetDetectionLocator(AppId, FdKey) as FaceDetection;
            _recognize = new FaceRecognize(AppId, FrKey);
            _processor = new FaceProcessor(_traking, _recognize);

            //init cache
            if (Directory.Exists(FaceLibraryPath))
            {
                var files = Directory.GetFiles(FaceLibraryPath);
                foreach (var file in files)
                {
                    var info = new FileInfo(file);
                    _cache.Add(info.Name.Replace(info.Extension, ""), File.ReadAllBytes(file));
                }
            }

            stride     = width * pixelSize;
            bufferSize = stride * height;

            _pImage = Marshal.AllocHGlobal(bufferSize);
            _image  = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, _pImage);

            var ffmpeg = new FFMpegConverter();

            outputStream = new MemoryStream();

            var setting =
                new ConvertSettings
            {
                CustomOutputArgs = "-s 1920x1080", //根据业务需求-r参数可以调整,取决于摄像机的FPS
            };                                     //-s 1920x1080 -q:v 2 -b:v 64k

            //-an -r 15 -pix_fmt bgr24 -updatefirst 1
            //task = ffmpeg.ConvertLiveMedia("rtsp://*****:*****@192.168.1.64:554/h264/ch1/main/av_stream", null,
            //    outputStream, Format.raw_video, setting);

            /*
             * USB摄像头捕获
             * 通过ffmpeg可以捕获USB摄像,如下代码所示。
             * 首先通过:ffmpeg -list_devices true -f dshow -i dummy命令,可以列出系统中存在的USB摄像设备(或通过控制面板的设备管理工具查看设备名称),例如在我电脑中叫USB2.0 PC CAMERA。
             * 然后根据捕获的分辨率,修改视频图形信息,包括width和height,一般像素大小不用修改,如果要参考设备支持的分辨率,可以使用:
             * ffmpeg -list_options true -f dshow -i video="USB2.0 PC CAMERA"命令
             */
            task = ffmpeg.ConvertLiveMedia("video=Logitech HD Webcam C270", "dshow",
                                           outputStream, Format.raw_video, setting);

            task.OutputDataReceived += DataReceived;
            task.Start();

            _renderTask = new Task(Render);
            _renderTask.Start();
        }
Exemple #5
0
        public FaceDetectionService()
        {
            _age    = new FaceAge(AppConfigurations.AppId, AppConfigurations.AgeKey);        // 年龄识别
            _gender = new FaceGender(AppConfigurations.AppId, AppConfigurations.GenderKey);  // 性别识别
            //// 图片检测人脸
            _detection = LocatorFactory.GetDetectionLocator(AppConfigurations.AppId, AppConfigurations.FdKey, _age, _gender) as FaceDetection;
            _traking   = LocatorFactory.GetTrackingLocator(AppConfigurations.AppId, AppConfigurations.FtKey, _age, _gender) as FaceTracking;

            _recognize = new FaceRecognize(AppConfigurations.AppId, AppConfigurations.FrKey);

            _processor            = new FaceProcessor(_detection, _recognize);
            _personFaceRepository = new PersonFaceRepository();
        }
Exemple #6
0
        public async Task RemoveGenderMismatches(string imagesDirectory, FaceGender expectedGender, string quarantineDirectory)
        {
            this.fileSystem.TestWritePermissionsOrCreateDirectoryIfNotExists(quarantineDirectory);

            foreach (string imageFile in this.fileSystem.Directory
                     .EnumerateFiles(imagesDirectory, "*", SearchOption.AllDirectories))
            {
                try
                {
                    using (Stream stream = this.fileSystem.File.OpenRead(imageFile))
                    {
                        IList <DetectedFace> faces = await this.faceClient.Face.DetectWithStreamAsync(
                            stream,
                            returnFaceId : true,
                            returnFaceLandmarks : false,
                            new[] { FaceAttributeType.Gender, FaceAttributeType.Age }).ConfigureAwait(false);

                        Gender detectedGender = faces[0].FaceAttributes.Gender.Value;

                        if (faces.Count != 1)
                        {
                            this.logger.LogDebug(
                                $"Removing '{imageFile}' because the image contains {faces.Count} faces instead of 1");
                        }
                        else if (detectedGender.IsSameAs(expectedGender))
                        {
                            this.logger.LogDebug(
                                $"Removing '{imageFile}' because the detected gender '{detectedGender}' is not '{expectedGender}'");
                        }
                        else
                        {
                            continue;
                        }

                        string destinationFile = this.fileSystem.Path.Combine(
                            quarantineDirectory,
                            this.fileSystem.Path.GetFileName(imageFile));
                        this.fileSystem.File.Move(imageFile, destinationFile);
                    }
                }
                catch (Exception ex)
                {
                    this.logger.LogDebug($"Skipping '{imageFile}' due to error: '{ex.Message}'");
                }
            }
        }
Exemple #7
0
        private static void TestAgeAndGender()
        {
            using (var detection = LocatorFactory.GetDetectionLocator("appid", "key"))
            {
                var image1 = Image.FromFile("test2.jpg");
                using (var estimate = new FaceAge("appid", "key"))
                {
                    var result1 = estimate.StaticEstimation(detection, new Bitmap(image1));
                    foreach (var result1Age in result1.Ages)
                    {
                        Console.WriteLine(result1Age);
                    }
                }

                using (var estimate = new FaceGender("appid", "key"))
                {
                    var result1 = estimate.StaticEstimation(detection, new Bitmap(image1));
                    foreach (var result1Gender in result1.Genders)
                    {
                        Console.WriteLine(result1Gender);
                    }
                }
            }

            //another
            var age    = new FaceAge("appid", "key");
            var gender = new FaceGender("appid", "key");

            using (var detection = LocatorFactory.GetDetectionLocator("appid", "key", age, gender))
            {
                var image1 = Image.FromFile("test2.jpg");
                var result = detection.Detect(new Bitmap(image1), out var location,
                                              LocateOperation.IncludeAge | LocateOperation.IncludeGender); //default is None, no age and gender estimation
                for (var i = 0; i < location.FaceCount; i++)
                {
                    Console.WriteLine(location.Ages[i]);
                    Console.WriteLine(location.Genders[i]);
                }
            }
            age.Dispose();
            gender.Dispose();
        }