예제 #1
0
        //int i = 0;
        void ProcessFrame(object sender, EventArgs e)
        {
            if (btn_pause.Checked)
            {
                return;
            }


            try
            {
                fps++;

                #region Get Frame
                if (videoDevice == null)
                {
                    return;
                }
                //videoDevice.QueryFrame();
                for (int i = 0; i < skipCams; i++)
                {
                    cMan.getRecordedFrame(currentCam);
                }
                //videoDevice.QuerySmallFrame();

                Mat frame = cMan.getRecordedFrame(currentCam);



                //Mat frame = videoDevice.QuerySmallFrame();
                //Mat frame = videoDevice.QueryFrame();

                if (frame == null || frame.IsEmpty)
                {
                    GenerateVideo();
                    return;
                }


                frame = frame.ToImage <Bgr, byte>().Resize(640, 480, Inter.Cubic).Mat;

                //pbView.Image = frame.Bitmap;
                //return;

                #endregion

                detectedBodies = body.GetAllPersonBodies(frame);

                if (detectedBodies != null && detectedBodies.Length != 0)
                {
                    #region Variable initialization
                    List <Rectangle> brec    = new List <Rectangle>();
                    List <double>    distHog = new List <double>();
                    List <double>    distHs  = new List <double>();
                    List <double>    distRgb = new List <double>();
                    List <bool>      mtch    = new List <bool>();


                    int index = 0;
                    ((BodyFeatureView)flpsuspects.Controls[0]).ClearControl();
                    ((BodyFeatureView)flpsuspects.Controls[1]).ClearControl();
                    #endregion

                    foreach (Rectangle detectedBody in detectedBodies)
                    {
                        #region Body Crop and Resize
                        // Cropping image
                        var img = frame.ToImage <Bgr, byte>();
                        img.ROI = detectedBody;
                        img     = img.Copy();

                        // Reduce rectangle size to remove background
                        Size imgSize = img.Size;
                        img.ROI = new Rectangle((imgSize.Width / 4), (imgSize.Height / 8), (imgSize.Width / 2), (int)(imgSize.Height / 1.5)); img = img.Copy();
                        img     = img.Copy();

                        img = img.Resize(64, 128, Inter.Cubic);
                        #endregion

                        #region Equaulize the body

                        // 1
                        //img._EqualizeHist();

                        // 2 YCrCb method
                        Image <Ycc, byte> yccImage = img.Convert <Ycc, byte>();
                        //var channels = yccImage.Split();
                        //channels[0]._EqualizeHist();
                        //yccImage[0] = channels[0];
                        yccImage[0]._EqualizeHist();
                        img = yccImage.Convert <Bgr, byte>();
                        //CvInvoke.Merge(channels, yccImage);


                        #endregion


                        if (rbIndentification.Checked)
                        {
                            #region Identification
                            // non-tracking mode


                            bfvTarget.ClearControl();
                            bfvTarget.RefreshImage(img);

                            tagetbodyImage = (img);


                            pbView.Image = DrawController.DrawRectangle(detectedBodies, frame, Color.Yellow, 2).ToBitmap();
                            #endregion
                        }
                        else
                        {
                            #region Re-identification

                            if (tagetbodyImage != null)
                            {
                                // Get its bodyfeatures
                                body.AddTargetBody(body.GetAllBodyFeatures(tagetbodyImage));
                                tagetbodyImage = null;
                            }


                            // tracking mode
                            // Can use this to display all person on side
                            // pbBody.Image = img.ToBitmap();

                            if (index < 2)
                            {
                                ((BodyFeatureView)flpsuspects.Controls[index]).RefreshImage(img);
                            }


                            // Extract Features after removing background
                            body.suspectBody = body.GetAllBodyFeatures(img);


                            // Just for display OTHERWISE use direct method
                            double distancHog  = body.GetHogDistance();
                            double distanceHs  = body.GetHSDistance();
                            double distanceRgb = body.GetRGBDistance();

                            if (index < 2)
                            {
                                ((BodyFeatureView)flpsuspects.Controls[index++]).RefreshText(distancHog, distanceRgb, distanceHs);
                            }

                            brec.Add(detectedBody);
                            distHog.Add(distancHog);
                            distHs.Add(distanceHs);
                            distRgb.Add(distanceRgb);

                            //bool isMatched = body.IsBodySame(distancHog, distanceHs, distanceRgb);
                            bool isMatched = body.IsBodySame();

                            mtch.Add(isMatched);



                            #endregion
                        }
                    }


                    Rectangle mostMatchedBodyLocation = Rectangle.Empty;
                    bool      isSuspectFound          = false;
                    Rectangle lastSuspectPosition     = new Rectangle();
                    if (!rbIndentification.Checked)
                    {
                        #region Find most matched body and its location and Verification
                        int suspectIndexinMtch = -1;
                        body.FindMostMatchedBody(distHog, distHs, distRgb, ref mtch);
                        for (int i = 0; i < mtch.Count; i++)
                        {
                            if (mtch[i])
                            {
                                mostMatchedBodyLocation = detectedBodies[i];
                                //When body is found using DC then Set the person verification (i.e. set the body
                                // to track if it is lost in next frame)
                                body.SetPersonVerification(frame, mostMatchedBodyLocation, detectedBodies);
                                suspectIndexinMtch = i;

                                if (isFirstTimeInNewNeighbour)
                                {
                                    // Add suspect image everytime camera switches
                                    Debug.AddTrackText("New Body Image added");
                                    var temp = frame.ToImage <Bgr, byte>();
                                    temp.ROI = mostMatchedBodyLocation;

                                    body.AddTargetBody(body.GetAllBodyFeatures(temp.Copy()));

                                    isFirstTimeInNewNeighbour = false;
                                }
                            }
                        }


                        #endregion

                        #region Track timeline
                        if (track == null)
                        {
                            track         = new Track(ref timeLine, new TimeInterval(DateTime.Now, DateTime.Now.AddMinutes(1)));
                            lblStart.Text = DateTime.Now.Hour + " : " + DateTime.Now.Minute + " : " + DateTime.Now.Second;
                        }
                        #endregion

                        body.VerifyPerson(frame, mostMatchedBodyLocation, detectedBodies);


                        for (int i = 0; i < brec.Count; i++)
                        {
                            // suspectIndex is the index of suspect in mtch, brec
                            if (body.IsPersonVerified() && (/*i == suspectIndexinMtch ||*/ i == body.GetVerificationPersonIndex()))
                            {
                                #region Suspect Verified
                                if (body.IsPersonVerified() && !mtch[i])
                                {
                                    // temp if
                                    int a = 10;
                                }

                                // Calculating for next frame (if suspect lost in next frame)
                                PointF[] points = body.CalculateOF(frame, brec[i], brec.ToArray(), true);

                                #region Display found Suspect Body
                                //DrawController.DrawCircles(frame, points);
                                //pbView.Image = frame.Bitmap;
                                //return;
                                lastSuspectPosition = brec[i];
                                isSuspectFound      = true;
                                //double colorRatio = Math.Min(((double)body.currentVerificationNumber) / BodyController.numberOfVerificationFrames, 1);
                                double colorRatio = body.GetVerficationConfidence();

                                //Debug.AddTrackText(String.Format("{0}% OF>({1}) DC>({1}))",body.GetVerficationConfidence()*100, body.IsPersonVerified()),mtch[i]);

                                if (body.IsPersonVerified())
                                {
                                    frame = DrawController.DrawRectangle(brec[i], (Convert.ToInt32(distHog[i])) + " " + (Convert.ToInt32(distHs[i])) + " " + ((distRgb[i])), frame.ToImage <Bgr, byte>(), Color.Green).Mat;
                                }
                                else
                                {
                                    frame = DrawController.DrawRectangle(brec[i], "Verifying " + body.GetVerficationConfidence(), frame.ToImage <Bgr, byte>(), Color.FromArgb(0, (int)((colorRatio) * 255), (int)((1 - colorRatio) * 255))).Mat;
                                }

                                //histogramBox1.ClearHistogram();
                                //histogramBox1.AddHistogram("Red",Color.Red, body.suspectBody.rgb.Item1,256, new float[] { 0f, 255f });
                                //histogramBox1.Refresh();
                                #endregion
                                #endregion
                            }
                            else if (body.IsUnderVerification() && (/*i == suspectIndexinMtch ||*/ i == body.GetVerificationPersonIndex()))
                            {
                                #region Not Verified but matched (for less number of frames than threshold)
                                // if person is being verified

                                double colorRatio = body.GetVerficationConfidence();

                                frame = DrawController.DrawRectangle(brec[i], "Verifying " + body.GetVerficationConfidence(), frame.ToImage <Bgr, byte>(), Color.FromArgb(0, (int)((colorRatio) * 255), (int)((1 - colorRatio) * 255))).Mat;
                                #endregion
                            }
                            else
                            {
                                #region Display bodies which is not target
                                frame = DrawController.DrawRectangle(brec[i], (Convert.ToInt32(distHog[i])) + " " + (Convert.ToInt32(distHs[i])) + " " + ((distRgb[i])), frame.ToImage <Bgr, byte>(), Color.Red).Mat;
                                //frame = Draw.DrawRectangle(brec[i], ((distHog[i])) + " " + ((distHs[i])) + " " + ((distRgb[i])), frame.ToImage<Bgr, byte>(), Color.Red).Mat;
                                #endregion
                            }
                        }
                        pbView.Image = frame.Bitmap;
                    }



                    #region If suspect not found
                    if (track != null && !isSuspectFound)
                    {
                        if (!isLive)
                        {
                            #region Draw OF

                            PointF[] points = body.CalculateOF(frame, lastSuspectPosition, brec.ToArray());

                            //Rectangle ofRect = BodyTracking.FindOuterRectangle(points);
                            //frame = DrawController.DrawRectangle(ofRect, "Using OF", frame.ToImage<Bgr, byte>(), Color.Brown).Mat;

                            //pbView.Image = frame.Bitmap;

                            #endregion

                            #region Draw person suggested by OF

                            Rectangle ofApproxBody = body.GetOFApproxBody();
                            frame = DrawController.DrawRectangle(ofApproxBody, frame.ToImage <Bgr, byte>(), Color.Purple).Mat;

                            #endregion

                            pbView.Image = frame.Bitmap; //DrawController.DrawCircles(frame, points).Bitmap;

                            //pbView.Image = frame.Bitmap;
                        }
                    }
                    #endregion

                    #region manage timeline Track
                    if (track != null)
                    {
                        if (isSuspectFound)
                        {
                            track.CheckCamera(currentCam);

                            lblStart.Text = track.TimeToString(track.camtime.startTime);
                            lblEnd.Text   = track.TimeToString(track.camtime.endTime);
                        }
                        else
                        {
                            if (track.CheckCamera(null))
                            {
                                SuspectLeftFOV();
                            }
                            else
                            {
                                lblStart.Text = track.TimeToString(track.camtime.startTime);
                                lblEnd.Text   = track.TimeToString(track.camtime.endTime);
                            }
                        }
                    }
                    #endregion

                    #region Temporary
                    //var image = frame.ToImage<Bgr, byte>();
                    //image.ROI = detectedBodies[0];
                    //image = image.Copy();
                    //image = image.Resize(64, 128, Inter.Cubic);
                    //Point centre = new Point(image.Width / 2, image.Height / 2);
                    ////double borderSize = 0.25;

                    //Bgr white = new Bgr(Color.White);
                    //// apply weighted filter
                    //for (int r = 0; r < image.Height; r++)
                    //{
                    //    //if (r > (image.Height * borderSize) && r < (image.Height - (image.Height * borderSize)))
                    //    //{
                    //    //    continue;
                    //    //}
                    //    for (int c = 0; c < image.Width; c++)
                    //    {
                    //        //if (c>(image.Width * borderSize) && c<(image.Width-(image.Width*borderSize))
                    //        //    && (r > (image.Height * borderSize) && r < (image.Height - (image.Height * borderSize))))
                    //        //    continue;

                    //        Bgr org = image[r, c];
                    //        double dist = Math.Abs((r - centre.Y) * 1.56) + Math.Abs((c - centre.X) * 3.1);
                    //        Bgr newImg = new Bgr(dist * 1.27 /*+ org.Blue*/, dist * 1.27 /*+ org.Green*/, dist * 1.27/* + org.Red*/);
                    //        if (dist < (100))
                    //            continue;
                    //        image[r, c] = new Bgr(Color.White);
                    //    }
                    //}

                    //pbView.Image = image.Bitmap;

                    // if a body detected
                    //pbView.Image = Draw.DrawRectangle(detectedBodies, frame, Color.Green, 2).ToBitmap();
                    #endregion

                    #region OD Pass

                    //if (body.IsInOD_Door_outSide(mostMatchedBodyLocation, currentCam))
                    //{
                    //    odNeighbour = currentCam.OD_Neighbour;
                    //    lastSeenOnOD_Door = true;
                    //}
                    //else
                    //    lastSeenOnOD_Door = false;

                    #endregion
                }
                else
                {
                    if (track != null && track.CheckCamera(null))
                    {
                        SuspectLeftFOV();
                    }
                    //else
                    //    lblEnd.Text = DateTime.Now.Hour + " " + DateTime.Now.Minute + " " + DateTime.Now.Second;


                    pbView.Image = frame.Bitmap;
                }
            }
            catch (AccessViolationException ex)
            {
                MessageBox.Show(ex.Message);
            }



            // next here
            //try
            //{
            //    using (GFTTDetector detector = new GFTTDetector(1000, 0.01, 1, 3, false, 0.04))
            //    {
            //        Mat img = frame.Clone();

            //        var keypoints = detector.Detect(img);

            //        foreach (MKeyPoint p in keypoints)
            //        {
            //            CvInvoke.Circle(img, Point.Round(p.Point), 3, new Bgr(255, 0, 0).MCvScalar, 1);
            //        }


            //        pbView.Image = img.Bitmap;
            //    }

            //}
            //catch (Exception ex)
            //{ MessageBox.Show(ex.Message); }
        }
