private void ProcessFrame(object sender, EventArgs arg) { if (imageFrame != null) { Image <Gray, byte> greyImage = imageFrame.Copy().Convert <Gray, byte>(); greyImage = greyImage.SmoothMedian(3); //stopWatch.Start(); var faces = face.Detect(greyImage, 1.3, 6, HAAR_DETECTION_TYPE.FIND_BIGGEST_OBJECT, new Size(120, 120), new Size(300, 300)); if (faces.Length == 0) { var eyeObjects = eyeWithGlass.DetectMultiScale(greyImage, 1.3, 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 margin = (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 - margin, eyeObjects[0].Y + (eyeObjects[0].Height/6)); * * p[4] = new Point(righteyebrowpoint + margin, 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 - margin, eyeObjects[0].Y), 1), new Bgr(Color.AliceBlue), 2); * //imageFrame.Draw(new CircleF(new PointF(righteyebrowpoint + margin, 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 { learningTag = true; name = "Processing..."; userid = "Processing..."; timestampFlag = true; faceRectangle = Rectangle.Empty; realfaceRectangle = Rectangle.Empty; label2.ForeColor = Color.DeepSkyBlue; label2.Text = "Idle"; progressBar1.Value = 0; recogNameResult.Clear(); recogDistanceResult.Clear(); Console.WriteLine("Clear"); } ARDisplayFlag = false; } 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.3, 6, minEye, maxEye); greyImage.ROI = 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[1].X + eyeObjects[1].Width; // int margin = (int)((1.5 / 8.0) * betweeneLength); int neareyebrowpoint = (int)(0.2 * betweeneLength); int faceheight = (int)(2.3 * betweeneLength); realFacePosition = new Point(facePosition.X + lefteyebrowpoint - margin, facePosition.Y + eyeObjects[0].Y - neareyebrowpoint); realfaceRectangleSize = new Size((righteyebrowpoint + margin) - (lefteyebrowpoint - margin), faceheight); realfaceRectangle = new Rectangle(realFacePosition, realfaceRectangleSize); greyImage.ROI = realfaceRectangle; imageroi = greyImage.Copy(); greyImage.ROI = Rectangle.Empty; if (eigenRecog.IsTrained) { imageroi._EqualizeHist(); imageroi = imageroi.Resize(ROIwidth, ROIheight, INTER.CV_INTER_LINEAR); Image <Gray, byte> plainImage = imageroi.Copy(); imageroi.FillConvexPoly(pL, new Gray(128)); imageroi.FillConvexPoly(pR, new Gray(128)); //find the most relative face progressBar1.Value = recogNameResult.Count; if (recogNameResult.Count == maxImageCount && !ARDisplayFlag) { Console.WriteLine("Processing..."); int max = 0; string mostFace = ""; double euclidVal = 0; 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; userid = mydb.getUserIdFromSerrogate(name); if (learningTag && !(name.Equals("UnknownNull") || name.Equals("UnknownFace"))) { learnImage = imageroi; matchedResult = eigenRecog.Recognise(learnImage); //matchedResult = fishRecog.Recognise(learnImage); string[] matchedData = matchedResult.Split(' '); if ((Double.Parse(matchedData[1]) <= eigenRecog.getRecognizeTreshold) && (Double.Parse(matchedData[1]) != 0)) //if ((Double.Parse(matchedData[1]) <= fishRecog.getRecognizeTreshold) && (Double.Parse(matchedData[1]) != 0)) { int ItemIndex = 0; List <double> recogResultSum = new List <double>(); foreach (var item in recogNameResult) { if (mydb.getUserIdFromSerrogate(item).Equals(userid) && !item.Equals("UnknownNull")) { recogResultSum.Add(recogDistanceResult[ItemIndex]); } ItemIndex++; } meanDistance = recogResultSum.Sum() / recogResultSum.Count; euclidVal = falsePositiveCheck(mydb.getComputeResultImage(name), imageroi.Copy()); if (euclidVal < 9000) { confident = (((double)recogResultSum.Count / (double)maxImageCount) * 100) * (1.00 - (euclidVal - (meanDistance * 130 / 100)) / 4500.00); if (confident > 100) { confident = 100; } if (confident >= 80) //if (meanDistance <= fishRecog.getRecognizeTreshold) { learnImage.Save(folderPath + tempPath); plainImage.Save(folderPath + tempPath2); Console.WriteLine(folderPath + tempPath); string dbPath = (folderPath + tempPath).Replace("\\", "/"); string dbPathPlain = (folderPath + tempPath2).Replace("\\", "/"); mydb.InsertImageTraining(int.Parse(userid), dbPathPlain, dbPath, false); // //auto train // if (mydb.getSpecifyImageCount(userid) > 5) { mydb.DeleteOldestImage(userid); } //eigenRecog.reloadData(); //fishRecog.reloadData(); learningTag = false; Console.WriteLine("Learning:" + userid + " Distance:" + meanDistance); } else { Console.WriteLine("Distance:" + meanDistance + "\n"); } } } if (timestampFlag) { Image <Gray, byte> resultImage = imageroi; resultImage.Save(folderPath + tempPath); Console.WriteLine(folderPath + tempPath); string dbPath = (folderPath + tempPath).Replace("\\", "/"); mydb.InsertTimestamp(userid, Environment.MachineName, confident.ToString(), dbPath, meanDistance.ToString(), euclidVal.ToString()); timestampFlag = false; } } } else { Console.WriteLine("recognizing..."); matchedResult = eigenRecog.Recognise(imageroi); //get serro+distance Console.WriteLine("Result:" + matchedResult + "\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])); //File.AppendAllText(@"C:\Users\Xypher\Desktop\test2.csv", matchedData[1]+"\r\n"); } } } } } catch { } }); } /*stopWatch.Stop(); * TimeSpan ts = stopWatch.Elapsed; * * string elapsedTime = String.Format("{0}",ts.TotalMilliseconds * 10000); * stopWatch.Reset();*/ } }