Beispiel #1
0
 private Area GetAssociatedArea(GTLocation groundTruth)
 {
     return(_production.AreaConfig.Single(a => a.GTRegionStart <= groundTruth.Estimated && a.GTRegionStop > groundTruth.Estimated));
 }
        private static IEnumerable <GTLocation> ProcessTake(Guid cameraId, Take take, Mat reference)
        {
            var media       = take.Media.Single(m => m.CameraId == cameraId);
            var camera      = new Capture(media.Url.AbsoluteUri + "full_720p.mp4");
            var gtLocations = new List <GTLocation>();

            CvInvoke.NamedWindow("Feed");
            CvInvoke.NamedWindow("Reference");
            CvInvoke.NamedWindow("Delta");

            var frame  = new Mat();
            var resize = new Mat();
            var delta  = new Mat();
            var grey   = new Mat();
            var blur   = new Mat();
            var thresh = new Mat();
            var dilate = new Mat();

            camera.Start();
            var frameCount  = camera.GetCaptureProperty(CapProp.FrameCount);
            var fps         = camera.GetCaptureProperty(CapProp.Fps);
            var frameNumber = 0;

            camera.ImageGrabbed += (sender, eventArgs) =>
            {
                try
                {
                    Console.Write($"\r    Processing frame: {frameNumber}/{frameCount}");
                    camera.Retrieve(frame);
                    CvInvoke.Resize(frame, resize, Resolution);

                    CvInvoke.CvtColor(resize, grey, ColorConversion.Bgr2Gray);
                    CvInvoke.GaussianBlur(grey, blur, new Size(21, 21), 0);

                    CvInvoke.AbsDiff(reference, blur, delta);
                    CvInvoke.Threshold(delta, thresh, 25, 255, ThresholdType.Binary);
                    CvInvoke.Rectangle(thresh, new Rectangle(0, 0, Resolution.Width, Resolution.Height - ROI.Height), new MCvScalar(0), -1);

                    CvInvoke.Imshow("Reference", reference);

                    CvInvoke.Dilate(thresh, dilate, null, Point.Empty, 0, BorderType.Default, new MCvScalar());
                    CvInvoke.Imshow("Delta", dilate);
                    VectorOfVectorOfPoint conts = new VectorOfVectorOfPoint();
                    CvInvoke.FindContours(dilate, conts, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);

                    for (var i = 0; i < conts.Size; i++)
                    {
                        if (CvInvoke.ContourArea(conts[i]) < MinArea)
                        {
                            continue;
                        }
                        var rect = CvInvoke.BoundingRectangle(conts[i]);
                        CvInvoke.Rectangle(resize, rect, new MCvScalar(0, 255), 2);
                        CvInvoke.Rectangle(resize, ROI, new MCvScalar(255), 2);
                        CvInvoke.Imshow("Reference", resize);

                        var location = new GTLocation
                        {
                            Timestamp = media.StartTime.AddSeconds(frameNumber / fps),
                            Position  = new Rect
                            {
                                X      = (float)rect.X / frame.Width,
                                Y      = (float)rect.Y / frame.Height,
                                Width  = (float)rect.Width / frame.Width,
                                Height = (float)rect.Height / frame.Height
                            }
                        };

                        location.Estimated = location.Position.X + location.Position.Width / 2;

                        gtLocations.Add(location);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"    Unable to process capture: {take.Name} - {e.Message}:{e.StackTrace}");
                }

                frameNumber++;
            };

            while (frameNumber < frameCount)
            {
                CvInvoke.WaitKey(100);
            }

            camera.Stop();
            camera.Dispose();

            return(gtLocations);
        }