예제 #2
0
        void ProcessFrame(object sender, EventArgs e)
        {
            //FindAverageFrameTime();

            fps++;
            if (videoDevice == null)
            {
                return;
            }
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    videoDevice.QueryFrame();
                }

                Mat currentFrame = videoDevice.QueryFrame();
                //Emgu.CV.CvInvoke.Resize(currentFrame, currentFrame, new Size(), 0.5, 0.5,Emgu.CV.CvEnum.Inter.Cubic);
                //CvInvoke.GaussianBlur(currentFrame, currentFrame,new Size(5,5),2);


                int         type  = 0;
                Rectangle[] faces = face.FindAllFaces(currentFrame, ref type);


                if (faces.Length != 0)
                {
                    var image     = currentFrame.ToImage <Gray, Byte>();
                    var colorFace = currentFrame.ToImage <Bgr, byte>();
                    image.ROI     = faces[0];
                    colorFace.ROI = faces[0];
                    Image <Gray, Byte> faceImage = image.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);

                    double distance = 0;
                    int    personId = face.MatchedFace(faceImage, ref distance);

                    //if (personId != -1)
                    //{


                    int guess = GuessFace(personId);
                    if (guess == -1)
                    {
                        // Unauthorized
                        pbView.Image = DrawController.DrawRectangle(faces, currentFrame, Color.Red, 2).ToBitmap();

                        for (int i = 0; i < unauthorized.Count; i++)
                        {
                            if (unauthorized[i].id == personId)
                            {
                                return;
                            }
                        }
                        unauthorized.Add(new PersonInfo(personId, "unknown", colorFace.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic).ToBitmap()));
                        MessageBox.Show("Unauthorized Person Detected", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (guess == -2)
                    {
                        pbView.Image = DrawController.DrawRectangle(faces[0], "Working...", currentFrame.ToImage <Bgr, byte>(), Color.Yellow, 2).ToBitmap();
                    }
                    else
                    {
                        // Authorized

                        string personName = face.GetPersonName(personId);
                        pbView.Image = DrawController.DrawRectangle(faces[0], personName, currentFrame.ToImage <Bgr, Byte>(), Color.Green, 2).ToBitmap();
                        //BeginInvoke((MethodInvoker)delegate { tbConsole.Text += "\n" + pr.Label.ToString()+" Detected"; });

                        for (int i = 0; i < authorized.Count; i++)
                        {
                            if (authorized[i].id == personId)
                            {
                                return;
                            }
                        }

                        authorized.Add(new PersonInfo(personId, personName, colorFace.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic).ToBitmap()));
                    }
                    //}
                    //else
                    //{
                    //    // Face Mismatched
                    //    pbView.Image = DrawController.DrawRectangle(faces, currentFrame, Color.Red, 2).ToBitmap();
                    //}

                    BeginInvoke((MethodInvoker) delegate { tbConsole.Text = distance + " " + personId.ToString(); });
                }
                else
                {
                    pbView.Image = currentFrame.Bitmap;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.InnerException);
            }
        }
