private bool trackingInit(Image <Bgr, byte> bgrImage) { using (var grayframe = new Image <Gray, byte>(new Bitmap(bgrImage.Bitmap))) { var faceDetector = new FaceDetector(); isInit = tracker.Init(grayframe.Mat, humanFace.faceRectangle); //Debug.Print("Tracking Init {0}", isInit); if (isInit && !findFace) { bgrImage = faceDetector.drawFaceToImage(bgrImage, humanFace); findFace = true; UpdatePersonAsync(); failFrameCount = 0; } return(isInit); } }
private async void BtnImport_Click(object sender, EventArgs e) { FaceDetector faceDetector = new FaceDetector(); int successCount = 0; foreach (String file in files) { if (File.Exists(file)) { var filename = Path.GetFileNameWithoutExtension(file); Image imageFile = Image.FromFile(file); var human = faceDetector.findHumanFace(imageFile); if (human.Count() > 0) { Byte[] bytes = human[0].face.Resize(RESIZE_IMAGE_WIIDTH, RESIZE_IMAGE_HEIGHT, Emgu.CV.CvEnum.Inter.Cubic).ToJpegData(); //Byte[] bytes = File.ReadAllBytes(file); String image = Convert.ToBase64String(bytes); try { if (!(await ImportFaceToServerAsync(new Person(filename, "", new WebEntity.Face(image))))) { LstFailBox.Items.Add(file); } else { successCount++; } } catch (Exception ex) { Debug.WriteLine("Error import image: " + ex); LstFailBox.Items.Add(file); } } } } MessageBox.Show("Import complete, total " + successCount + " file import success.", "Import complete", MessageBoxButtons.OK); }
public bool trackFace(Image <Bgr, byte> bgrImage) { var result = false; using (var grayframe = new Image <Gray, byte>(new Bitmap(bgrImage.Bitmap))) { if (!isInit) { if (bgrImage != null) { result = trackingInit(bgrImage); } } else { if (bgrImage != null) { try { var updateFace = new Rectangle(); result = tracker.Update(grayframe.Mat, out updateFace); if (!result) { failFrameCount++; findFace = false; } else { //var updateFace = new Rectangle(); //var result = tracker.Update(grayframe.Mat, out updateFace); /* * for ( var i = 0; i < updateFace.ToArray().Count(); i ++) * { * bgrImage.Draw(updateFace.ToArray()[i], new Bgr(Color.Red), 3); * }*/ Color color = Color.Gray; if (Person != null) { color = Color.Green; } else if (FailToRecognize) { color = Color.Red; } bgrImage.Draw(updateFace, new Bgr(color), 3); //TODO update name if (Person != null) { using (Graphics g = Graphics.FromImage(bgrImage.Bitmap)) { int tWidth = (int)g.MeasureString(Person.Name, new Font("Arial", 12, FontStyle.Bold)).Width; int x; if (tWidth >= updateFace.Width) { x = updateFace.Left - ((tWidth - updateFace.Width) / 2); } else { x = (updateFace.Width / 2) - (tWidth / 2) + updateFace.Left; } g.DrawString(Person.Name, new Font("Arial", 12, FontStyle.Bold), Brushes.Green, new PointF(x, updateFace.Top - 18)); } } humanFace.faceRectangle = updateFace; findFace = true; //UpdatePersonAsync(); if (FailToRecognize) { failFrameCount++; successFrameCount = 0; } else { failFrameCount = 0; if (successFrameCount > SUCCESSRETRYFRAMECOUNT) { FaceDetector faceDetector = new FaceDetector(); var detectedFaces = faceDetector.findFace(bgrImage); var sameFaceFind = false; foreach (var r1 in detectedFaces) { var r1Size = r1.Height * r1.Width; var r2 = humanFace.faceRectangle; float r2Size = r2.Height * r2.Width; var r3 = Rectangle.Intersect(r1, r2); if (!r3.IsEmpty) { float r3Size = r3.Height * r3.Width; float ratio = r3Size / Math.Min(r1Size, r2Size); if (ratio < INTERSECTRATIO) { Debug.Print("Face compare, Ratio: {0}", ratio); } else { sameFaceFind = true; } } else { Debug.Print("r3 is empty"); } } if (!sameFaceFind) { missingFace = true; } faceDetector = null; successFrameCount = 0; } else { successFrameCount++; } } } } catch (Exception ex) { Debug.Print(ex.ToString()); } } } } return(result); }