private static StoredFace StoredFace(FaceInfo dface) { TS.Core.Model.Rectangle faceRectangle = dface.FaceRectangle; return(new StoredFace() { FaceInfo = dface, XPos = Math.Max((int)(faceRectangle.X - faceRectangle.Width / 2.0), 0), YPos = Math.Max((int)(faceRectangle.Y - faceRectangle.Height / 2.0), 0), Width = (int)(faceRectangle.Width * 2.0), Height = (int)(faceRectangle.Height * 2.0) }); }
private static FaceFrame CropFaceToExtractor(StoredFace face, System.Drawing.Image frameImage) { StoredFace storedFace = face; FaceInfo faceInfo = face.FaceInfo; using (Bitmap image = new Bitmap(storedFace.Width, storedFace.Height)) { using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)image)) { graphics.InterpolationMode = InterpolationMode.High; graphics.DrawImage(frameImage, new RectangleF(0.0f, 0.0f, (float)storedFace.Width, (float)storedFace.Height), new RectangleF((float)storedFace.XPos, (float)storedFace.YPos, (float)storedFace.Width, (float)storedFace.Height), GraphicsUnit.Pixel); } using (Bitmap bmp = Grayscale.CommonAlgorithms.Y.Apply(image)) { TS.Core.Model.Rectangle faceRectangle = faceInfo.FaceRectangle; FaceFrame faceFrame1 = new FaceFrame(); faceFrame1.Frame = JpegCompression.ConvertToJpeg(bmp, 70); faceFrame1.FrameId = storedFace.FrameId; FaceFrame faceFrame2 = faceFrame1; FaceData faceData1 = new FaceData(); faceData1.DetectionProb = (float)faceInfo.DetectionProbability; faceData1.Width = (int)faceRectangle.Width; faceData1.Height = (int)faceRectangle.Height; faceData1.LeftX = (int)faceRectangle.X - storedFace.XPos; faceData1.LeftY = (int)faceRectangle.Y - storedFace.YPos; FaceData faceData2 = faceData1; TS.Core.Model.Point point = faceInfo.LeftEye; int num1 = (int)point.X - storedFace.XPos; faceData2.LeftEyeX = num1; FaceData faceData3 = faceData1; point = faceInfo.RightEye; int num2 = (int)point.X - storedFace.XPos; faceData3.RightEyeX = num2; FaceData faceData4 = faceData1; point = faceInfo.LeftEye; int num3 = (int)point.Y - storedFace.YPos; faceData4.LeftEyeY = num3; FaceData faceData5 = faceData1; point = faceInfo.RightEye; int num4 = (int)point.Y - storedFace.YPos; faceData5.RightEyeY = num4; FaceData faceData6 = faceData1; faceFrame2.Face = faceData6; return(faceFrame1); } } }
private void DetectFaces(Bitmap img, int index) { try { FaceInfo[] faceInfoArray = _faces[index].DetectAllFaces(img.ConvertFrom(), null); if (faceInfoArray.Length > 0) { try { for (int index1 = 0; index1 < faceInfoArray.Length; ++index1) { double num1 = Math.Abs(faceInfoArray[index1].PitchAngle); double num2 = Math.Abs(faceInfoArray[index1].YawAngle); if (faceInfoArray[index1].DetectionProbability >= _fquality && num1 <= 0.600000023841858 && num2 <= 0.800000011920929) { Rectangle faceRectangle = faceInfoArray[index1].FaceRectangle; System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle { X = (int)(faceRectangle.X - 0.4 * faceRectangle.Width), Y = (int)(faceRectangle.Y - 0.4 * faceRectangle.Height), Height = (int)(faceRectangle.Height + 0.8 * faceRectangle.Height), Width = (int)(faceRectangle.Width + 0.8 * faceRectangle.Width) }; if (srcRect.Y < 0) { srcRect.Y = 0; } if (srcRect.X + srcRect.Width > img.Width) { srcRect.Width = img.Width - srcRect.X; } if (faceRectangle.Y + srcRect.Height > img.Height) { srcRect.Height = img.Height - srcRect.Y; } using (Bitmap bitmap = new Bitmap(srcRect.Width, srcRect.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { try { graphics.DrawImage(img, new System.Drawing.Rectangle(0, 0, srcRect.Width, srcRect.Height), srcRect, GraphicsUnit.Pixel); Invoke(new AddImageFunc(AddImage), (object)bitmap); } catch { } } } } } } catch { } } img.Dispose(); try { _detectors[index] = 0; } catch { } } catch { try { _detectors[index] = 0; } catch { } try { img.Dispose(); } catch { } } }
public void SaveFace(string str, FaceRecognition rec) { try { BcFace bcFace = new BcFace(); string[] strArray = str.Replace(_currentDir + "\\", "").ToUpper().Replace(".JPEG", "").Replace(".JPG", "").Replace(".BMP", "").Replace(".GIF", "").Replace(".PNG", "").Replace(".TIFF", "").Split(new string[1] { " " }, StringSplitOptions.RemoveEmptyEntries); bcFace.Id = Guid.Empty; if (strArray.Length > 1) { bcFace.FirstName = strArray[1]; } if (strArray.Length > 0) { bcFace.Surname = strArray[0]; } if (strArray.Length > 2) { bcFace.LastName = strArray[2]; } Bitmap source = (Bitmap)Image.FromFile(str); TS.Sdk.StaticFace.Model.Image image = source.ConvertFrom(); BcKey bcKey = new BcKey(); FaceInfo face = rec.Engine.DetectMaxFace(image, null); if (face != null) { bcFace.Comment = CommonComment; bcFace.Sex = CommonSex; bcFace.AccessId = _selectedCategoryId; bcFace.EditUserId = MainForm.CurrentUser.Id; Rectangle faceRectangle = face.FaceRectangle; int width1 = (int)faceRectangle.Width * 2; faceRectangle = face.FaceRectangle; int height1 = (int)faceRectangle.Height * 2; Bitmap bitmap = new Bitmap(width1, height1); using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height); faceRectangle = face.FaceRectangle; double x1 = faceRectangle.X; faceRectangle = face.FaceRectangle; double num1 = faceRectangle.Width / 2.0; int x2 = (int)(x1 - num1); double num2 = source.Height; faceRectangle = face.FaceRectangle; double y1 = faceRectangle.Y; double num3 = num2 - y1; faceRectangle = face.FaceRectangle; double height2 = faceRectangle.Height; double num4 = num3 - height2; faceRectangle = face.FaceRectangle; double num5 = faceRectangle.Width / 2.0; int y2 = (int)(num4 - num5); int width2 = bitmap.Width; int height3 = bitmap.Height; graphics.DrawImage(source, new System.Drawing.Rectangle(0, 0, width2, height3), new System.Drawing.Rectangle(x2, y2, width2, height3), GraphicsUnit.Pixel); } MemoryStream memoryStream1 = new MemoryStream(); MemoryStream memoryStream2 = new MemoryStream(); bitmap.Save(memoryStream1, ImageFormat.Jpeg); BcImage bcImage = new BcImage(); if (bitmap.Width >= 300) { bitmap = new Bitmap(bitmap, new Size(bitmap.Width / 2, bitmap.Height / 2)); bitmap.Save(memoryStream2, ImageFormat.Jpeg); bcFace.ImageIcon = memoryStream2.GetBuffer(); bcImage.ImageIcon = memoryStream2.GetBuffer(); } else { bcFace.ImageIcon = memoryStream1.GetBuffer(); bcImage.ImageIcon = memoryStream1.GetBuffer(); } bcFace.Save(); bcImage.FaceId = bcFace.Id; bcImage.Image = memoryStream1.GetBuffer(); bcImage.FaceId = bcFace.Id; bcImage.Save(); bitmap.Dispose(); memoryStream1.Close(); memoryStream1.Dispose(); memoryStream2.Close(); memoryStream2.Dispose(); bcKey.ImageKey = rec.Engine.ExtractTemplate(image, face); if (bcKey.ImageKey != null) { bcKey.Ksid = -1; bcKey.ImageId = bcImage.Id; bcKey.FaceId = bcFace.Id; bcKey.Save(); } else { Errors.Add("Error to create template"); ErrorFiles.Add(str); } } else { Errors.Add(Messages.NoFaceWasFound); ErrorFiles.Add(str); } source.Dispose(); } catch (Exception ex) { Errors.Add(ex.Message); ErrorFiles.Add(str); } rec.IsWork = false; try { Invoke(new NewValueFileFunc(NewValueFile), (object)_progVal, (object)_mainIndex, (object)_commonFiles.Count); } catch { } }