private static FSDK.TPoint[] GetFeaturePoints(Image sourceImage) { FSDK.TPoint[] pointFeature = null; var image = new FSDK.CImage(new Bitmap(sourceImage)); var faceRectangle = Rectangle.Empty; var facePosition = image.DetectFace(); if (0 == facePosition.w) { return(pointFeature); } pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition); return(pointFeature); }
private void btnOpenPhoto_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { FSDK.CImage image = new FSDK.CImage(openFileDialog1.FileName); // resize image to fit the window width double ratio = System.Math.Min((pictureBox1.Width + 0.4) / image.Width, (pictureBox1.Height + 0.4) / image.Height); image = image.Resize(ratio); Image frameImage = image.ToCLRImage(); Graphics gr = Graphics.FromImage(frameImage); FSDK.TFacePosition facePosition = image.DetectFace(); if (0 == facePosition.w) { MessageBox.Show("No faces detected", "Face Detection"); } else { int left = facePosition.xc - (int)(facePosition.w * 0.6f); int top = facePosition.yc - (int)(facePosition.w * 0.5f); gr.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition.w * 1.2), (int)(facePosition.w * 1.2)); FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition); int i = 0; foreach (FSDK.TPoint point in facialFeatures) { gr.DrawEllipse((++i > 2) ? Pens.LightGreen : Pens.Blue, point.x, point.y, 3, 3); } gr.Flush(); } // display image pictureBox1.Image = frameImage; pictureBox1.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception"); } } }
private void picImg_Click(object sender, EventArgs e) { if (MessageBox.Show("Do You Want To Change This Pciture?", "Ïntrusion Detection", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } try { openFileDialog.InitialDirectory = @"C:\"; openFileDialog.RestoreDirectory = false; openFileDialog.Title = "Select Passport Photo"; openFileDialog.Filter = "Images Files (*.BMP;*.JPG;*.GIF,*.PNG,*.TIFF)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF"; openFileDialog.CheckFileExists = true; openFileDialog.CheckPathExists = true; // openFileDialog.ShowDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { try { FSDK.CImage image = new FSDK.CImage(openFileDialog.FileName); // resize image to fit the window width double ratio = System.Math.Min((picImg.Width + 0.4) / image.Width, (picImg.Height + 0.4) / image.Height); image = image.Resize(ratio); Image frameImage = image.ToCLRImage(); Graphics gr = Graphics.FromImage(frameImage); FSDK.TFacePosition facePosition = image.DetectFace(); if (0 == facePosition.w) { MessageBox.Show("No faces detected", "Face Detection"); } else { int left = facePosition.xc - (int)(facePosition.w * 0.6f); int top = facePosition.yc - (int)(facePosition.w * 0.5f); gr.DrawRectangle(Pens.LightGreen, left, top, (int)(facePosition.w * 1.2), (int)(facePosition.w * 1.2)); faceTemplate = new byte[FSDK.TemplateSize]; FSDK.GetFaceTemplateInRegion(image.ImageHandle, ref facePosition, out faceTemplate); //GetFaceTemplate(image, out templateData); FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition); int i = 0; foreach (FSDK.TPoint point in facialFeatures) { gr.DrawEllipse((++i > 2) ? Pens.LightGreen : Pens.Blue, point.x, point.y, 3, 3); } gr.Flush(); } // display image picImg.Image = frameImage; picImg.Refresh(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Exception"); } } //picImg.Image = Image.FromFile(openFileDialog.FileName); } catch (Exception q) { } }
public bool Recognize(ref string path, bool needCrop) { var AppDataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Abalone", "TestRotator"); FaceRectRelative = RectangleF.Empty; LeftEyeCenter = RightEyeCenter = LeftMouth = LeftNose = RightNose = RightMouth = Vector2.Zero; var executablePath = Path.GetDirectoryName(Application.ExecutablePath); FSDK.TPoint[] pointFeature; var image = new FSDK.CImage(path); var faceRectangle = Rectangle.Empty; var mouthRectangle = Rectangle.Empty; FSDK.SetFaceDetectionThreshold(5); FSDK.SetFaceDetectionParameters(true, true, 512); var facePosition = image.DetectFace(); if (0 == facePosition.w) { MessageBox.Show("No faces detected", "Face Detection"); return(false); } if (needCrop) { RotatedAngle = facePosition.angle; // угол, на который повернута голова. } pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition); var left = facePosition.xc - (int)(facePosition.w * 0.6f); left = left < 0 ? 0 : left; // int top = facePosition.yc - (int)(facePosition.w * 0.5f); // верхушку определяет неправильлно. поэтому просто не будем обрезать :) BottomFace = new Vector2(pointFeature[11].x, pointFeature[11].y); var distance = pointFeature[2].y - pointFeature[11].y; var top = pointFeature[16].y + distance - 15; // определение высоты по алгоритму старикана top = top < 0 ? 0 : top; var newWidth = (int)(facePosition.w * 1.2); newWidth = newWidth > image.Width || newWidth == 0 ? image.Width : newWidth; faceRectangle = new Rectangle(left, top, newWidth, BottomFace.Y + 15 < image.Height ? (int)(BottomFace.Y + 15) - top : image.Height - top - 1); if (needCrop) // если это создание проекта - то нужно обрезать фотку и оставить только голову { var bmpImage = new Bitmap(path); using (var croppedImage = ImageEx.Crop(bmpImage, faceRectangle)) { path = AppDataDir; FolderEx.CreateDirectory(path); path = Path.Combine(path, "tempHaarImage.jpg"); croppedImage.Save(path, ImageFormat.Jpeg); return(Recognize(ref path, false)); } } LeftEyeCenter = new Vector2(pointFeature[0].x, pointFeature[0].y); RightEyeCenter = new Vector2(pointFeature[1].x, pointFeature[1].y); LeftMouth = new Vector2(pointFeature[3].x, pointFeature[3].y); RightMouth = new Vector2(pointFeature[4].x, pointFeature[4].y); LeftNose = new Vector2(pointFeature[45].x, pointFeature[45].y); RightNose = new Vector2(pointFeature[46].x, pointFeature[46].y); TopFace = new Vector2(pointFeature[66].x, pointFeature[66].y); MiddleFace1 = new Vector2(pointFeature[66].x, pointFeature[66].y); MiddleFace2 = new Vector2(pointFeature[5].x, pointFeature[5].y); RightMiddleFace1 = new Vector2(pointFeature[67].x, pointFeature[67].y); RightMiddleFace2 = new Vector2(pointFeature[6].x, pointFeature[6].y); #region Поворот фотки по глазам! var v = new Vector2(LeftEyeCenter.X - RightEyeCenter.X, LeftEyeCenter.Y - RightEyeCenter.Y); v.Normalize(); // ПД ! var xVector = new Vector2(1, 0); var xDiff = xVector.X - v.X; var yDiff = xVector.Y - v.Y; var angle = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI; if (Math.Abs(angle) > 1 && angleCount <= 5) // поворачиваем наклоненные головы { ++angleCount; using (var ms = new MemoryStream(File.ReadAllBytes(path))) // Don't use using!! { var originalImg = (Bitmap)Image.FromStream(ms); path = AppDataDir; FolderEx.CreateDirectory(path); path = Path.Combine(path, "tempHaarImage.jpg"); using (var ii = ImageEx.RotateImage(new Bitmap(originalImg), (float)-angle)) ii.Save(path, ImageFormat.Jpeg); } return(Recognize(ref path, false)); } #endregion var upperUpperLip = pointFeature[54]; // вехняя точка верхней губы var lowerUpperLip = pointFeature[61]; // нижняя точка верхней губы var lowerLip = pointFeature[64]; // верхняя точка нижней губы var diff2 = Math.Abs(lowerUpperLip.y - upperUpperLip.y); var diffX = Math.Abs(lowerLip.y - lowerUpperLip.y); IsOpenSmile = diffX > diff2; // экспериментальным путем выяснено, что улыбка на 90% открытая, если расстояние между верхней и нижней губой больше, чем толщина верхней губы #region Переводим в относительные координаты LeftMouth = new Vector2(LeftMouth.X / (image.Width * 1f), LeftMouth.Y / (image.Height * 1f)); RightMouth = new Vector2(RightMouth.X / (image.Width * 1f), RightMouth.Y / (image.Height * 1f)); LeftEyeCenter = new Vector2(LeftEyeCenter.X / (image.Width * 1f), LeftEyeCenter.Y / (image.Height * 1f)); RightEyeCenter = new Vector2(RightEyeCenter.X / (image.Width * 1f), RightEyeCenter.Y / (image.Height * 1f)); LeftNose = new Vector2(LeftNose.X / (image.Width * 1f), LeftNose.Y / (image.Height * 1f)); RightNose = new Vector2(RightNose.X / (image.Width * 1f), RightNose.Y / (image.Height * 1f)); TopFace = new Vector2(TopFace.X / (image.Width * 1f), TopFace.Y / (image.Height * 1f)); MiddleFace1 = new Vector2(MiddleFace1.X / (image.Width * 1f), MiddleFace1.Y / (image.Height * 1f)); MiddleFace2 = new Vector2(MiddleFace2.X / (image.Width * 1f), MiddleFace2.Y / (image.Height * 1f)); BottomFace = new Vector2(BottomFace.X / (image.Width * 1f), BottomFace.Y / (image.Height * 1f)); RightMiddleFace1 = new Vector2(RightMiddleFace1.X / (image.Width * 1f), RightMiddleFace1.Y / (image.Height * 1f)); RightMiddleFace2 = new Vector2(RightMiddleFace2.X / (image.Width * 1f), RightMiddleFace2.Y / (image.Height * 1f)); FacialFeatures = new List <Vector3>(); RealPoints = new List <Vector2>(); int index = 0; var pointDepths = GetPointDepths(); foreach (var point in pointFeature) { FacialFeatures.Add(new Vector3(point.x / (image.Width * 1f), point.y / (image.Height * 1f), pointDepths[index++])); RealPoints.Add(new Vector2(point.x, point.y)); } ImageWidth = image.Width; ImageHeight = image.Height; #endregion return(true); }
private static void CropImage(Image sourceImage, string imageName, bool needCrop = true, int angleCount = 0) { FSDK.TPoint[] pointFeature; var image = new FSDK.CImage(new Bitmap(sourceImage)); var faceRectangle = Rectangle.Empty; var facePosition = image.DetectFace(); if (0 == facePosition.w) { SaveToFTP(sourceImage, imageName); return; } pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition); var left = facePosition.xc - (int)(facePosition.w * 0.6f); left = left < 0 ? 0 : left; // int top = facePosition.yc - (int)(facePosition.w * 0.5f); // верхушку определяет неправильлно. поэтому просто не будем обрезать :) var BottomFace = new Vector2(pointFeature[11].x, pointFeature[11].y); var distance = pointFeature[2].y - pointFeature[11].y; var top = pointFeature[16].y + distance - 15; // определение высоты по алгоритму старикана top = top < 0 ? 0 : top; var newWidth = (int)(facePosition.w * 1.2); newWidth = newWidth > image.Width || newWidth == 0 ? image.Width : newWidth; faceRectangle = new Rectangle(left, top, newWidth, BottomFace.Y + 15 < image.Height ? (int)(BottomFace.Y + 15) - top : image.Height - top - 1); if (needCrop) { sourceImage = ImageEx.Crop(new Bitmap(sourceImage), faceRectangle); } // по новой картинке еще раз распознаемм все image = new FSDK.CImage(new Bitmap(sourceImage)); facePosition = image.DetectFace(); if (0 == facePosition.w) { SaveToFTP(sourceImage, imageName); return; } pointFeature = image.DetectFacialFeaturesInRegion(ref facePosition); var LeftEyeCenter = new Vector2(pointFeature[0].x, pointFeature[0].y); var RightEyeCenter = new Vector2(pointFeature[1].x, pointFeature[1].y); #region Поворот фотки по глазам! var v = new Vector2(LeftEyeCenter.X - RightEyeCenter.X, LeftEyeCenter.Y - RightEyeCenter.Y); v.Normalize(); // ПД ! var xVector = new Vector2(1, 0); var xDiff = xVector.X - v.X; var yDiff = xVector.Y - v.Y; var angle = Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI; if (Math.Abs(angle) > 1 && angleCount <= 5) // поворачиваем наклоненные головы { ++angleCount; sourceImage = ImageEx.RotateImage(new Bitmap(sourceImage), (float)-angle); CropImage(sourceImage, imageName, false, angleCount); return; } #endregion #region Корректируем размер фотки const int selectedSize = 1024; // вызывается уже при создании проекта var max = (float)Math.Max(sourceImage.Width, sourceImage.Height); if (max != selectedSize) { var k = selectedSize / max; sourceImage = ImageEx.ResizeImage(sourceImage, new Size((int)Math.Round(sourceImage.Width * k), (int)Math.Round((sourceImage.Height * k)))); } #endregion SaveToFTP(sourceImage, imageName); }
public void proses() { Pen p1 = new Pen(Color.LightGreen, 2); OpenFileDialog open = new OpenFileDialog(); open.Filter = "Image Files(*.jpg; *bmp; *.png; *.tiff; *.gif)| *.jpg; *.bmp; *.png; *.tiff; *.gif"; if (open.ShowDialog() == DialogResult.OK) { } FSDK.CImage image = new FSDK.CImage(open.FileName); double ratio = System.Math.Min((pictureBox.Width + 0.4) / image.Width, pictureBox.Height + 0.4 / image.Height); image = image.Resize(ratio); Image frameImage = image.ToCLRImage(); Graphics graphicFromImage = Graphics.FromImage(frameImage); pictureBox.Image = new Bitmap(frameImage); FSDK.TFacePosition facePosition = image.DetectFace(); FSDK.TPoint[] facialFeatures = image.DetectFacialFeaturesInRegion(ref facePosition); if (0 == facePosition.w) { MessageBox.Show("No Face detected", "Face Detected"); } else { left = facePosition.xc - facePosition.w / 2; top = facePosition.yc - facePosition.w / 2; width = facePosition.w; height = facePosition.w + 50; graphicFromImage.DrawRectangle(p1, left, top, width, height); int i = 0; foreach (FSDK.TPoint point in facialFeatures) { { graphicFromImage.DrawEllipse((++i > 3) ? Pens.Yellow : Pens.Blue, point.x, point.y, 3, 3); } // richTextBoxForPicture.Text += i.ToString() + "." + point.x.ToString() + ", " + point.y.ToString() + Environment.NewLine; } graphicFromImage.Flush(); pictureBox.Image = frameImage; pictureBox.Refresh(); /// ========================================================================================== /// /* * FSDK.CImage croppedImage = image.CopyRect(left, top, left + width - 1, top + height - 1); * pictureBoxCropped.Image = croppedImage.ToCLRImage(); * pictureBoxCropped.SizeMode = PictureBoxSizeMode.Zoom; * pictureBoxCropped.Refresh(); * * int a = 30; * * FSDK.CImage eyeImage = image.CopyRect(facialFeatures[0].x-a, facialFeatures[0].y - a, facialFeatures[0].x + a, facialFeatures[0].y + a); * pictureBox1.Image = eyeImage.ToCLRImage(); * pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox1.Refresh(); * * FSDK.CImage eyeRImage = image.CopyRect(facialFeatures[1].x - a, facialFeatures[1].y - a, facialFeatures[1].x + a, facialFeatures[1].y + a); * pictureBox2.Image = eyeRImage.ToCLRImage(); * pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox2.Refresh(); * * FSDK.CImage noseImage = image.CopyRect(facialFeatures[2].x - a, facialFeatures[2].y - a, facialFeatures[2].x + a, facialFeatures[2].y + a); * pictureBox3.Image = noseImage.ToCLRImage(); * pictureBox3.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox3.Refresh(); * * FSDK.CImage uppermouthImage = image.CopyRect(facialFeatures[64].x - a, facialFeatures[64].y - a, facialFeatures[64].x + a, facialFeatures[64].y); * pictureBox6.Image = uppermouthImage.ToCLRImage(); * pictureBox6.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox6.Refresh(); * * FSDK.CImage lowmouthImage = image.CopyRect(facialFeatures[64].x - a, facialFeatures[64].y , facialFeatures[64].x + a, facialFeatures[64].y + a); * pictureBox4.Image = lowmouthImage.ToCLRImage(); * pictureBox4.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox4.Refresh(); * * FSDK.CImage dagu = image.CopyRect(facialFeatures[11].x - (a+20), facialFeatures[11].y - a, facialFeatures[11].x + a + 20, facialFeatures[11].y + a); * pictureBox5.Image = dagu.ToCLRImage(); * pictureBox5.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox5.Refresh(); * * FSDK.CImage nasolabialright = image.CopyRect(facialFeatures[53].x , facialFeatures[53].y - a, facialFeatures[53].x + a, facialFeatures[53].y + a); * pictureBox7.Image = nasolabialright.ToCLRImage(); * pictureBox7.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox7.Refresh(); * * FSDK.CImage nasolabialleft = image.CopyRect(facialFeatures[52].x - (a), facialFeatures[52].y - a, facialFeatures[52].x, facialFeatures[52].y + a); * pictureBox8.Image = nasolabialleft.ToCLRImage(); * pictureBox8.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox8.Refresh(); * * FSDK.CImage eyebrowleft = image.CopyRect(facialFeatures[19].x - a, facialFeatures[19].y - a, facialFeatures[19].x + a, facialFeatures[19].y+10); * pictureBox9.Image = eyebrowleft.ToCLRImage(); * pictureBox9.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox9.Refresh(); * * FSDK.CImage eyebrowright = image.CopyRect(facialFeatures[20].x - a, facialFeatures[20].y - a, facialFeatures[20].x + a, facialFeatures[20].y+10); * pictureBox10.Image = eyebrowright.ToCLRImage(); * pictureBox10.SizeMode = PictureBoxSizeMode.Zoom; * pictureBox10.Refresh(); */ } pictureBox.SizeMode = PictureBoxSizeMode.Normal; pictureBox1.SizeMode = PictureBoxSizeMode.Normal; /// Gambar 2 /// ========================================================================================= if (open.ShowDialog() == DialogResult.OK) { FSDK.CImage img = new FSDK.CImage(open.FileName); double rat = System.Math.Min((pictureBox1.Width + 0.4) / img.Width, pictureBox1.Height + 0.4 / img.Height); img = img.Resize(rat); Image frameI = img.ToCLRImage(); Graphics graphic = Graphics.FromImage(frameI); pictureBox1.Image = new Bitmap(frameI); FSDK.TFacePosition facePos = img.DetectFace(); FSDK.TPoint[] facialF = img.DetectFacialFeaturesInRegion(ref facePos); if (0 == facePos.w) { MessageBox.Show("No Face detected", "Face Detected"); } else { left = facePos.xc - facePos.w / 2; top = facePos.yc - facePos.w / 2; width = facePos.w; height = facePos.w + 50; graphic.DrawRectangle(p1, left, top, width, height); int i = 0; double[] temp = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; foreach (FSDK.TPoint point in facialF) { { graphic.DrawEllipse((++i > 3) ? Pens.Yellow : Pens.Blue, point.x, point.y, 3, 3); } if (i == 12 || i == 18 || i == 16 || i == 19 || i == 13)// Left eyebrow { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[0] += Math.Sqrt((x * x) + (y * y)); } else if (i == 14 || i == 20 || i == 17 || i == 21 || i == 15)// Right eyebrow { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[1] += Math.Sqrt((x * x) + (y * y)); } else if (i == 23 || i == 35 || i == 28 || i == 36 || i == 24 || i == 38 || i == 27 || i == 37)// Left eye { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[2] += Math.Sqrt((x * x) + (y * y)); } else if (i == 30 || i == 32 || i == 40 || i == 26 || i == 42 || i == 31 || i == 41 || i == 25)// Right eye { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[3] += Math.Sqrt((x * x) + (y * y)); } else if (i == 22 || i == 43 || i == 45 || i == 47 || i == 49 || i == 48 || i == 46 || i == 44)// Nose { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[4] += Math.Sqrt((x * x) + (y * y)); } else if (i == 50 || i == 52) //Left Cheek { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[5] += Math.Sqrt((x * x) + (y * y)); } else if (i == 51 || i == 53)//Right Cheek { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[9] += Math.Sqrt((x * x) + (y * y)); } else if (i == 56 || i == 54 || i == 57 || i == 61 || i == 60 || i == 62)// upper mouth 61 60 62 { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[6] += Math.Sqrt((x * x) + (y * y)); } else if (i == 58 || i == 55 || i == 59 || i == 64 || i == 63 || i == 65)// lower mouth 64 63 65 { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[7] += Math.Sqrt((x * x) + (y * y)); } else if (i == 3 || i == 4)// mouth { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[6] += Math.Sqrt((x * x) + (y * y)); temp[7] += Math.Sqrt((x * x) + (y * y)); } else if (i == 5 || i == 7 || i == 9 || i == 11 || i == 10 || i == 8 || i == 6)// chin { int x = point.x - facialFeatures[i - 1].x; int y = point.y - facialFeatures[i - 1].y; temp[8] += Math.Sqrt((x * x) + (y * y)); } // richTextBoxForPicture.Text += i.ToString() + "." + point.x.ToString() + ", " + point.y.ToString() + Environment.NewLine; } graphic.Flush(); eucLEyeB.Text = temp[0].ToString(); eucREyeB.Text = temp[1].ToString(); eucLEye.Text = temp[2].ToString(); eucREye.Text = temp[3].ToString(); eucnose.Text = temp[4].ToString(); eucpipi.Text = temp[5].ToString(); eucUmouth.Text = temp[6].ToString(); eucLmouth.Text = temp[7].ToString(); eucchin.Text = temp[8].ToString(); Rcheck.Text = temp[9].ToString(); double avg = 0; avg = (temp[0] + temp[1] + temp[2] + temp[3] + temp[4] + temp[5] + temp[6] + temp[7] + temp[8] + temp[9]) / 10; avgbox.Text = avg.ToString(); } pictureBox1.Image = frameI; pictureBox1.Refresh(); } }
private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; int cameraHandle = 0; int r = FSDKCam.OpenVideoCamera(ref cameraName, ref cameraHandle); if (r != FSDK.FSDKE_OK) { MessageBox.Show("Error opening the first camera", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } // set realtime face detection parameters FSDK.SetFaceDetectionParameters(false, false, 100); FSDK.SetFaceDetectionThreshold(3); // list where we store face templates // faceTemplates = new List(); faceTemplates = new List <FaceTemplate>(); while (!needClose) { // faceTemplates.Clear(); Int32 imageHandle = 0; if (FSDK.FSDKE_OK != FSDKCam.GrabFrame(cameraHandle, ref imageHandle)) // grab the current frame from the camera { Application.DoEvents(); continue; } FSDK.CImage image = new FSDK.CImage(imageHandle); Image frameImage = image.ToCLRImage(); Graphics gr = Graphics.FromImage(frameImage); FSDK.TFacePosition facePosition = image.DetectFace(); // if a face is detected, we can recognize it if (facePosition.w != 0) { gr.DrawRectangle(Pens.LightGreen, facePosition.xc - facePosition.w / 2, facePosition.yc - facePosition.w / 2, facePosition.w, facePosition.w); // create a new face template FaceTemplate template = new FaceTemplate(); template.templateData = new byte[FSDK.TemplateSize]; FaceTemplate template1 = new FaceTemplate(); if (programState == ProgramState.psRemember || programState == ProgramState.psRecognize) { template.templateData = image.GetFaceTemplateInRegion(ref facePosition); } switch (programState) { case ProgramState.psNormal: // normal state - do nothing break; case ProgramState.psRemember: // Remember Me state - store facial templates label1.Text = "Templates stored: " + faceTemplates.Count.ToString(); faceTemplates.Add(template); if (faceTemplates.Count > 9) { // get the user name InputName inputName = new InputName(); inputName.ShowDialog(); userName = inputName.userName; cmd = new SqlCommand("insert into facetb values(@Name,@face)", con); cmd.Parameters.AddWithValue("@Name", userName); cmd.Parameters.AddWithValue("@face", template.templateData); con.Open(); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Save!"); programState = ProgramState.psRecognize; } break; case ProgramState.psRecognize: // recognize the user bool match = false; con.Open(); cmd = new SqlCommand("select * from facetb ORDER BY id ASC ", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { template1.templateData = (byte[])dr["face"]; faceTemplates.Add(template1); strList.Add(dr["Name"].ToString()); } con.Close(); int ii = 0; foreach (FaceTemplate t in faceTemplates) { float similarity = 0.0f; FaceTemplate t1 = t; FSDK.MatchFaces(ref template.templateData, ref t1.templateData, ref similarity); float threshold = 0.0f; FSDK.GetMatchingThresholdAtFAR(0.01f, ref threshold); // set FAR to 1% if (similarity > threshold) { userName = strList[ii].ToString(); label3.Text = strList[ii].ToString(); match = true; break; } ii++; } con.Close(); if (match) { StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; gr.DrawString(userName, new System.Drawing.Font("Arial", 16), new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen), facePosition.xc, facePosition.yc + facePosition.w * 0.55f, format); // abc = 0; send(); } else { abc = 0; label3.Text = "UnKnow FACE"; } break; } } // display current frame pictureBox1.Image = frameImage; GC.Collect(); // collect the garbage after the deletion // make UI controls accessible Application.DoEvents(); } FSDKCam.CloseVideoCamera(cameraHandle); FSDKCam.FinalizeCapturing(); }