本帖最后由 whble 于 2013-4-1 17:24 编辑
最近学习了Emgu cv 的图像处理算法,使用opencv公司提供的开源代码做了一个远程
图片流的人脸识别检测的上位机,首先来看看效果图
首先启动程序后
然后点击取得连接,蓝色区域将会显示远程摄像头采集的视频图像,
此时将捕捉人脸,并识别,如图
侧脸时,由于我使用的是之前训练的识别库,这个角度没法识别出我的脸,
调整角度后结果就如下图,识别出我的脸,并显示出在识别库对应的我的名字。
当点击训练识别时将调用笔记本的摄像头进行训练,界面如下(此段是参考别人的训练方法)
在训练框中的名字栏填写要的存储对应头像的人名。
训练完成后,关闭窗口,自动回调出远程视频连接。
再来测试一下识别能力
同为侧面角度,就能识别出人脸并检测了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using Emgu.CV.UI;
- using Emgu.CV.GPU;
- using Emgu.CV.CvEnum;
- namespace FacesDetect
- {
- public partial class VideoForm : Form
- {
- private Image<Bgr, byte> currentImage;//设置当前画面
- Image<Gray, byte> result = null;//返回帧
- Image<Gray, byte> gray_frame = null;//灰度帧
- public HaarCascade Face = new HaarCascade(Application.StartupPath + "/Cascades/haarcascade_frontalface_alt2.xml");//载入训练库数据
- MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_COMPLEX, 0.5, 0.5);//设置摄像头字体
- //Classifier with default training location
- Classifier_Train Eigen_Recog = new Classifier_Train();//实例化一个分类训练器
- public VideoForm()
- {
- InitializeComponent();
- }
-
- private void showVideo(object sender, EventArgs e)
- {
- MyCaputre myCap = new MyCaputre();
- string sourceURL = this.textBox1.Text;
- Bitmap bmp = myCap.myCaputre(sourceURL);
- currentImage = new Image<Bgr, byte>(bmp);
- markFace(currentImage);
- pictureBox1.Image = new System.Drawing.Bitmap(currentImage.ToBitmap(), 850, 660);
- }
- private Image<Bgr, byte> markFace(Image<Bgr, byte> pic)
- {
- currentImage =currentImage.Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- //Convert it to Grayscale
- if (currentImage != null)
- {
- gray_frame = currentImage.Convert<Gray, Byte>();
- //Face Detector
- MCvAvgComp[][] facesDetected = gray_frame.DetectHaarCascade(Face, 1.2, 10, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(50, 50));
- //Action for each element detected
- foreach (MCvAvgComp face_found in facesDetected[0])
- {
- result = currentImage.Copy(face_found.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- //draw the face detected in the 0th (gray) channel with blue color
- currentImage.Draw(face_found.rect, new Bgr(Color.Red), 2);
- if (Eigen_Recog.IsTrained)
- {
- string name = Eigen_Recog.Recognise(result);
- int match_value = (int)Eigen_Recog.Get_Eigen_Distance;
- //Draw the label for each face detected and recognized
- currentImage.Draw(name + " ", ref font, new Point(face_found.rect.X - 2, face_found.rect.Y - 2), new Bgr(Color.LightGreen));
- }
- }
- //Show the faces procesed and recognized
- pictureBox1.Image = currentImage.ToBitmap();
- }
- return pic;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- start_cam();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- stop_cam();
- //OpenForm
- Training_Form TF = new Training_Form(this);//以本窗体为父窗体打开训练窗体
- TF.Show();//启动训练窗体
-
- }
- public void retrain()//重新加载数据
- {
- Eigen_Recog = new Classifier_Train();
- if (Eigen_Recog.IsTrained)
- {
- // message_bar.Text = "训练图像数据加载完毕!";
- }
- else
- {
- // message_bar.Text = "训练数据无法找到, 请通过训练菜单选项进行训练!";
- }
- }
- public void start_cam()
- {
- Application.Idle += new EventHandler(showVideo);
- }
- private void stop_cam()
- {
- Application.Idle -= new EventHandler(showVideo);
- if (currentImage != null)
- {
- currentImage.Dispose();
- }
- }
- }
- }
复制代码
|