I've been trying to create a face recognition system. I managed to write the code to save faces as objects and store them in a folder. However, I'm now confused about how to make it automatically recognize faces when I turn on the webcam, and turns the label to the face file name. Since my code I believe were simple than the Internet, so it makes me more confused, I'm all ears to hear every single advice and comment, thank you, shall I change the whole code?
Код: Выделить всё
private async Task StreamVideo2() { var faceCasade = new CascadeClassifier("./detection/haarcascade_frontalface_default.xml"); var vc = new VideoCapture(0, Emgu.CV.VideoCapture.API.DShow); // Get the list of saved face images string folder = Path.Combine(Application.StartupPath, "TrainedFaces"); string[] savedImages = Directory.GetFiles(folder, "*.bmp"); while (streamVideo) { var frame = new Mat(); var frameGray = new Mat(); vc.Read(frame); CvInvoke.CvtColor(frame, frameGray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); var faces = faceCasade.DetectMultiScale(frameGray, 1.3, 5); if (faces != null && faces.Length > 0) { CvInvoke.Rectangle(frame, faces[0], new MCvScalar(0, 255, 0), 2); // --> MCvScalar EMGU STRUCTURE [ RETACLE GREEN ON FACE ] // Crop var faceRect = faces[0]; var croppedFace = new Mat(frame, faceRect); // LABEL string labelText = "Detected Face"; var font = new Emgu.CV.CvEnum.FontFace(); var fontScale = 0.8; var fontThickness = 1; var fontColor = new MCvScalar(0, 255, 0); var textOrg = new System.Drawing.Point(faceRect.X, faceRect.Y - 10); CvInvoke.PutText(frame, labelText, textOrg, font, fontScale, fontColor, fontThickness); // ResizedCroppedFace var resizedCroppedFace = new Mat(); var resizedSize = new System.Drawing.Size(184, 192); // Size to keep the croppedFace. CvInvoke.Resize(croppedFace, resizedCroppedFace, resizedSize); // Convert cropped face to black and white (grayscale) var grayCroppedFace = new Mat(); CvInvoke.CvtColor(resizedCroppedFace, grayCroppedFace, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); // Display the grayscale cropped face in pictureBox2 pictureBox2.Image = grayCroppedFace.ToBitmap(); } else { pictureBox2.Image = null; } // TURN CAMERA TO IMG PICTURE BOX var img = frame.ToBitmap(); pictureBox1.Image = img; label1.Text = "Mat Size: " + frame.Width.ToString() + " X " + frame.Height.ToString(); if (CvInvoke.WaitKey(1) == 27) { break; } await Task.Delay(16); } } private void button1_Click(object sender, System.EventArgs e) { if (pictureBox2.Image != null) { string folder = Path.Combine(Application.StartupPath, "TrainedFaces"); string filename = textBox1.Text.Trim(); string savePath = Path.Combine(folder, $"{filename}.bmp"); // Saved As An Object using var bitmapImage = new Bitmap(pictureBox2.Image); bitmapImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Bmp); MessageBox.Show("Succed"); textBox1.Clear(); if (!Directory.Exists(folder)) { MessageBox.Show("Not Found"); } } else { MessageBox.Show("Turn On The Camera"); } }``` I tried few things, but it just make me lost more & more.
Источник: https://stackoverflow.com/questions/781 ... -the-faces