예제 #1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            OnButtonClick += delegate
            {
                long time;
                using (Image <Bgr, Byte> image = PickImage("pedestrian.png"))
                {
                    if (image == null)
                    {
                        return;
                    }
                    Rectangle[] pedestrians = FindPedestrian.Find(image.Mat, false, true, out time);

                    SetMessage(String.Format("Detection completed in {0} milliseconds.", time));
                    foreach (Rectangle rect in pedestrians)
                    {
                        image.Draw(rect, new Bgr(System.Drawing.Color.Red), 2);
                    }

                    SetImageBitmap(image.ToBitmap());
                }
            };
        }
        public PeopleDetect Post(string url)
        {
            PeopleDetect results = new PeopleDetect();

            results.counted = 0;
            var webClient = new WebClient();

            //download image from url
            byte[] imageBytes = webClient.DownloadData(url);
            Mat    image      = new Mat();

            CvInvoke.Imdecode(imageBytes, LoadImageType.Color, image);

            bool tryUseCuda = false;
            long processingTime;

            //find the pedestrians in the image and return rectangles and count
            results.rectCoord = FindPedestrian.Find(image, tryUseCuda, out processingTime);
            foreach (Rectangle rect in results.rectCoord)
            {
                results.counted = results.counted + 1;
            }

            return(results);
        }
 public override void ViewDidLoad()
 {
     base.ViewDidLoad();
     ButtonText     = "Detect Pedestrian";
     OnButtonClick += delegate
     {
         long processingTime;
         using (Image <Bgr, byte> image = new Image <Bgr, byte>("pedestrian.png"))
         {
             Rectangle[] pedestrians = FindPedestrian.Find(
                 image.Mat, false, false,
                 out processingTime
                 );
             foreach (Rectangle rect in pedestrians)
             {
                 image.Draw(rect, new Bgr(Color.Red), 1);
             }
             Size frameSize = FrameSize;
             using (Image <Bgr, Byte> resized = image.Resize(frameSize.Width, frameSize.Height, Emgu.CV.CvEnum.Inter.Nearest, true))
             {
                 MessageText = String.Format(
                     "Detection Time: {0} milliseconds.",
                     processingTime
                     );
                 SetImage(resized);
             }
         }
     };
 }
예제 #4
0
        public void viewing()
        {
            Image <Bgr, byte> image1 = new Image <Bgr, byte>("p1.jpg");

            this.WindowState   = FormWindowState.Maximized;
            pictureBox1.Height = this.Height;
            pictureBox1.Width  = this.Width;
            image1             = image1.Resize(this.Width, this.Height, Emgu.CV.CvEnum.Inter.Linear);
            Mat  image = image1.Mat;
            long processingTime;

            Rectangle[] results;

            if (CudaInvoke.HasCuda)
            {
                using (GpuMat gpuMat = new GpuMat(image))
                    results = FindPedestrian.Find(gpuMat, out processingTime);
            }
            else
            {
                using (UMat uImage = image.ToUMat(AccessType.ReadWrite))
                    results = FindPedestrian.Find(uImage, out processingTime);
            }

            foreach (Rectangle rect in results)
            {
                CvInvoke.Rectangle(image, rect, new Bgr(Color.Red).MCvScalar);
            }
            Image <Bgr, Byte> img = image.ToImage <Bgr, Byte>();

            pictureBox1.Image = img.ToBitmap();
        }