예제 #3
0
        void ProcessFrame(object sender, EventArgs e)
        {
            try
            {
                fps++;
                if (videoDevice == null)
                {
                    return;
                }
                for (int i = 0; i < 5; i++)
                {
                    videoDevice.QueryFrame();
                }
                Mat currentFrame = videoDevice.QueryFrame();

                //Mat tempFrame;
                //while(true)
                //{
                //    tempFrame = videoDevice.QueryFrame();
                //    if (tempFrame != null)
                //        currentFrame = tempFrame;
                //    else
                //        break;
                //}


                //Emgu.CV.CvInvoke.Resize(currentFrame, currentFrame, new Size(), 0.5, 0.5,Emgu.CV.CvEnum.Inter.Cubic);
                //CvInvoke.GaussianBlur(currentFrame, currentFrame,new Size(5,5),2);

                int         type  = 0;
                Rectangle[] faces = face.FindAllFaces(currentFrame, ref type);

                if (isExpertMode)
                {
                    BeginInvoke((MethodInvoker) delegate { tbConsole.Text += "\n" + type; });
                }


                pbView.Image = DrawController.DrawRectangle(faces, currentFrame, Color.Green, 1).ToBitmap();



                if (faces.Length != 0)
                {
                    // Standardize the Value (hsV)
                    // Increase contrast
                    //var enhanced = currentFrame.ToImage<Hsv, byte>().Copy();
                    //enhanced._EqualizeHist();
                    //enhanced._GammaCorrect(1.5d);
                    //for (int i = 0; i < enhanced.Size.Height; i++)
                    //    for (int j = 0; j < enhanced.Size.Width; j++)
                    //        enhanced.Data[i, j, 1] = 250;


                    var image = currentFrame.ToImage <Gray, byte>();

                    //var image = currentFrame.ToImage<Gray, byte>();

                    image.ROI = faces[0];
                    var faceImage = image.Copy().Resize(100, 100, Emgu.CV.CvEnum.Inter.Cubic);


                    if (registeredImages.Count == 0)
                    {
                        registeredImages.Add(faceImage.Convert <Gray, Byte>());
                        registeredImages.Add(faceImage.Convert <Gray, Byte>());
                        PictureBox pb = new PictureBox();
                        pb.Size     = new Size(50, 50);
                        pb.Image    = faceImage.ToBitmap();
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        flpFaces.Controls.Add(pb);
                    }
                    else
                    {
                        bool isMatched = face.IsFaceMatched(faceImage, registeredImages.ToArray());
                        pbFace.Image = faceImage.ToBitmap();

                        //flpFaces.Controls.Clear();
                        //for(int i=1;i< registeredImages.Count;i++)
                        //{
                        //    PictureBox pb = new PictureBox();
                        //    pb.Size = new Size(50, 50);
                        //    pb.Image = registeredImages[i].ToBitmap();
                        //    pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        //    flpFaces.Controls.Add(pb);
                        //}

                        //if(isExpertMode)
                        //    BeginInvoke((MethodInvoker)delegate { tbConsole.Text = isMatched.ToString() ; });

                        if (!isMatched)
                        {
                            registeredImages.Add(faceImage.Convert <Gray, Byte>());
                            PictureBox pb = new PictureBox();
                            pb.Size     = new Size(50, 50);
                            pb.Image    = faceImage.ToBitmap();
                            pb.SizeMode = PictureBoxSizeMode.StretchImage;
                            flpFaces.Controls.Add(pb);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("--> " + ex.Message);
            }
        }