public void FaceppThread( ) { facesDetected = smallGrayFrame.DetectHaarCascade( haar, 1.1, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20))[0]; int NUM = 0; for (int i = 0; i < facesDetected.Length; i++) { MCvAvgComp f = facesDetected[i]; DETECTED = true; this.smallGrayFrame.ROI = f.rect; Image <Gray, Byte> roiImage = new Image <Gray, Byte> (f.rect.Size); this.smallGrayFrame.Copy(roiImage, smallGrayFrame); fsFileName = "DECT.jpg"; roiImage.Save(fsFileName); DetectResult res_ = fs.Detection_DetectImg(this.smallGrayFrame); DetectResult res = fs.Detection_DetectImg(System.Environment.CurrentDirectory + "\\" + fsFileName); if (res.face.Count > 0) { age[NUM] = res.face[0].attribute.age; gender[NUM] = res.face[0].attribute.gender; NUM++; } } if (DETECTED == true) { processingData( ); } }
private void button1_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "选择文件"; openFileDialog.Filter = "jpg文件|*.jpg|png文件|*.png|所有文件|*.*"; openFileDialog.FileName = string.Empty; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; openFileDialog.DefaultExt = "jpg"; DialogResult result = openFileDialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { return; } String filepath = openFileDialog.FileName; BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri(filepath); bitmap.DecodePixelHeight = (int)image1.Height; bitmap.DecodePixelWidth = (int)image1.Width; bitmap.EndInit(); image1.Source = bitmap; PngBitmapEncoder pngE = new PngBitmapEncoder(); pngE.Frames.Add(BitmapFrame.Create(bitmap)); using (Stream stream = File.Create(System.IO.Path.Combine(System.Environment.CurrentDirectory, "temp.jpg"))) { pngE.Save(stream); } DetectResult res = fs.Detection_DetectImg(System.IO.Path.Combine(System.Environment.CurrentDirectory, "temp.jpg")); canvas1.Children.Clear(); for (int i = 0; i < res.face.Count; ++i) { RectangleGeometry rect = new RectangleGeometry(); rect.Rect = new Rect(max(res.face[i].position.center.x * image1.Width / 100.0 - res.face[i].position.width * image1.Width / 200.0, 0), max(res.face[i].position.center.y * image1.Height / 100.0 - res.face[i].position.height * image1.Height / 200.0, 0), res.face[i].position.width * image1.Width / 100.0, res.face[i].position.height * image1.Height / 100.0); System.Windows.Shapes.Path myPath = new System.Windows.Shapes.Path(); myPath.Stroke = Brushes.Red; myPath.StrokeThickness = 3; myPath.Data = rect; label1.Content = label1.Content + String.Format("({0:F2},{1:F2})", res.face[0].position.center.x, res.face[0].position.center.y); label2.Content = label2.Content + String.Format("({0:F2},{1:F2})", res.face[0].position.width, res.face[0].position.height); canvas1.Children.Add(myPath); } }
private void RunCamera(string stage) { if (videoSource == null) { videoSource = new VideoCaptureDevice(); } if (videoSource.IsRunning) { videoSource.SignalToStop(); videoSource.NewFrame -= videoSource_NewFrame; videoSource = null; PngBitmapEncoder pngE = new PngBitmapEncoder(); pngE.Frames.Add(BitmapFrame.Create((BitmapSource)personImage.Source)); using (Stream stream = File.Create(System.Environment.CurrentDirectory + "/temp.jpg")) { pngE.Save(stream); } DetectResult detectResult = fs.Detection_DetectImg(System.Environment.CurrentDirectory + "/temp.jpg"); File.Delete(System.Environment.CurrentDirectory + "/temp.jpg"); if (stage == "Default") { DefaultStage(detectResult); camera.Content = "Run Camera"; } else { AddFaceStage(detectResult); cameraTab.Content = "Run Camera"; } //videoSource.Stop(); } else { videoSource = new VideoCaptureDevice(videoDevices[deviceComboBox.SelectedIndex].MonikerString); videoSource.NewFrame += videoSource_NewFrame; videoSource.Start(); if (stage == "Default") { camera.Content = "Detect"; } else { cameraTab.Content = "Detect"; } } }
static void Main(string[] args) { String apiKey = "847e6315f892e21449da5f4077c5104f"; String apiSecret = "BmskojfFyrZVQhkLfNSnRzX-lK8musO6"; FaceService faceService = new FaceService(apiKey, apiSecret); string filePath = "D:\\Codes\\datasets\\face_morph\\bbt.jpg"; DetectResult detectResult = faceService.Detection_DetectImg(filePath); Image <Bgr, Byte> srcImg = new Image <Bgr, Byte>(filePath); for (int cnt = 0; cnt < detectResult.face.Count; cnt++) { string pointFileName = String.Format("D:\\Codes\\datasets\\face_morph\\result_bbt_face_{0}.txt", cnt); FileStream fileStream = new FileStream(pointFileName, FileMode.Create); StreamWriter streamWriter = new StreamWriter(fileStream); Rectangle faceRect = new Rectangle( (int)(detectResult.face[cnt].position.center.x * srcImg.Width / 100 - detectResult.face[cnt].position.width * srcImg.Width * 0.5 / 100), (int)(detectResult.face[cnt].position.center.y * srcImg.Height / 100 - detectResult.face[cnt].position.height * srcImg.Height * 0.5 / 100), (int)detectResult.face[cnt].position.width * srcImg.Width / 100, (int)detectResult.face[cnt].position.height * srcImg.Height / 100); Image <Bgr, byte> faceImg = srcImg.GetSubRect(faceRect); string fileName = String.Format("D:\\Codes\\datasets\\face_morph\\result_bbt_face_{0}.jpg", cnt); faceImg.Save(fileName); IList <FaceppSDK.Point> featurePoints = new List <FaceppSDK.Point>(); //featurePoints.Add(detectResult.face[cnt].position.center); FaceppSDK.Point tempPoint1 = new FaceppSDK.Point(); tempPoint1.x = (detectResult.face[cnt].position.eye_left.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint1.y = (detectResult.face[cnt].position.eye_left.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint1); FaceppSDK.Point tempPoint2 = new FaceppSDK.Point(); tempPoint2.x = (detectResult.face[cnt].position.eye_right.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint2.y = (detectResult.face[cnt].position.eye_right.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint2); FaceppSDK.Point tempPoint3 = new FaceppSDK.Point(); tempPoint3.x = (detectResult.face[cnt].position.mouth_left.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint3.y = (detectResult.face[cnt].position.mouth_left.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint3); FaceppSDK.Point tempPoint4 = new FaceppSDK.Point(); tempPoint4.x = (detectResult.face[cnt].position.mouth_right.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint4.y = (detectResult.face[cnt].position.mouth_right.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint4); FaceppSDK.Point tempPoint5 = new FaceppSDK.Point(); tempPoint5.x = (detectResult.face[cnt].position.nose.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint5.y = (detectResult.face[cnt].position.nose.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint5); foreach (FaceppSDK.Point featurePoint in featurePoints) { streamWriter.WriteLine(featurePoint.x.ToString()); streamWriter.WriteLine(featurePoint.y.ToString()); System.Drawing.PointF point = new System.Drawing.PointF((float)featurePoint.x * srcImg.Width / 100, (float)featurePoint.y * srcImg.Height / 100); Cross2DF cross = new Cross2DF(point, (float)3.0, (float)3.0); srcImg.Draw(cross, new Bgr(0, 255, 0), 3); } streamWriter.Flush(); streamWriter.Close(); fileStream.Close(); //srcImg.Save("D:\\Codes\\datasets\\face_morph\\result_bbt.jpg"); } }
//Shot and detect Process private void Photograph_Cilck_Process(object arg) { //Face detect try { Bitmap tempImage0 = new Bitmap(TempImage, pictureBox.Width, pictureBox.Height); string argString = (string)arg; if (argString == "TIMER") { } else if (argString == "BUTTON") { this.Invoke((EventHandler)(delegate { pictureBox.Image = tempImage0; })); textBoxOutput.Clear(); textBoxOutput.AppendText("正在识别。" + Environment.NewLine); } else { //Never Reach } Bitmap tempImage1 = new Bitmap(TempImage, Env.PicProcessWidth, Env.PicProcessHeight); RandomMemoImage = TempImage; string tempPath = Path.GetTempFileName(); tempImage1.Save(tempPath); DetectResult res = fs.Detection_DetectImg(tempPath); nowDetectResult = res; Graphics g = Graphics.FromImage(tempImage0); float FontSize = 20; for (int i = 0; i < res.face.Count; i++) { #region Draw lines float centerX = (float)(res.face[i].position.center.x / 100 * pictureBox.Width); float centerY = (float)(res.face[i].position.center.y / 100 * pictureBox.Height); float halfX = (float)(res.face[i].position.width / 2 / 100 * pictureBox.Width); float halfY = (float)(res.face[i].position.height / 2 / 100 * pictureBox.Height); g.DrawString(i.ToString(), new Font("等线", FontSize), Brushes.DarkTurquoise, new PointF(centerX - FontSize / 2, centerY - FontSize / 2)); g.DrawLine(new Pen(Color.DarkTurquoise, 4), centerX - halfX, centerY - halfY, centerX + halfX, centerY - halfY); g.DrawLine(new Pen(Color.DarkTurquoise, 4), centerX - halfX, centerY + halfY, centerX + halfX, centerY + halfY); g.DrawLine(new Pen(Color.DarkTurquoise, 4), centerX - halfX, centerY - halfY, centerX - halfX, centerY + halfY); g.DrawLine(new Pen(Color.DarkTurquoise, 4), centerX + halfX, centerY - halfY, centerX + halfX, centerY + halfY); #endregion } g.Dispose(); pictureBox.Image = tempImage0; #region Button enable buttonAddToLib.Enabled = true; buttonCompare.Enabled = true; #endregion ConsoleOutput(res); File.Delete(tempPath); } catch (Exception ex) { textBoxOutput.Clear(); textBoxOutput.AppendText("识别已终止。" + Environment.NewLine); MessageBox.Show("图片识别中发生错误:" + ex.Message); } GC.Collect(); }
static void Main(string[] args) { String apiKey = "847e6315f892e21449da5f4077c5104f"; String apiSecret = "BmskojfFyrZVQhkLfNSnRzX-lK8musO6"; FaceService faceService = new FaceService(apiKey, apiSecret); string filePath = "D:\\Codes\\datasets\\face_morph\\bbt.jpg"; DetectResult detectResult = faceService.Detection_DetectImg(filePath); Image<Bgr, Byte> srcImg = new Image<Bgr, Byte>(filePath); for(int cnt=0; cnt < detectResult.face.Count; cnt++) { string pointFileName = String.Format("D:\\Codes\\datasets\\face_morph\\result_bbt_face_{0}.txt", cnt); FileStream fileStream = new FileStream(pointFileName, FileMode.Create); StreamWriter streamWriter = new StreamWriter(fileStream); Rectangle faceRect = new Rectangle( (int)(detectResult.face[cnt].position.center.x * srcImg.Width /100 - detectResult.face[cnt].position.width * srcImg.Width * 0.5 / 100), (int)(detectResult.face[cnt].position.center.y * srcImg.Height / 100 - detectResult.face[cnt].position.height * srcImg.Height * 0.5 / 100), (int)detectResult.face[cnt].position.width * srcImg.Width / 100, (int)detectResult.face[cnt].position.height * srcImg.Height / 100); Image<Bgr, byte> faceImg = srcImg.GetSubRect(faceRect); string fileName = String.Format("D:\\Codes\\datasets\\face_morph\\result_bbt_face_{0}.jpg", cnt); faceImg.Save(fileName); IList<FaceppSDK.Point> featurePoints = new List<FaceppSDK.Point>(); //featurePoints.Add(detectResult.face[cnt].position.center); FaceppSDK.Point tempPoint1 = new FaceppSDK.Point(); tempPoint1.x = (detectResult.face[cnt].position.eye_left.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint1.y = (detectResult.face[cnt].position.eye_left.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint1); FaceppSDK.Point tempPoint2 = new FaceppSDK.Point(); tempPoint2.x = (detectResult.face[cnt].position.eye_right.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint2.y = (detectResult.face[cnt].position.eye_right.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint2); FaceppSDK.Point tempPoint3 = new FaceppSDK.Point(); tempPoint3.x = (detectResult.face[cnt].position.mouth_left.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint3.y = (detectResult.face[cnt].position.mouth_left.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint3); FaceppSDK.Point tempPoint4 = new FaceppSDK.Point(); tempPoint4.x = (detectResult.face[cnt].position.mouth_right.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint4.y = (detectResult.face[cnt].position.mouth_right.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint4); FaceppSDK.Point tempPoint5 = new FaceppSDK.Point(); tempPoint5.x = (detectResult.face[cnt].position.nose.x - detectResult.face[cnt].position.center.x) / detectResult.face[cnt].position.width; tempPoint5.y = (detectResult.face[cnt].position.nose.y - detectResult.face[cnt].position.center.y) / detectResult.face[cnt].position.height; featurePoints.Add(tempPoint5); foreach (FaceppSDK.Point featurePoint in featurePoints) { streamWriter.WriteLine(featurePoint.x.ToString()); streamWriter.WriteLine(featurePoint.y.ToString()); System.Drawing.PointF point = new System.Drawing.PointF((float)featurePoint.x * srcImg.Width / 100, (float)featurePoint.y * srcImg.Height / 100); Cross2DF cross = new Cross2DF(point, (float)3.0, (float)3.0); srcImg.Draw(cross, new Bgr(0, 255, 0), 3); } streamWriter.Flush(); streamWriter.Close(); fileStream.Close(); //srcImg.Save("D:\\Codes\\datasets\\face_morph\\result_bbt.jpg"); } }