private void btnPredict_Click(object sender, EventArgs e) { Camera.Retrieve(Frame); var ImgFrame = Frame.ToImage <Gray, byte>(); if (ImgFrame != null) { var faces = FaceDetection.DetectMultiScale(ImgFrame, 1.3, 5); if (faces.Count() != 0) { var processedImg = ImgFrame.Copy(faces[0]).Resize(ProcessedImageWidth, ProcessedImageHeight, Emgu.CV.CvEnum.Inter.Cubic); var result = FacialRecognition.Predict(processedImg); if (result.Label.ToString() == "15") { MessageBox.Show("FESTUS"); } else { MessageBox.Show("Test Person"); } } } }
private void Timer_Tick(object sender, EventArgs e) { Camera.Retrieve(Frame); var ImgFrame = Frame.ToImage <Gray, byte>(); if (TimerCounter < TimeLimit) { TimerCounter++; if (ImgFrame != null) { var faces = FaceDetection.DetectMultiScale(ImgFrame, 1.3, 5); if (faces.Count() > 0) { var processedImage = ImgFrame.Copy(faces[0]).Resize(ProcessedImageWidth, ProcessedImageHeight, Emgu.CV.CvEnum.Inter.Cubic); Faces.Add(processedImage); Ids.Add(Convert.ToInt32(IdBox.Text)); ScanCounter++; OutputBox.AppendText($"{ScanCounter} Succesful Scans Taken...{Environment.NewLine}"); OutputBox.ScrollToCaret(); } } } else { FacialRecognition.Train(Faces.ToArray(), Ids.ToArray()); FacialRecognition.Write(YMLPath); Timer.Stop(); TimerCounter = 0; btnTrain.Enabled = !btnTrain.Enabled; IdBox.Enabled = !IdBox.Enabled; OutputBox.AppendText($"Training Complete! {Environment.NewLine}"); MessageBox.Show("Training Complete"); } }
private List <Face> GetNewFacesToRecognize(FacialRecognition recognizer, long thumbnailId, double maxDistance, double minWidth) { Logger.Trace($"GetNewFacesToRecognize started for {thumbnailId} with {maxDistance} distance and {minWidth} width."); var start = DateTime.Now; try { var thumbnail = Context.Thumbnails .Include(t => t.FileSystemFolder) .Include(t => t.Faces) .First(t => t.Id == thumbnailId); using (var img = Image.Load(Helpers.GetFullPath(thumbnail))) { Helpers.ExifRotate(img); using (var imageStream = new MemoryStream()) { img.SaveAsBmp(imageStream); Logger.Trace($"Scanning image {thumbnail.FileName} for new faces"); var foundRectangles = thumbnail.Faces.Select(f => f.Rectangle) .ToList(); var faces = recognizer.Recognize(foundRectangles, imageStream); var sideFaces = recognizer.Recognize(foundRectangles, imageStream, FacialRecognition.HaarCascadeSideProfile); using (var flippedStream = new MemoryStream()) { img.Mutate(x => x.RotateFlip(RotateMode.None, FlipMode.Horizontal)); img.SaveAsBmp(flippedStream); var flipped = foundRectangles.Select(k => new System.Drawing.Rectangle(k.X + k.Width + thumbnail.ImageWidth, k.Y, k.Width, k.Height)).ToList(); var leftSideFaces = recognizer.Recognize(flipped, flippedStream, FacialRecognition.HaarCascadeSideProfile); foreach (var face in leftSideFaces) { //need to flip the images back. face.RectX = thumbnail.ImageWidth - face.RectWidth - face.RectX; } Logger.Trace($"Found {leftSideFaces.Count} flipped faces. {sideFaces.Count}"); faces.AddRange(sideFaces); faces.AddRange(leftSideFaces); } var filtered = new List <Face>(); foreach (var face in faces) { if ((face.Distance <= maxDistance || face.RectWidth > minWidth || face.RectHeight > minWidth) && !filtered.Any(f => f.Rectangle.Contains(face.Rectangle) || face.Rectangle.Contains(f.Rectangle) || Helpers.Overlaps(f.Rectangle, face.Rectangle, 70))) { filtered.Add(face); face.Thumbnail = thumbnail; } } var newFaces = filtered.Where(f => !thumbnail.Faces.Any(i => f.Rectangle.Contains(i.Rectangle) || i.Rectangle.Contains(f.Rectangle) || (i.Rectangle.IntersectsWith(f.Rectangle) && Helpers.Overlaps(i.Rectangle, f.Rectangle, 70)) ) ).ToList(); if (newFaces.Count != filtered.Count) { Logger.Trace($"Removed {filtered.Count - newFaces.Count} faces due to already existing"); } foreach (var face in newFaces) { var filePath = Path.Combine(Configuration["ImageProcessing:FacesToLocalPath"], "Dynamic"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } using (var tmp = Image.Load(new MemoryStream(face.Bytes))) { tmp.Save(Path.Combine(filePath, $"{face.TagId}_{Guid.NewGuid():N}.jpg")); } } return(newFaces); } } } catch (Exception ex) { Logger.Error(ex, $"Failed to get new faces: {ex.Message}\n:{ex}"); return(new List <Face>()); } finally { Logger.Trace($"GetNewFacesToRecognize completed for {thumbnailId} in {(DateTime.Now - start).Milliseconds} ms."); } }
private void Awake() { instance = this; }