コード例 #1
0
        private void button3_Click_1(object sender, EventArgs e)
        {
            Stopwatch stopwatch = new Stopwatch();

            // Begin timing
            stopwatch.Start();
            eigenRecog.reloadData();
            stopwatch.Stop();

            // Write result
            MessageBox.Show("Time elapsed: " + stopwatch.Elapsed);
        }
コード例 #2
0
        private void ProcessFrame(object sender, EventArgs arg)
        {
            if (imageFrame != null)
            {
                Image <Gray, byte> greyImage = imageFrame.Copy().Convert <Gray, byte>();


                //greyImage._SmoothGaussian(3);
                //greyImage._EqualizeHist();
                stopWatch.Start();
                var faces = face.Detect(greyImage, 1.2, 6, HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(120, 120), new Size(200, 200));

                if (faces.Length == 0)
                {
                    var eyeObjects = eyeWithGlass.DetectMultiScale(greyImage, 1.2, 6, minEye, maxEye);
                    if (eyeObjects.Length == 2)
                    {
                        #region comment

                        /*Console.WriteLine("helper");
                         * if(eyeObjects[0].X > eyeObjects[1].X)
                         * {
                         *  var temp = eyeObjects[0];
                         *  eyeObjects[0] = eyeObjects[1];
                         *  eyeObjects[1] = temp;
                         * }
                         * int betweeneLength = eyeObjects[1].X - eyeObjects[0].X;
                         * int middleposition = eyeObjects[0].X + ((betweeneLength + eyeObjects[1].Width )/ 2);
                         * int forheadpeak = (int)(0.8 * betweeneLength);//
                         * int forheadpeakpeak = (int)(0.7 * betweeneLength);//
                         * int forheadbelowpeak = (int)(0.4 * betweeneLength);
                         * int foreheadpoint = (int)(0.6 * betweeneLength);
                         * int neareyebrowpoint = (int)(0.2 * betweeneLength);
                         * int lefteyebrowpoint = eyeObjects[0].X;//
                         * int righteyebrowpoint = eyeObjects[0].X + betweeneLength + eyeObjects[1].Width;//
                         * //int nosepoint =
                         * int xx = (int)((5.0 / 12.0) * betweeneLength);
                         * int xxx = (int)((1.5 / 8.0) * betweeneLength);
                         *
                         * int x1 = (int)((1.0 / 16.0) * betweeneLength);
                         *
                         *
                         * int round = 3;
                         * int around = round-2;
                         * double tempcolor = 0;
                         * for (int i = 0; i<round; i++)
                         * {
                         *  for (int j = 0; j < round; j++)
                         *  {
                         *      tempcolor += greyImage[middleposition - around + i, eyeObjects[0].Y + forheadpeak - around + j].Intensity;
                         *  }
                         * }
                         * Gray skincolor = new Gray(tempcolor/(round*round));
                         * Point[] p = new Point[7];
                         * p[0] = new Point(middleposition, eyeObjects[0].Y - forheadpeak);
                         *
                         * p[1] = new Point(eyeObjects[0].X + (eyeObjects[0].Width / 2), eyeObjects[0].Y - forheadpeakpeak);
                         * p[2] = new Point(eyeObjects[0].X - x1, eyeObjects[0].Y - forheadbelowpeak);
                         * p[3] = new Point(lefteyebrowpoint - xxx, eyeObjects[0].Y + (eyeObjects[0].Height/6));
                         *
                         * p[4] = new Point(righteyebrowpoint + xxx, eyeObjects[0].Y + (eyeObjects[0].Height /6));
                         * p[5] = new Point(righteyebrowpoint + x1, eyeObjects[0].Y - forheadbelowpeak);
                         * p[6] = new Point(eyeObjects[1].X + (eyeObjects[1].Width / 2), eyeObjects[0].Y - forheadpeakpeak);
                         *
                         * //imageFrame.Draw(new Rectangle(new Point(eyeObjects[0].X, eyeObjects[0].Y), new Size(betweeneLength + eyeObjects[1].Width, eyeObjects[0].Height)), new Bgr(Color.Aqua), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(middleposition,eyeObjects[0].Y+ foreheadpoint), 1), new Bgr(Color.Yellow), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(middleposition,eyeObjects[0].Y - forheadpeak), 1), new Bgr(Color.Yellow), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(middleposition, eyeObjects[0].Y - neareyebrowpoint), 1), new Bgr(Color.Gold), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(lefteyebrowpoint - xxx, eyeObjects[0].Y), 1), new Bgr(Color.AliceBlue), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(righteyebrowpoint + xxx, eyeObjects[0].Y), 1), new Bgr(Color.AliceBlue), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(lefteyebrowpoint, eyeObjects[0].Y - neareyebrowpoint), 1), new Bgr(Color.LimeGreen), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(righteyebrowpoint, eyeObjects[0].Y - neareyebrowpoint), 1), new Bgr(Color.LimeGreen), 2);
                         * //imageFrame.DrawPolyline(p,true, new Bgr(Color.Azure), 2);
                         * greyImage.FillConvexPoly(p, skincolor);
                         * //imageFrame.Draw(new CircleF(new PointF(eyeObjects[0].X - x1, eyeObjects[0].Y - forheadbelowpeak), 1), new Bgr(Color.LimeGreen), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(righteyebrowpoint + x1, eyeObjects[0].Y - forheadbelowpeak), 1), new Bgr(Color.LimeGreen), 2);
                         *
                         * //imageFrame.Draw(new CircleF(new PointF(eyeObjects[0].X + (eyeObjects[0].Width / 2), eyeObjects[0].Y - forheadpeakpeak), 1), new Bgr(Color.LimeGreen), 2);
                         * //imageFrame.Draw(new CircleF(new PointF(eyeObjects[1].X + (eyeObjects[1].Width / 2), eyeObjects[0].Y - forheadpeakpeak), 1), new Bgr(Color.LimeGreen), 2);
                         */
                        #endregion
                    }
                    else //not see eye in frame
                    {
                        name              = "Processing...";
                        learningTag       = true;
                        faceRectangle     = Rectangle.Empty;
                        realfaceRectangle = Rectangle.Empty;
                        recogNameResult.Clear();
                        recogDistanceResult.Clear();
                        Console.WriteLine("Clear");
                    }
                }
                else
                {
                    Parallel.ForEach(faces, facecount =>
                    {
                        try
                        {
                            facePosition      = new Point(facecount.rect.X, facecount.rect.Y);
                            faceRectangleSize = new Size(facecount.rect.Width, facecount.rect.Height);
                            faceRectangle     = new Rectangle(facePosition, faceRectangleSize);
                            greyImage.ROI     = faceRectangle;
                            var eyeObjects    = eyeWithGlass.DetectMultiScale(greyImage, 1.2, 6, minEye, maxEye);
                            greyImage.ROI     = System.Drawing.Rectangle.Empty;
                            if (eyeObjects.Length == 2)
                            {
                                Console.WriteLine("eye detected...");
                                if (eyeObjects[0].X > eyeObjects[1].X)
                                {
                                    var temp      = eyeObjects[0];
                                    eyeObjects[0] = eyeObjects[1];
                                    eyeObjects[1] = temp;
                                }

                                int betweeneLength    = eyeObjects[1].X - eyeObjects[0].X;
                                int lefteyebrowpoint  = eyeObjects[0].X;                                        //
                                int righteyebrowpoint = eyeObjects[0].X + betweeneLength + eyeObjects[1].Width; //
                                int xxx = (int)((1.5 / 8.0) * betweeneLength);
                                int neareyebrowpoint = (int)(0.2 * betweeneLength);
                                int faceheight       = (int)(2.3 * betweeneLength);

                                realFacePosition      = new Point(facePosition.X + lefteyebrowpoint - xxx, facePosition.Y + eyeObjects[0].Y - neareyebrowpoint);
                                realfaceRectangleSize = new Size((righteyebrowpoint + xxx) - (lefteyebrowpoint - xxx), faceheight);
                                realfaceRectangle     = new Rectangle(realFacePosition, realfaceRectangleSize);


                                greyImage.ROI = realfaceRectangle;
                                imageroi      = greyImage.Copy();
                                greyImage.ROI = new Rectangle();


                                if (eigenRecog.IsTrained)
                                {
                                    imageroi._EqualizeHist();
                                    imageroi = imageroi.Resize(ROIwidth, ROIheight, INTER.CV_INTER_LINEAR);

                                    //find the most relative face
                                    if (recogNameResult.Count == maxImageCount)
                                    {
                                        Console.WriteLine("Processing...");
                                        int max         = 0;
                                        string mostFace = "";
                                        foreach (string value in recogNameResult.Distinct())
                                        {
                                            Console.WriteLine("\"{0}\" occurs {1} time(s).\n", value, recogNameResult.Count(v => v == value));
                                            if (recogNameResult.Count(v => v == value) > max)
                                            {
                                                max      = recogNameResult.Count(v => v == value);
                                                mostFace = value;
                                            }
                                        }
                                        name = mostFace;
                                        if (learningTag && !(name.Equals("UnknownNull") || name.Equals("UnknownFace")))
                                        {
                                            learnImage           = imageroi;
                                            matchedResult        = eigenRecog.Recognise(learnImage);
                                            string[] matchedData = matchedResult.Split(' ');
                                            if ((Double.Parse(matchedData[1]) <= eigenRecog.getRecognizeTreshold) && (Double.Parse(matchedData[1]) != 0))
                                            {
                                                meanDistance = recogDistanceResult.Sum() / maxImageCount;
                                                if (meanDistance <= eigenRecog.getRecognizeTreshold)
                                                {
                                                    learnImage.Save(tempPath);
                                                    mydb.InsertImageTraining(name, tempPath, false);
                                                    if (mydb.getSpecifyImageCount(name) > 3)
                                                    {
                                                        mydb.DeleteOldestImage(name);
                                                    }
                                                    eigenRecog.reloadData();
                                                    learningTag = false;
                                                    Console.WriteLine("Learning:" + name + "  Distance:" + meanDistance);
                                                }
                                                else
                                                {
                                                    Console.WriteLine("Distance:" + meanDistance + "\n");
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("recognizing...");
                                        matchedResult = eigenRecog.Recognise(imageroi);
                                        Console.WriteLine("Result:" + matchedResult + "\n");
                                        File.AppendAllText(@logFolder + logName + "_ver1.0.txt", "Result:" + matchedResult + "\r\n");
                                        string[] matchedData = matchedResult.Split(' ');
                                        if (!matchedResult[0].Equals("UnknownNull") && !matchedResult[0].Equals("UnknownFace"))
                                        {
                                            //Console.WriteLine(matchedData[0] +" "+ matchedData[1]);
                                            recogNameResult.Add(matchedData[0]);
                                            recogDistanceResult.Add(Double.Parse(matchedData[1]));
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Write(e);
                        }
                    });
                }
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;

                // Format and display the TimeSpan value.
                string elapsedTime = String.Format("{0}",

                                                   ts.TotalMilliseconds * 10000);
                textBox2.Text = elapsedTime;
                //listView1.Items.Add(elapsedTime);
                //File.AppendAllText(@logFolder + logName + "_ver1.0.txt", "Frametime: "+elapsedTime+"\r\n");
                stopWatch.Reset();
            }
        }
        private bool TrainFrame(int newid)
        {
            try
            {
                Image <Gray, byte> darkimage = new Image <Gray, byte>(ROIwidth, ROIheight);
                Image <Gray, byte> cropimage = new Image <Gray, byte>(ROIwidth, ROIheight);

                //ArrayList pic = new ArrayList();
                if (loadImage != null)
                {
                    var faces = face.Detect(loadImage, 1.3, 6, HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(120, 120), new Size(200, 200));
                    if (faces.Length > 0)
                    {
                        foreach (var facecount in faces)
                        {
                            facePosition = new Point(facecount.rect.X, facecount.rect.Y);
                            var eyeObjects = eyeWithGlass.DetectMultiScale(loadImage, 1.3, 6, minEye, maxEye);
                            if (eyeObjects.Length == 2)
                            {
                                Console.WriteLine("eye");
                                if (eyeObjects[0].X > eyeObjects[1].X)
                                {
                                    var temp = eyeObjects[0];
                                    eyeObjects[0] = eyeObjects[1];
                                    eyeObjects[1] = temp;
                                }
                                int betweeneLength    = eyeObjects[1].X - eyeObjects[0].X;
                                int lefteyebrowpoint  = eyeObjects[0].X;                                        //
                                int righteyebrowpoint = eyeObjects[0].X + betweeneLength + eyeObjects[1].Width; //
                                int xxx = (int)((1.5 / 8.0) * betweeneLength);
                                int neareyebrowpoint = (int)(0.2 * betweeneLength);
                                int faceheight       = (int)(2.3 * betweeneLength);


                                loadImage.ROI = new Rectangle(new Point(lefteyebrowpoint - xxx, eyeObjects[0].Y - neareyebrowpoint), new Size((righteyebrowpoint + xxx) - (lefteyebrowpoint - xxx), faceheight));

                                cropimage     = loadImage.Copy().Resize(ROIwidth, ROIheight, INTER.CV_INTER_LINEAR);
                                loadImage.ROI = Rectangle.Empty;
                                loadImage.Draw(new Rectangle(new Point(lefteyebrowpoint - xxx, eyeObjects[0].Y - neareyebrowpoint), new Size((righteyebrowpoint + xxx) - (lefteyebrowpoint - xxx), faceheight)), new Gray(0), 2);
                                if (!cropimage.Equals(darkimage))
                                {
                                    cropimage._EqualizeHist();

                                    imageBox7.Image = cropimage;     //line 2


                                    cropimage.Save(folderPath + tempPath);
                                    string dbPath = (folderPath + tempPath).Replace("\\", "/");
                                    //mydb.InsertImageTraining(newid, dbPath, true);

                                    //File.Delete(tempPath);
                                    eigenRecog.reloadData();
                                    imageBox1.Image = loadImage;
                                    imageBox7.Image = cropimage;
                                    return(true);
                                    //Fish_Recog.reloadData();
                                }
                                else
                                {
                                    imageBox1.Image = loadImage;
                                    imageBox7.Image = cropimage;
                                    return(false);
                                }
                            }
                            else
                            {
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
            return(false);
        }
コード例 #4
0
        private void TrainFrame()
        {
            try
            {
                Image <Gray, byte> darkimage = new Image <Gray, byte>(ROIwidth, ROIheight);
                Image <Gray, byte> cropimage = new Image <Gray, byte>(ROIwidth, ROIheight);

                //ArrayList pic = new ArrayList();
                if (imageFrameT != null)
                {
                    Image <Gray, byte> greyImage = imageFrameT.Convert <Gray, byte>();


                    var faces = face.Detect(greyImage, 1.3, 6, HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(120, 120), new Size(200, 200));
                    if (faces.Length > 0)
                    {
                        foreach (var facecount in faces)
                        {
                            facePosition = new Point(facecount.rect.X, facecount.rect.Y);
                            var eyeObjects = eyeWithGlass.DetectMultiScale(greyImage, 1.3, 6, minEye, maxEye);
                            if (eyeObjects.Length == 2)
                            {
                                Console.WriteLine("eye");
                                if (eyeObjects[0].X > eyeObjects[1].X)
                                {
                                    var temp = eyeObjects[0];
                                    eyeObjects[0] = eyeObjects[1];
                                    eyeObjects[1] = temp;
                                }
                                int betweeneLength    = eyeObjects[1].X - eyeObjects[0].X;
                                int lefteyebrowpoint  = eyeObjects[0].X;                                        //
                                int righteyebrowpoint = eyeObjects[0].X + betweeneLength + eyeObjects[1].Width; //
                                int xxx = (int)((1.5 / 8.0) * betweeneLength);
                                int neareyebrowpoint = (int)(0.2 * betweeneLength);
                                int faceheight       = (int)(2.3 * betweeneLength);

                                //imageFrameT.Draw(facecount.rect, new Bgr(Color.Red), 2);
                                //imageFrameT.Draw(facecount.rect.Height + "," + facecount.rect.Width, ref font, new Point(facecount.rect.X - 2, facecount.rect.Y - 2), new Bgr(Color.LightGreen));
                                greyImage.ROI = new Rectangle(new Point(lefteyebrowpoint - xxx, eyeObjects[0].Y - neareyebrowpoint), new Size((righteyebrowpoint + xxx) - (lefteyebrowpoint - xxx), faceheight));
                                //CropFrame = greyImage.Copy();
                                //pic.Add(CropFrame);

                                //get bigger face in frame
                                cropimage = greyImage.Resize(ROIwidth, ROIheight, INTER.CV_INTER_LINEAR);
                                if (!cropimage.Equals(darkimage))
                                {
                                    cropimage._EqualizeHist();
                                    //CvInvoke.cvSmooth(cropimage, cropimage, SMOOTH_TYPE.CV_GAUSSIAN, 1, 1, 1, 1);
                                    //cropimage = eigenRecog.convertLBP(cropimage,1);
                                    imageBox7.Image = cropimage;     //line 2


                                    cropimage.Save(tempPath);
                                    mydb.InsertImageTraining(textBox1.Text, tempPath, true);

                                    //File.Delete(tempPath);
                                    eigenRecog.reloadData();
                                    //Fish_Recog.reloadData();
                                }
                            }
                            imageBox7.Image = cropimage;
                        }
                    }
                }
            }
            catch
            {
                // MessageBox.Show("Enable the face detection first", "Training Fail", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }