public static void Detect( Mat image, String faceFileName, String eyeFileName, List <Rectangle> faces, List <Rectangle> eyes, bool tryUseCuda) { #if !(__IOS__ || NETFX_CORE) if (tryUseCuda && CudaInvoke.HasCuda) { if (face == null) { face = new CudaCascadeClassifier(faceFileName); } if (eye == null) { eye = new CudaCascadeClassifier(eyeFileName); } //using (face) //using (eye) { face.ScaleFactor = 1.1; face.MinNeighbors = 10; face.MinObjectSize = Size.Empty; eye.ScaleFactor = 1.1; eye.MinNeighbors = 10; eye.MinObjectSize = Size.Empty; using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>()) using (GpuMat region = new GpuMat()) { face.DetectMultiScale(gpuGray, region); Rectangle[] faceRegion = face.Convert(region); faces.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eye.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eye.Convert(eyeRegionMat); foreach (Rectangle e in eyeRegion) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } } } #endif }
public void TestClone() { if (CudaInvoke.HasCuda) { Image <Gray, Byte> img = new Image <Gray, byte>(300, 400); img.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0)); using (CudaImage <Gray, Byte> gImg1 = new CudaImage <Gray, byte>(img)) using (CudaImage <Gray, Byte> gImg2 = gImg1.Clone(null)) using (Image <Gray, Byte> img2 = gImg2.ToImage()) { Assert.IsTrue(img.Equals(img2)); } } }
IImage CudaDetect(IImage original, List <Rectangle> faces, List <Rectangle> eyes) { using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName)) using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName)) { face.ScaleFactor = 1.1; face.MinNeighbors = 10; face.MinObjectSize = Size.Empty; eye.ScaleFactor = 1.1; eye.MinNeighbors = 10; eye.MinObjectSize = Size.Empty; using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(original)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>()) using (GpuMat region = new GpuMat()) { face.DetectMultiScale(gpuGray, region); Rectangle[] faceRegion = face.Convert(region); faces.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eye.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eye.Convert(eyeRegionMat); foreach (Rectangle e in eyeRegion) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } } IImage copy = CopyAndDraw(original, faces.ToArray()); copy = CopyAndDraw(copy, eyes.ToArray()); return(copy); //return eyes; }
//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; //FaceDetection.DetectFaceAndEyes( // 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)); public static void DetectFaceAndEyes( Mat image, String faceFileName, String eyeFileName, List <Rectangle> faces, List <Rectangle> eyes, bool tryUseCuda, out long detectionTime) { Stopwatch watch; #if !(__IOS__ || NETFX_CORE) if (tryUseCuda && CudaInvoke.HasCuda) { using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName)) using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName)) { face.ScaleFactor = 1.1; face.MinNeighbors = 10; face.MinObjectSize = Size.Empty; eye.ScaleFactor = 1.1; eye.MinNeighbors = 10; eye.MinObjectSize = Size.Empty; watch = Stopwatch.StartNew(); using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>()) using (GpuMat region = new GpuMat()) { face.DetectMultiScale(gpuGray, region); Rectangle[] faceRegion = face.Convert(region); faces.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eye.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eye.Convert(eyeRegionMat); foreach (Rectangle e in eyeRegion) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } watch.Stop(); } } else #endif { //Read the HaarCascade objects using (CascadeClassifier face = new CascadeClassifier(faceFileName)) using (CascadeClassifier eye = new CascadeClassifier(eyeFileName)) { watch = Stopwatch.StartNew(); using (UMat ugray = new UMat()) { CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); //normalizes brightness and increases contrast of the image CvInvoke.EqualizeHist(ugray, ugray); //Detect the faces from the gray scale image and store the locations as rectangle //The first dimensional is the channel //The second dimension is the index of the rectangle in the specific channel Rectangle[] facesDetected = face.DetectMultiScale( ugray, 1.1, 10, new Size(20, 20)); faces.AddRange(facesDetected); foreach (Rectangle f in facesDetected) { //Get the region of interest on the faces using (UMat faceRegion = new UMat(ugray, f)) { Rectangle[] eyesDetected = eye.DetectMultiScale( faceRegion, 1.1, 10, new Size(20, 20)); foreach (Rectangle e in eyesDetected) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } watch.Stop(); } } detectionTime = watch.ElapsedMilliseconds; }
public static void Detect( Mat image, String faceFileName, String eyeFileName, List <Rectangle> faces, List <Rectangle> eyes, bool tryUseCuda, bool tryUseOpenCL, out long detectionTime) { Stopwatch watch; #if !(IOS || NETFX_CORE) if (tryUseCuda && CudaInvoke.HasCuda) { using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName)) using (CudaCascadeClassifier eye = new CudaCascadeClassifier(eyeFileName)) { watch = Stopwatch.StartNew(); using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>()) { Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty); faces.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) { Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty); foreach (Rectangle e in eyeRegion) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } watch.Stop(); } } else #endif { //Many opencl functions require opencl compatible gpu devices. //As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented //So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices). CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice; //Read the HaarCascade objects using (CascadeClassifier face = new CascadeClassifier(faceFileName)) using (CascadeClassifier eye = new CascadeClassifier(eyeFileName)) { watch = Stopwatch.StartNew(); using (UMat ugray = new UMat()) { CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); //normalizes brightness and increases contrast of the image CvInvoke.EqualizeHist(ugray, ugray); //Detect the faces from the gray scale image and store the locations as rectangle //The first dimensional is the channel //The second dimension is the index of the rectangle in the specific channel Rectangle[] facesDetected = face.DetectMultiScale( ugray, 1.1, 10, new Size(20, 20)); faces.AddRange(facesDetected); foreach (Rectangle f in facesDetected) { //Get the region of interest on the faces using (UMat faceRegion = new UMat(ugray, f)) { Rectangle[] eyesDetected = eye.DetectMultiScale( faceRegion, 1.1, 10, new Size(20, 20)); foreach (Rectangle e in eyesDetected) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } watch.Stop(); } } detectionTime = watch.ElapsedMilliseconds; }
public static void DetectFace( Mat image, bool detectEyes, List <Rectangle> faces, List <Rectangle> eyes, out long detectionTime) { Stopwatch watch; String faceFileName = Application.dataPath + "\\Emgu\\haarcascades\\haarcascade_frontalface_default.xml"; String eyeFileName = Application.dataPath + "\\Emgu\\haarcascade_eye.xml"; if (!detectEyes) { eyeFileName = ""; } #if !(IOS || NETFX_CORE) if (CudaInvoke.HasCuda) { CudaCascadeClassifier face = null; if (faceFileName != "") { face = new CudaCascadeClassifier(faceFileName); } CudaCascadeClassifier eye = null; if (eyeFileName != "") { eye = new CudaCascadeClassifier(eyeFileName); } { face.ScaleFactor = 1.1; face.MinNeighbors = 10; face.MinObjectSize = Size.Empty; if (eye != null) { eye.ScaleFactor = 1.1; eye.MinNeighbors = 10; eye.MinObjectSize = Size.Empty; } watch = Stopwatch.StartNew(); using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte> (image)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte> ()) using (GpuMat region = new GpuMat()) { face.DetectMultiScale(gpuGray, region); Rectangle[] faceRegion = face.Convert(region); faces.AddRange(faceRegion); if (eye != null) { foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eye.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eye.Convert(eyeRegionMat); foreach (Rectangle e in eyeRegion) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } } watch.Stop(); detectionTime = watch.ElapsedMilliseconds; } } else #endif { detectionTime = 0; } }
public void Detect( IInputArray image, List <Rectangle> faces, List <Rectangle> eyes) { using (InputArray iaImage = image.GetInputArray()) { #if !(__IOS__ || NETFX_CORE) if (iaImage.Kind == InputArray.Type.CudaGpuMat && CudaInvoke.HasCuda) { // Traitement avec CUDA using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>()) using (GpuMat region = new GpuMat()) { faceCuda.DetectMultiScale(gpuGray, region); Rectangle[] faceRegion = faceCuda.Convert(region); faces.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eyeCuda.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eyeCuda.Convert(eyeRegionMat); foreach (Rectangle e in eyeRegion) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } } else #endif { // Traitement sans CUDA using (UMat ugray = new UMat()) { CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); //normalizes brightness and increases contrast of the image CvInvoke.EqualizeHist(ugray, ugray); //Detect the faces from the gray scale image and store the locations as rectangle //The first dimensional is the channel //The second dimension is the index of the rectangle in the specific channel Rectangle[] facesDetected = faceCpu.DetectMultiScale( ugray, 1.1, 10, new Size(20, 20)); faces.AddRange(facesDetected); foreach (Rectangle f in facesDetected) { //Get the region of interest on the faces using (UMat faceRegion = new UMat(ugray, f)) { Rectangle[] eyesDetected = eyeCpu.DetectMultiScale( faceRegion, 1.1, 10, new Size(20, 20)); foreach (Rectangle e in eyesDetected) { Rectangle eyeRect = e; eyeRect.Offset(f.X, f.Y); eyes.Add(eyeRect); } } } } } } }
public void TestClone() { if (CudaInvoke.HasCuda) { Image<Gray, Byte> img = new Image<Gray, byte>(300, 400); img.SetRandUniform(new MCvScalar(0.0), new MCvScalar(255.0)); using (CudaImage<Gray, Byte> gImg1 = new CudaImage<Gray, byte>(img)) using (CudaImage<Gray, Byte> gImg2 = gImg1.Clone(null)) using (Image<Gray, Byte> img2 = gImg2.ToImage()) { Assert.IsTrue(img.Equals(img2)); } } }
public static void Detect( Mat image, String faceFileName, String eyeleftFileName, string eyerightFileName, List <Rectangle> faces, List <Rectangle> eyesleft, List <Rectangle> eyesright, bool tryUseCuda, bool tryUseOpenCL, out long detectionTime) { Stopwatch watch; #if !(IOS || NETFX_CORE) if (tryUseCuda && CudaInvoke.HasCuda) { using (CudaCascadeClassifier face = new CudaCascadeClassifier(faceFileName)) using (CudaCascadeClassifier eyeleft = new CudaCascadeClassifier(eyeleftFileName)) using (CudaCascadeClassifier eyeright = new CudaCascadeClassifier(eyerightFileName)) { face.ScaleFactor = 1.1; face.MinNeighbors = 10; face.MinObjectSize = Size.Empty; eyeleft.ScaleFactor = 1.1; eyeleft.MinNeighbors = 10; eyeleft.MinObjectSize = Size.Empty; eyeright.ScaleFactor = 1.1; eyeright.MinNeighbors = 10; eyeright.MinObjectSize = Size.Empty; watch = Stopwatch.StartNew(); using (CudaImage <Bgr, Byte> gpuImage = new CudaImage <Bgr, byte>(image)) using (CudaImage <Gray, Byte> gpuGray = gpuImage.Convert <Gray, Byte>()) using (GpuMat region = new GpuMat()) { face.DetectMultiScale(gpuGray, region); Rectangle[] faceRegion = face.Convert(region); faces.AddRange(faceRegion); foreach (Rectangle f in faceRegion) { using (CudaImage <Gray, Byte> faceImg = gpuGray.GetSubRect(f)) { //For some reason a clone is required. //Might be a bug of CudaCascadeClassifier in opencv using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eyeleft.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eyeleft.Convert(eyeRegionMat); foreach (Rectangle eleft in eyeRegion) { Rectangle eyeRectleft = eleft; eyeRectleft.Offset(f.X, f.Y); eyesleft.Add(eyeRectleft); } } using (CudaImage <Gray, Byte> clone = faceImg.Clone(null)) using (GpuMat eyeRegionMat = new GpuMat()) { eyeright.DetectMultiScale(clone, eyeRegionMat); Rectangle[] eyeRegion = eyeright.Convert(eyeRegionMat); foreach (Rectangle eright in eyeRegion) { Rectangle eyeRectright = eright; eyeRectright.Offset(f.X, f.Y); eyesright.Add(eyeRectright); } } } } } watch.Stop(); } } else #endif { //Many opencl functions require opencl compatible gpu devices. //As of opencv 3.0-alpha, opencv will crash if opencl is enable and only opencv compatible cpu device is presented //So we need to call CvInvoke.HaveOpenCLCompatibleGpuDevice instead of CvInvoke.HaveOpenCL (which also returns true on a system that only have cpu opencl devices). CvInvoke.UseOpenCL = tryUseOpenCL && CvInvoke.HaveOpenCLCompatibleGpuDevice; //Read the HaarCascade objects using (CascadeClassifier face = new CascadeClassifier(faceFileName)) using (CascadeClassifier eyeleft = new CascadeClassifier(eyeleftFileName)) using (CascadeClassifier eyeright = new CascadeClassifier(eyerightFileName)) { watch = Stopwatch.StartNew(); using (UMat ugray = new UMat()) { CvInvoke.CvtColor(image, ugray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray); //Cân bằng sáng của ảnh CvInvoke.EqualizeHist(ugray, ugray); //Phát hiện các khuôn mặt từ hình ảnh màu xám và lưu các vị trí làm hình chữ nhật // Chiều thứ nhất là kênh // Kích thước thứ hai là chỉ mục của hình chữ nhật trong kênh cụ thể Rectangle[] facesDetected = face.DetectMultiScale( ugray, 1.1, 10, new Size(20, 20)); faces.AddRange(facesDetected); foreach (Rectangle f in facesDetected) { //Sử dụng khu vực của khuôn mặt using (UMat faceRegion = new UMat(ugray, f)) { //tìm hình chữ nhật của mắt phải Rectangle[] eyesleftDetected = eyeleft.DetectMultiScale( faceRegion, 1.1, 10, new Size(20, 20)); foreach (Rectangle eleft in eyesleftDetected) { Rectangle eyeRectleft = eleft; eyeRectleft.Offset(f.X, f.Y); eyesleft.Add(eyeRectleft); } //tìm hình chữ nhật của mắt phải Rectangle[] eyesrightDetected = eyeright.DetectMultiScale( faceRegion, 1.1, 10, new Size(20, 20)); foreach (Rectangle eright in eyesrightDetected) { Rectangle eyeRectright = eright; eyeRectright.Offset(f.X, f.Y); eyesright.Add(eyeRectright); } } } } watch.Stop(); } } detectionTime = watch.ElapsedMilliseconds;//đo tổng thời gian trôi qua }