예제 #5
0
        public PedestrianDetectionPage()
            : base()
        {
            var button = this.GetButton();

            button.Text = "Perform Pedestrian Detection";

            OnImagesLoaded += async(sender, image) =>
            {
                if (image == null || image [0] == null)
                {
                    return;
                }
                GetLabel().Text = "please wait...";
                SetImage(null);

                Task <Tuple <Mat, long> > t = new Task <Tuple <Mat, long> >(
                    () =>
                {
                    long time;
                    Rectangle [] pedestrians;
                    if (image[0].NumberOfChannels == 4)
                    {
                        //if the png file is loaded with alpha channel
                        using (Mat bgr = new Mat())
                        {
                            CvInvoke.CvtColor(image [0], bgr, ColorConversion.Bgra2Bgr);
                            pedestrians = FindPedestrian.Find(bgr, out time);
                        }
                    }
                    else
                    {
                        pedestrians = FindPedestrian.Find(image[0], out time);
                    }

                    foreach (Rectangle rect in pedestrians)
                    {
                        CvInvoke.Rectangle(image[0], rect, new MCvScalar(0, 0, 255), 2);
                    }

                    return(new Tuple <Mat, long>(image[0], time));
                });

                t.Start();
                var    result        = await t;
                String computeDevice = CvInvoke.UseOpenCL ? "OpenCL: " + Ocl.Device.Default.Name : "CPU";
                GetLabel().Text = String.Format("Detection completed with {1} in {0} milliseconds.", t.Result.Item2,
                                                computeDevice);
                SetImage(t.Result.Item1);
            };

            button.Clicked += OnButtonClicked;
        }
        public PeopleDetect UploadFile()
        {
            if (HttpContext.Current.Request.Files.AllKeys.Any())
            {
                // Get the uploaded image from the Files collection
                var httpPostedFile = HttpContext.Current.Request.Files[0];

                if (httpPostedFile != null)
                {
                    //get the file path and save the image
                    var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);
                    httpPostedFile.SaveAs(fileSavePath);

                    PeopleDetect results = new PeopleDetect();
                    results.counted = 0;

                    Mat image = new Mat(fileSavePath, LoadImageType.Color);

                    bool tryUseCuda = false;
                    long processingTime;

                    //find the pedestrians in the image and return rectangles and count
                    results.rectCoord = FindPedestrian.Find(image, tryUseCuda, out processingTime);
                    foreach (Rectangle rect in results.rectCoord)
                    {
                        results.counted = results.counted + 1;
                    }

                    File.Delete(fileSavePath);

                    return(results);
                }
                else
                {
                    PeopleDetect results = new PeopleDetect();
                    results.counted = 0;
                    return(results);
                }
            }
            else
            {
                PeopleDetect results = new PeopleDetect();
                results.counted = 0;
                return(results);
            }
        }
예제 #7
0
        public PedestrianDetectionPage()
            : base()
        {
            var button = this.GetButton();

            button.Text = "Perform Pedestrian Detection";

            OnImageLoaded += async(sender, image) =>
            {
                GetLabel().Text = "please wait...";
                SetImage(null);

                Task <Tuple <Mat, long> > t = new Task <Tuple <Mat, long> >(
                    () =>
                {
                    long time;


                    if (image == null)
                    {
                        return(new Tuple <Mat, long>(null, 0));
                    }
                    Rectangle[] pedestrians = FindPedestrian.Find(image, false, out time);

                    foreach (Rectangle rect in pedestrians)
                    {
                        CvInvoke.Rectangle(image, rect, new MCvScalar(0, 0, 255), 2);
                    }

                    return(new Tuple <Mat, long>(image, time));
                });

                t.Start();
                var    result        = await t;
                String computeDevice = CvInvoke.UseOpenCL ? "OpenCL: " + OclDevice.Default.Name : "CPU";
                GetLabel().Text = String.Format("Detection completed with {1} in {0} milliseconds.", t.Result.Item2,
                                                computeDevice);
                SetImage(t.Result.Item1);
            };

            button.Clicked += OnButtonClicked;
        }
