Esempio n. 1
0
        //private static void ReadImage()
        //{
        //    src = new Mat("Images/IMG_20160315_222740.jpg", ImreadModes.Unchanged);
        //    src = ImageUtil.ProportionalImageResize(src, 600);

        //    grey = src.CvtColor(ColorConversionCodes.BGR2GRAY);
        //    tresh = GetTresh();

        //    using (var w = new Window("src", src))
        //    {
        //        using (var wd = new Window("dst", tresh))
        //        {
        //            wd.CreateTrackbar("t1", 0, 100, on_trackbart1);
        //            wd.CreateTrackbar("t2", 100, 255, on_trackbart2);

        //            Init();

        //            Cv2.WaitKey();
        //        }
        //    }
        //}

        //private static void ReadVideo()
        //{
        //    // Opens MP4 file (ffmpeg is probably needed)
        //    var capture = new VideoCapture("Videos/cube_003.avi");

        //    int sleepTime = (int)Math.Round(1000 / capture.Fps);

        //    using (var window = new Window("capture"))
        //    {
        //        using (var image = new Mat()) // Frame image buffer
        //        {
        //            // When the movie playback reaches end, Mat.data becomes NULL.
        //            while (true)
        //            {
        //                capture.Read(image); // same as cvQueryFrame
        //                if (image.Empty())
        //                    break;

        //                src = ImageUtil.GetCopy(image);
        //                grey = image.CvtColor(ColorConversionCodes.BGR2GRAY);
        //                tresh = GetTresh();

        //                //window.ShowImage(tresh);

        //                var corners = FaceDetector.GetFaceCorners(src, tresh, window);
        //                if (corners != null && !corners.Rotated)
        //                {
        //                    var face = FaceExtractor.Extract(src, corners);
        //                    if (FaceUniquenessDetector.IsUnique(face))
        //                    {
        //                        var bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(face);
        //                        bitmap.Save("Results\\face" + Guid.NewGuid() + ".jpg", ImageFormat.Jpeg);

        //                        var colors = ColorsExtractor.Extract(face);

        //                        var cs = new List<Color>();
        //                        for (int i = 0; i < 3; i++)
        //                        {
        //                            for (int j = 0; j < 3; j++)
        //                            {
        //                                var t = colors.GetColor($"{i}-{j}");
        //                                var closestColor = ColorsExtractor.ClosestColorHue(t);
        //                                cs.Add(closestColor);
        //                            }
        //                        }
        //                    }
        //                }

        //                var k = Cv2.WaitKey(sleepTime);
        //                if (k == 27) break;
        //            }
        //        }
        //    }
        //}

        //public static void Main()
        //{
        //    var resultDir = new DirectoryInfo("Results");
        //    if (resultDir.Exists)
        //    {
        //        resultDir.Delete(true);
        //        Thread.Sleep(2000);
        //    }
        //    resultDir.Create();

        //    //ReadImage();
        //    //ReadVideo();
        //}

        public static List <List <Color> > GetFaceColors(bool captureFromCamera, string path = null)
        {
            var jobId = Guid.NewGuid();

            var resultsDirectory = new DirectoryInfo($"Results\\{jobId}\\Detected");

            if (!resultsDirectory.Exists)
            {
                resultsDirectory.Create();
            }

            resultsDirectory = new DirectoryInfo($"Results\\{jobId}\\Extracted\\Unique");
            if (!resultsDirectory.Exists)
            {
                resultsDirectory.Create();
            }

            FaceUniquenessDetector.Init();

            var facesColors = new List <List <Color> >();

            var capture = captureFromCamera ? new VideoCapture(0) : new VideoCapture(path);

            capture.Set(CaptureProperty.FrameWidth, 1280);
            capture.Set(CaptureProperty.FrameHeight, 720);

            int sleepTime = (int)Math.Round(1000f / 60);

            using (var window = new Window("capture"))
            {
                using (var image = new Mat())
                {
                    while (true)
                    {
                        capture.Read(image);
                        if (image.Empty())
                        {
                            Cv2.DestroyAllWindows();
                            break;
                        }

                        src   = ImageUtil.GetCopy(image);
                        grey  = image.CvtColor(ColorConversionCodes.BGR2GRAY);
                        tresh = GetTresh();

                        var corners = FaceDetector.GetFaceCorners(src, tresh, jobId, window);
                        if (corners != null && !corners.Rotated)
                        {
                            var face = FaceExtractor.Extract(src, corners, jobId);
                            if (FaceUniquenessDetector.IsUnique(face))
                            {
                                var colors = ColorsExtractor.Extract(face, jobId);

                                var cs = new List <Color>();
                                for (int i = 0; i < 3; i++)
                                {
                                    for (int j = 0; j < 3; j++)
                                    {
                                        var color = colors.GetColor($"{i}-{j}");
                                        cs.Add(color);
                                    }
                                }
                                facesColors.Add(cs);
                            }
                        }

                        if (facesColors.Count == 6)
                        {
                            Cv2.DestroyAllWindows();
                            break;
                        }

                        var k = Cv2.WaitKey(sleepTime);
                        if (k == 27)
                        {
                            Cv2.DestroyAllWindows();
                            break;
                        }
                    }
                }
            }

            if (facesColors.Count != 6)
            {
                return(null);
            }

            return(facesColors);
        }
        public static bool IsUnique(Mat face)
        {
            if (_faces.Count == 0)
            {
                var color1 = ColorsExtractor.ExtractMiddleColor(face);
                color1 = ColorsExtractor.ClosestColorRgb(color1);

                _faces.Add(face);
                _facesMiddleColors.Add(color1);
                return(true);
            }

            /// Using 50 bins for hue and 60 for saturation
            int h_bins = 50;
            int s_bins = 60;

            int[] histSize = new int[2] {
                h_bins, s_bins
            };

            // hue varies from 0 to 179, saturation from 0 to 255
            var h_ranges = new Rangef(0, 180);
            var s_ranges = new Rangef(0, 256);

            var ranges = new Rangef[] { h_ranges, s_ranges };

            // Use the o-th and 1-st channels
            int[] channels = new int[2] {
                0, 1
            };

            var faceHsv = face.CvtColor(ColorConversionCodes.BGR2HSV);

            var faceHistogram = new Mat();

            Cv2.CalcHist(new Mat[] { faceHsv }, channels, new Mat(), faceHistogram, 2, histSize, ranges, true, false);
            Cv2.Normalize(faceHistogram, faceHistogram, 0, 1, NormTypes.MinMax, -1, new Mat());

            var color = ColorsExtractor.ExtractMiddleColor(face);

            foreach (var f in _faces)
            {
                var fHsv = f.CvtColor(ColorConversionCodes.BGR2HSV);

                var fHistogram = new Mat();
                Cv2.CalcHist(new Mat[] { fHsv }, channels, new Mat(), fHistogram, 2, histSize, ranges, true, false);
                Cv2.Normalize(fHistogram, fHistogram, 0, 1, NormTypes.MinMax, -1, new Mat());

                double correl = Cv2.CompareHist(faceHistogram, fHistogram, HistCompMethods.Correl);

                //if (correl > 0.5) return false;
                bool contains = _facesMiddleColors.Contains(color);
                if (correl > 0.4 && contains)
                {
                    return(false);
                }
                //if (correl > 0.65) return false;
            }

            color = ColorsExtractor.ExtractMiddleColor(face);

            _faces.Add(face);
            _facesMiddleColors.Add(color);
            return(true);
        }