/// <summary> /// Detects objects from an image and stores them as rectangles. /// </summary> private void Detect() { //areas of detected elements are described as rectangles faces = new List <Rectangle>(); eyePairs = new List <Rectangle>(); leftEyes = new List <Rectangle>(); rightEyes = new List <Rectangle>(); noses = new List <Rectangle>(); mouths = new List <Rectangle>(); try { DetectFace.Detect( image, "haarcascades\\frontalface_default.xml", "haarcascades\\eyes.xml", "haarcascades\\lefteye_2splits.xml", "haarcascades\\righteye_2splits.xml", "haarcascades\\nose.xml", "haarcascades\\mouth.xml", faces, eyePairs, leftEyes, rightEyes, noses, mouths, out long detectionTime); Console.WriteLine($"Image {FilePath} finished, detection time: {detectionTime} ms"); } catch (Emgu.CV.Util.CvException e) { Console.WriteLine("\n" + e.Message); Console.WriteLine("ERROR: Out of memory! Try to load smaller images!"); System.Windows.Forms.MessageBox.Show(new System.Windows.Forms.Form { TopMost = true }, "Out of memory! Try to load smaller images!", "Warning"); } foreach (Rectangle f in faces) { CvInvoke.Rectangle(image, f, new Bgr(Color.Red).MCvScalar, lineThickness); faceCount++; } foreach (Rectangle e in eyePairs) { CvInvoke.Rectangle(image, e, new Bgr(Color.Green).MCvScalar, lineThickness); eyePairCount++; } foreach (Rectangle le in leftEyes) { CvInvoke.Rectangle(image, le, new Bgr(Color.Pink).MCvScalar, lineThickness); leftEyeCount++; } foreach (Rectangle re in rightEyes) { CvInvoke.Rectangle(image, re, new Bgr(Color.Blue).MCvScalar, lineThickness); rightEyeCount++; } foreach (Rectangle n in noses) { CvInvoke.Rectangle(image, n, new Bgr(Color.White).MCvScalar, lineThickness); noseCount++; } foreach (Rectangle m in mouths) { CvInvoke.Rectangle(image, m, new Bgr(Color.Black).MCvScalar, lineThickness); mouthCount++; } }
static void Run() { Mat image = new Mat("lena.jpg", LoadImageType.Color); //Read the files as an 8-bit Bgr image long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); //The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release //disabling CUDA module for now bool tryUseCuda = false; DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, tryUseCuda, out detectionTime); foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2); } foreach (Rectangle eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); } //display the image ImageViewer.Show(image, String.Format( "Completed face and eye detection using {0} in {1} milliseconds", (tryUseCuda && CudaInvoke.HasCuda) ? "GPU" : CvInvoke.UseOpenCL ? "OpenCL" : "CPU", detectionTime)); }
static void Run() { IImage image; //Read the files as an 8-bit Bgr image image = new UMat("C:/Users/lucas/Desktop/samt/anno3/306/Progetto2_FaceDetectionPeopleCounting/img/facce.png", ImreadModes.Color); //UMat version //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2); } foreach (Rectangle eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); } //display the image using (InputArray iaImage = image.GetInputArray()) ImageViewer.Show(image, String.Format( "Completed face and eye detection using {0} in {1} milliseconds", (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" : (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL" : "CPU", detectionTime)); }
static void Run() { IImage image; Camera camera = new Camera(); camera.Show(); while (true) { image = camera.GetImage(); long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Green).MCvScalar, 2); } foreach (Rectangle eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); } camera.Refresh(); } }
private Mat ApplyDetection(Mat image) { long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); //The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release //disabling CUDA module for now bool tryUseCuda = false; bool tryUseOpenCL = true; DetectFace.Detect( image, "Dlls\\EmguCv\\haarcascade_frontalface_default.xml", "Dlls\\EmguCv\\haarcascade_eye.xml", faces, eyes, tryUseCuda, tryUseOpenCL, out detectionTime); foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2); } foreach (Rectangle eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); } return(image); }
public List <Bitmap> Search(List <Bitmap> images) { imageList.Clear(); for (int i = 0; i < images.Count; i++) { Image <Bgr, Byte> image = new Image <Bgr, byte>(images[i]); long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect(image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); if (faces.Count > 0) { //foreach (Rectangle face in faces) // image.Draw(face, new Bgr(Color.Red), 2); // foreach (Rectangle eye in eyes) // image.Draw(eye, new Bgr(Color.Blue), 2); Image <Bgr, byte> resizedImage = image.Resize(300, 300, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); imageList.Add(resizedImage.ToBitmap()); } //pictureboxview.Image = resizedImage.ToBitmap(); } //Read the files as an 8-bit Bgr image //Graphics g = Graphics.FromImage(image); return(imageList); //if (faces.Count > 0) //{ // using (ExifReader reader = new ExifReader(@"C:\test\test4face.jpg")) // { // // Extract the tag data using the ExifTags enumeration // DateTime datePictureTaken; // if (reader.GetTagValue<DateTime>(ExifTags.DateTimeDigitized, // out datePictureTaken)) // { // // Do whatever is required with the extracted information // String imageCommnets = string.Format("The picture was taken on {0}", datePictureTaken); // MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 3.0, 3.0); // image.Draw(imageCommnets, ref f, new Point(150, 150), new Emgu.CV.Structure.Bgr(255.0, 0, 0)); // } // } //} //display the image //Emgu.CV.UI.ImageViewer.Show(image, String.Format( //"Completed face and eye detection using {0} in {1} milliseconds", //GpuInvoke.HasCuda ? "GPU" : "CPU", //detectionTime)); }
private void process(object sender, EventArgs args) { Image <Bgr, Byte> image = camera.RetrieveBgrFrame(); long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect(image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) { image.Draw(face, new Bgr(Color.Red), 2); } foreach (Rectangle eye in eyes) { image.Draw(eye, new Bgr(Color.Blue), 2); } pictureBox1.Image = image.ToBitmap(); }
private void detectButton_Click(object sender, EventArgs e) { if (pictureBox.Image == null) { return; } if (loadFileCheckBox.Checked && openImageCheckBox.Checked) { pictureBox.Image = DetectFace.Detect((Mat)pictureBox.Image, _selectedAlgorithmIndex, eyesCheckBox.Checked, mouthCheckBox.Checked, noseCheckBox.Checked); return; } _detect = !_detect; if (!loadFileCheckBox.Checked) { detectButton.Text = _detect ? "Stop detection" : "Start detection"; } }
static void ProcessImage(IImage image, out long detectionTime) { List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2); } foreach (Rectangle eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); } }
static void Run() { //读取指定路径的图片 IImage image = new UMat("images/IMG_1288.jpg", ImreadModes.Color); //UMat version //定义人脸识别的时间 long detectionTime; //人脸列表 List <Rectangle> faces = new List <Rectangle>(); //眼睛列表 List <Rectangle> eyes = new List <Rectangle>(); //开始识别 DetectFace.Detect(image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); //将识别后的所有面部,用矩形区域标注出来 foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2); } /* * 2017-10-19 不需要标注眼睛,比较混乱 * * //将识别后的所有眼睛,用矩形区域标注出来 * foreach (Rectangle eye in eyes) * { * CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); * } */ using (InputArray iaImage = image.GetInputArray()) { //获取人脸检测方式 String method = (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) ? "CUDA" : (iaImage.IsUMat && CvInvoke.UseOpenCL) ? "OpenCL" : "CPU"; //显示图片 ImageViewer.Show(image, String.Format("检测完毕,使用 {0} 模式, 耗时 {1} 毫秒", method, detectionTime)); } }
public static Bitmap detectFace(Bitmap bitImage) { Image <Bgr, Byte> image = new Image <Bgr, byte>(bitImage); //Read the files as an 8-bit Bgr image Console.WriteLine("face"); long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect(image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) { image.Draw(face, new Bgr(Color.Red), 2); } foreach (Rectangle eye in eyes) { image.Draw(eye, new Bgr(Color.Blue), 2); } return(image.Bitmap); }
private void ProcessFrame(object sender, EventArgs arg) { if (webcamCheckBox.Checked) { var frame = new Mat(); _capture.Retrieve(frame, 0); pictureBox.Image = _detect ? DetectFace.Detect(frame, _selectedAlgorithmIndex, eyesCheckBox.Checked, mouthCheckBox.Checked, noseCheckBox.Checked) : frame; } else if (loadFileCheckBox.Checked) { var frame = _capture.QueryFrame(); pictureBox.Image = _detect ? DetectFace.Detect(frame, _selectedAlgorithmIndex, eyesCheckBox.Checked, mouthCheckBox.Checked, noseCheckBox.Checked) : frame; Thread.Sleep((int)(1000.0 / _frameRate)); } GC.Collect(); }
private static void Run() { IImage image; //Read the files as an 8-bit Bgr image image = new UMat("lena.jpg", ImreadModes.Color); //UMat version //image = new Mat("lena.jpg", ImreadModes.Color); //CPU version long detectionTime; var faces = new List <Rectangle>(); var eyes = new List <Rectangle>(); DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (var face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 2); } foreach (var eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(Color.Blue).MCvScalar, 2); } //display the image using (var iaImage = image.GetInputArray()) { ImageViewer.Show(image, string.Format( "Completed face and eye detection using {0} in {1} milliseconds", iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda ? "CUDA" : iaImage.IsUMat && CvInvoke.UseOpenCL ? "OpenCL" : "CPU", detectionTime)); } }
static void Run() { Image <Bgr, Byte> image = new Image <Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image long detectionTime; List <Rectangle> faces = new List <Rectangle>(); List <Rectangle> eyes = new List <Rectangle>(); DetectFace.Detect(image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) { image.Draw(face, new Bgr(Color.Red), 2); } foreach (Rectangle eye in eyes) { image.Draw(eye, new Bgr(Color.Blue), 2); } //display the image ImageViewer.Show(image, String.Format( "Completed face and eye detection using {0} in {1} milliseconds", GpuInvoke.HasCuda ? "GPU": "CPU", detectionTime)); }
void Process() { /*var context = CIContext.FromOptions(null); * var detector = CIDetector.CreateFaceDetector (context, true); * var ciImage = CIImage.FromCGImage(imageView.Image.CGImage); * CIFeature[] features = detector.FeaturesInImage(ciImage); * * using (Image<Bgr, Byte> image = new Image<Bgr, Byte>(imageView.Image.CGImage)) * { * if (features != null) * { * foreach (CIFaceFeature face in features) * { * CGPoint pTest = FlipForBottomOrigin(face.LeftEyePosition, 960); * image.Draw(new Rectangle((int)pTest.X, (int)pTest.Y, 200, 200), new Bgr(Color.Blue), 3); * } * } */ faces = new List <Rectangle>(); leftEyes = new List <Rectangle>(); rightEyes = new List <Rectangle>(); upperBodies = new List <Rectangle>(); lowerBodies = new List <Rectangle>(); mouths = new List <Rectangle>(); noses = new List <Rectangle>(); people = new List <Rectangle>(); bodies = new List <Rectangle>(); CGImage myImage = imageView.Image.CGImage; long processingTime; using (Image <Bgr, Byte> image = new Image <Bgr, Byte>(myImage)) { CGRect p = imageView.Frame; Dictionary <String, List <Rectangle> > recObjects = new Dictionary <string, List <Rectangle> >(); recObjects.Add("faces", faces); recObjects.Add("leftEyes", leftEyes); recObjects.Add("rightEyes", rightEyes); recObjects.Add("upperBodies", upperBodies); recObjects.Add("lowerBodies", lowerBodies); recObjects.Add("mouths", mouths); recObjects.Add("noses", noses); recObjects.Add("people", people); recObjects.Add("bodies", bodies); Dictionary <String, String> facialFeatures = new Dictionary <string, string>(); facialFeatures.Add("face", "haarcascade_frontalface_alt.xml"); facialFeatures.Add("reye", "haarcascade_righteye_2splits.xml"); facialFeatures.Add("leye", "haarcascade_lefteye_2splits.xml"); facialFeatures.Add("mouth", "Mouth.xml"); facialFeatures.Add("nose", "Nariz.xml"); facialFeatures.Add("ubody", "haarcascade_mcs_upperbody.xml"); facialFeatures.Add("lbody", "haarcascade_lowerbody.xml"); facialFeatures.Add("body", "haarcascade_fullbody.xml"); DetectFace.Detect( image.Mat, facialFeatures, recObjects, out processingTime ); /*foreach (Rectangle body in upperBodies) * image.Draw(body, new Bgr(Color.LimeGreen), 3); * foreach (Rectangle body in lowerBodies) * image.Draw(body, new Bgr(Color.LimeGreen), 3); * foreach (Rectangle body in bodies) * image.Draw(body, new Bgr(Color.Yellow), 3); * //foreach (Rectangle person in people) * // image.Draw(person, new Bgr(Color.Blue), 5); * foreach (Rectangle face in faces) * image.Draw(face, new Bgr(Color.Red), 3); * foreach (Rectangle eye in leftEyes) * image.Draw(eye, new Bgr(Color.LightBlue), 3); * foreach (Rectangle eye in rightEyes) * image.Draw(eye, new Bgr(Color.DarkBlue), 3); * foreach (Rectangle mouth in mouths) * image.Draw(mouth, new Bgr(Color.Yellow), 3); * foreach (Rectangle nose in noses) * image.Draw(nose, new Bgr(Color.Violet), 3);*/ //using (Image<Bgr, Byte> resized = image.Resize((int)Window.Frame.Width, (int)Window.Frame.Height, Emgu.CV.CvEnum.Inter.Linear, true)) //{ //imageView.Frame = new RectangleF(PointF.Empty, resized.Size); imageView.Image = image.ToUIImage(); if (faces.Count > 0) { using (CGImage cr = imageView.Image.CGImage.WithImageInRect(faces[0])) { UIImage cropped = UIImage.FromImage(cr); //imageView.Image = cropped; } } //} //messageLabel.Text = String.Format("Face: (x: {0}, y: {1}, h: {2}, w: {3}", faces[0].X, faces[0].Y, faces[0].Height, faces[0].Width); //messageLabel.Text = String.Format("Processing Time: {0} milliseconds.", processingTime); //messageElement.GetImmediateRootElement().Reload(messageElement, UITableViewRowAnimation.Automatic); imageView.SetNeedsDisplay(); } }