예제 #8
0
        public void ProcessImage(Mat m)
        {
            Rectangle[] pedestrians;
            if (m.NumberOfChannels == 4)
            {
                //if the png file is loaded with alpha channel
                using (Mat bgr = new Mat())
                {
                    CvInvoke.CvtColor(m, bgr, ColorConversion.Bgra2Bgr);
                    pedestrians = FindPedestrian.Find(bgr);
                }
            }
            else
            {
                pedestrians = FindPedestrian.Find(m);
            }

            foreach (Rectangle rect in pedestrians)
            {
                CvInvoke.Rectangle(m, rect, new MCvScalar(0, 0, 255), 2);
            }
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            OnImagePicked += (sender, image) =>
            {
                if (image == null)
                {
                    return;
                }
                AppPreference appPreference = new AppPreference();
                CvInvoke.UseOpenCL = appPreference.UseOpenCL;
                String oclDeviceName = appPreference.OpenClDeviceName;
                if (!String.IsNullOrEmpty(oclDeviceName) && CvInvoke.UseOpenCL)
                {
                    CvInvoke.OclSetDefaultDevice(oclDeviceName);
                }

                long        time;
                Rectangle[] pedestrians;
                using (UMat umat = image.GetUMat(AccessType.ReadWrite))
                    pedestrians = FindPedestrian.Find(image, out time);

                String computeDevice = CvInvoke.UseOpenCL ? "OpenCL: " + Emgu.CV.Ocl.Device.Default.Name : "CPU";
                SetMessage(String.Format("Detection completed with {1} in {0} milliseconds.", time, computeDevice));
                foreach (Rectangle rect in pedestrians)
                {
                    CvInvoke.Rectangle(image, rect, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
                }

                SetImageBitmap(image.ToBitmap());
                image.Dispose();
            };

            OnButtonClick += (sender, args) =>
            {
                PickImage("pedestrian.png");
            };
        }
예제 #10
0
        private void ProcessFrame(object sender, EventArgs arg)
        {
            Mat frame = _capture.QueryFrame();

            pictureBox1.Height = frame.Height;
            pictureBox1.Width  = frame.Width;
            if (frame != null)
            {
                //image_array.Add(frame.Copy());
                long        processingTime;
                Rectangle[] results;

                if (CudaInvoke.HasCuda)
                {
                    using (GpuMat gpuMat = new GpuMat(frame))
                        results = FindPedestrian.Find(gpuMat, out processingTime);
                }
                else
                {
                    using (UMat uImage = frame.ToUMat(AccessType.ReadWrite))
                        results = FindPedestrian.Find(uImage, out processingTime);
                }

                foreach (Rectangle rect in results)
                {
                    CvInvoke.Rectangle(frame, rect, new Bgr(Color.Red).MCvScalar);
                }
                Image <Bgr, Byte> img = frame.ToImage <Bgr, Byte>();
                img.Resize(384, 288, Emgu.CV.CvEnum.Inter.Linear);
                Image <Gray, Byte> grayFrame         = img.Convert <Gray, Byte>();
                Image <Gray, Byte> smallGrayFrame    = grayFrame.PyrDown();
                Image <Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();
                pictureBox1.Image = smoothedGrayFrame.ToBitmap();
            }
            else
            {
                Application.Idle -= ProcessFrame;// treat as end of file
            }
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            ButtonText     = "Detect Pedestrian";
            OnButtonClick += delegate
            {
                //long processingTime;
                using (Mat image = CvInvoke.Imread("pedestrian.png", ImreadModes.Color))
                    using (HOGDescriptor hog = new HOGDescriptor())
                    {
                        hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());
                        Stopwatch   watch       = Stopwatch.StartNew();
                        Rectangle[] pedestrians = FindPedestrian.Find(image, hog);
                        watch.Stop();


                        foreach (Rectangle rect in pedestrians)
                        {
                            CvInvoke.Rectangle(
                                image,
                                rect,
                                new MCvScalar(0, 0, 255),
                                1);
                        }
                        Size frameSize = FrameSize;

                        using (Mat resized = new Mat())
                        {
                            CvInvoke.ResizeForFrame(image, resized, frameSize);
                            MessageText = String.Format(
                                "Detection Time: {0} milliseconds.",
                                watch.ElapsedMilliseconds
                                );
                            SetImage(resized);
                        }
                    }
            };
        }
