private void ProcessFrame(object sender, EventArgs e) { if (capture != null) { using (Mat frame = new Mat()) { capture.Retrieve(frame, 0); IEnumerable <IReadOnlyDictionary <string, int> > faces = faceDetection.Detect(frame); IEnumerable <Rectangle> facesRectangle = faces.Select(face => new Rectangle(face["top"], face["left"], face["width"], face["height"])); foreach (Rectangle faceRectangle in facesRectangle) { Mat face = FaceDetector.GetFaceImage(frame, faceRectangle); bool result = faceMaskDetector.Detect(BitmapExtension.ToBitmap(face)); if (result) { CvInvoke.Rectangle(frame, faceRectangle, new Bgr(Color.Green).MCvScalar, 2); } else { CvInvoke.Rectangle(frame, faceRectangle, new Bgr(Color.Yellow).MCvScalar, 2); } } imageBox1.Image = frame.ToImage <Bgr, byte>(); } } }
public static IEnumerable <FaceModel> FaceAnalysis(FaceMaskDetector faceMaskDetector, byte[] imageData, IEnumerable <FaceRectangle> faceRectangles, bool mask) { if (mask) { IEnumerable <Rectangle> rectangles = faceRectangles.Select(faceRectangle => new Rectangle(faceRectangle.Top, faceRectangle.Left, faceRectangle.Width, faceRectangle.Height)); IEnumerable <Bitmap> faces = rectangles.Select(rectangle => FaceDetector.GetFaceImage(imageData, rectangle).ToBitmap()); IEnumerable <bool> isMasks = faceMaskDetector.Detect(faces); return(faceRectangles.Zip(isMasks, (faceRectangle, isMask) => new FaceModel { FaceRectangle = faceRectangle, FaceAttributes = new FaceAttributes { IsMask = isMask } })); } return(faceRectangles.Select(faceRectangle => new FaceModel { FaceRectangle = faceRectangle, FaceAttributes = new FaceAttributes() })); }