예제 #12
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            ButtonText     = "Detect Pedestrian";
            OnButtonClick += delegate
            {
                long processingTime;
                using (Mat image = CvInvoke.Imread("pedestrian.png", ImreadModes.Color))
                {
                    Rectangle[] pedestrians = FindPedestrian.Find(
                        image,
                        out processingTime
                        );

                    foreach (Rectangle rect in pedestrians)
                    {
                        CvInvoke.Rectangle(
                            image,
                            rect,
                            new MCvScalar(0, 0, 255),
                            1);
                    }
                    Size frameSize = FrameSize;

                    using (Mat resized = new Mat())
                    {
                        CvInvoke.ResizeForFrame(image, resized, frameSize);
                        MessageText = String.Format(
                            "Detection Time: {0} milliseconds.",
                            processingTime
                            );
                        SetImage(resized);
                    }
                }
            };
        }
예제 #13
0
        private void ProcessFrame(object sender, EventArgs e)
        {
            List <Rectangle> rt = new List <Rectangle>();
            double           overallAngle = default(double), overallMotionPixelCount = default(double);
            Mat image = new Mat();

            _capture.Retrieve(image);

            if (fpsc++ >= allfpsc)
            {
                fpsc = 0;

                if (_forgroundDetector == null)
                {
                    _forgroundDetector = new BackgroundSubtractorMOG2();
                }

                _forgroundDetector.Apply(image, _forgroundMask);

                //update the motion history
                _motionHistory.Update(_forgroundMask);

                #region get a copy of the motion mask and enhance its color
                double[] minValues, maxValues;
                Point[]  minLoc, maxLoc;
                _motionHistory.Mask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc);
                Mat motionMask = new Mat();
                using (ScalarArray sa = new ScalarArray(255.0 / maxValues[0]))
                    CvInvoke.Multiply(_motionHistory.Mask, sa, motionMask, 1, DepthType.Cv8U);
                //Image<Gray, Byte> motionMask = _motionHistory.Mask.Mul(255.0 / maxValues[0]);
                #endregion

                //create the motion image
                Mat motionImage = new Mat(motionMask.Size.Height, motionMask.Size.Width, DepthType.Cv8U, 3);
                motionImage.SetTo(new MCvScalar(0));
                //display the motion pixels in blue (first channel)
                //motionImage[0] = motionMask;
                CvInvoke.InsertChannel(motionMask, motionImage, 0);

                //Threshold to define a motion area, reduce the value to detect smaller motion
                double minArea = 100;

                //storage.Clear(); //clear the storage
                Rectangle[] rects;
                using (VectorOfRect boundingRect = new VectorOfRect())
                {
                    _motionHistory.GetMotionComponents(_segMask, boundingRect);
                    rects = boundingRect.ToArray();
                }

                //iterate through each of the motion component
                foreach (Rectangle comp in rects)
                {
                    if (Zone.Contains(comp))
                    {
                        rt.Add(comp);
                    }

                    int area = comp.Width * comp.Height;
                    //reject the components that have small area;
                    if (area < minArea)
                    {
                        continue;
                    }

                    // find the angle and motion pixel count of the specific area
                    double angle, motionPixelCount;
                    _motionHistory.MotionInfo(_forgroundMask, comp, out angle, out motionPixelCount);

                    //reject the area that contains too few motion
                    if (motionPixelCount < area * 0.05)
                    {
                        continue;
                    }

                    //Draw each individual motion in red
                    //DrawMotion(motionImage, comp, angle, new Bgr(Color.Blue));
                }

                //rects = rt.ToArray();

                // find and draw the overall motion angle


                if (Zone.Width <= 0 & Zone.Height <= 0 & Zone.X < 0 & Zone.Y < 0)
                {
                    Zone = new Rectangle(0, 0, 10, 10);
                }

                _motionHistory.MotionInfo(_forgroundMask, Zone /*new Rectangle(Point.Empty, motionMask.Size)*/, out overallAngle, out overallMotionPixelCount);
                //DrawMotion(motionImage, new Rectangle(Point.Empty, motionMask.Size), overallAngle, new Bgr(Color.Green));

                Image <Bgr, Byte> grayImage = image.ToImage <Bgr, Byte>();

                if (DetectPed)
                {
                    long        processingTime;
                    Rectangle[] results;

                    if (CudaInvoke.HasCuda)
                    {
                        using (GpuMat gpuMat = new GpuMat(image))
                            results = FindPedestrian.Find(gpuMat, out processingTime);
                    }
                    else
                    {
                        using (UMat uImage = image.GetUMat(AccessType.ReadWrite))
                            results = FindPedestrian.Find(uImage, out processingTime);
                    }

                    foreach (Rectangle rect in results)
                    {
                        CvInvoke.Rectangle(image, rect, new Bgr(Color.Red).MCvScalar);
                    }
                }
            }

            if (maxfpsc++ > skipfpsc)
            {
                OnMD?.Invoke((long)overallMotionPixelCount, image, rt.Count);
            }
            else
            {
                OnMD?.Invoke(default(long), image, default(int));
            }
        }
예제 #14
0
        void matchingVideoTimer_Tick(object sender, EventArgs e)
        {
            //如果有影片
            if (videoCapture != null)
            {
                if (isPlay)
                {
                    lock (this)
                    {
                        //如果Frame的index沒有差過影片的最大index
                        if (oneSecFrameIndex < matchingVideoTotalFrame && videoCapture.GetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES) < matchingVideoTotalFrame)
                        {
                            videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES, oneSecFrameIndex);
                            oneSecFrameIndex += 15;
                            //顯示
                            senceImage = videoCapture.QueryFrame();
                            senceImage = senceImage.Resize(640, 480, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
                            if (surfOpenCheckBox.Checked)
                            {
                                pedesrianWorker = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(MatchImage));
                                pedesrianWorker.Start(senceImage);
                                //Match
                                //MatchImage(senceImage, ReadSURFFeature(dir.Parent.Parent.FullName + "\\SurfFeatureData\\000-0.xml"));
                            }
                            if (pedestrianCheckBox.Checked)
                            {
                                //行人偵測
                                long processingTime;
                                //Rectangle roi = new Rectangle(  (senceImage.Width / 3) ,0, (senceImage.Width / 3),senceImage.Height);
                                ////切割區塊,只抓路中央
                                //Image<Bgr,byte> roadROI = senceImage.Copy();
                                //roadROI.ROI = roi;
                                //Image<Bgr, Byte> result = FindPedestrian.Find(roadROI, out processingTime);

                                ////抓人臉
                                //Image<Gray, byte> graySence = senceImage.Convert<Gray, byte>();
                                //var faces = graySence.DetectHaarCascade(
                                //               haar, 1.4, 4,
                                //               HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                //               new Size(senceImage.Width / 8, senceImage.Height / 8)
                                //               )[0];

                                //Image<Bgr, Byte> result = senceImage.Copy();
                                //foreach (var face in faces)
                                //{
                                //    result.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
                                //}

                                Image <Bgr, Byte> result = FindPedestrian.Find(senceImage, out processingTime);
                                Console.WriteLine("Pedesrian process time = " + processingTime + "ms\n");
                                if (result != null)
                                {
                                    pedestrianViewer.Image = result;
                                    pedestrianViewer.Show();
                                }
                            }
                            mathcingVideoPictureBox.Image = senceImage.ToBitmap();
                        }
                    }
                }
                else if (isSuspend)
                {
                    //Nothing to do..
                }
                else if (isStop)
                {
                    //關閉,回到一開始畫面
                    videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_AVI_RATIO, 0);
                }
            }
        }