cooskas BLOG

以前做小车控制的时候,最激动的功能就是人脸识别,小车自动根据人脸的位置来转向

不过那时候用emgu是因为它工作在c#环境下,而且能很方便的控制摄像头。

现在上位机和摄像头不是直接联系,通过了wifi,而且703路由里openwrt系统的mjpg-stream发送出来的要么是图片要么就是mjpg流。

在网上逛了很久也没发现emgu能实时获取视频流。

后来发现另外一款关于机器视觉的库 aforge.net能实时获取。小开心了下,把基本代码发上来供有兴趣的人看看。

先从官网下载库文件,有这几个是我目前用到的:

AForge.Controls.dll、AForge.dll、AForge.Imaging.dll、AForge.Video.DirectShow.dll、AForge.Video.dll

其中controls.dll是控件库,可以加载到工具箱里。

using AForge.Video;using AForge.Video.DirectShow;using AForge.Imaging;

然后在窗体上拖进去一个VideoSourcePlayer控件,

在form_load里加上

if (videoSourcePlayer.VideoSource != null) {

videoSourcePlayer.SignalToStop();

videoSourcePlayer.WaitForStop();

}

然后写两个调用函数,

private void connectVidio() {

MJPEGStream mjpegSource = new MJPEGStream(:端口/?action=stream); //这里是从路由发出的视频流的地址,根据实际情况来写

OpenVideoSource(mjpegSource); }

private void OpenVideoSource(IVideoSource source) {

videoSourcePlayer.SignalToStop();

videoSourcePlayer.WaitForStop();

videoSourcePlayer.VideoSource = source;

videoSourcePlayer.Start();

}然后调用connectVidio就能在窗体上看到视频了。

不过刚刚接触aforge,其他很多无比强大的功能都还不会,据说有神经网络,强化算法等,有很多网友用这个库做了物体追踪的功能,佩服啊。

好想念以前的emgu,至少用的还算熟练。

突发奇想,用aforge获取的视频流的帧,,供emgu分析处理后放到窗体显示出来。

试试先,呵呵。。。 这里做个人脸识别看效果如何

先引用

using Emgu.CV;using Emgu.CV.Structure;using Emgu.CV.UI;using Emgu.CV.GPU;using Emgu.Util;

emgu例子里有个faceditect类,直接拷贝过来用。

videoSourcePlayer的newframe事件里加上以下代码

private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) { Image<Bgr, Byte> img = new Image<Bgr, Byte>(image); img = img.PyrDown(); long detectionTime; List<Rectangle> faces = new List<Rectangle>(); List<Rectangle> eyes = new List<Rectangle>(); DetectFace.Detect(img, "detectorxml/haarcascade_frontalface_default.xml", "detectorxml/haarcascade_eye.xml", faces, eyes, out detectionTime); foreach (Rectangle face in faces) img.Draw(face, new Bgr(Color.Red), 1); foreach (Rectangle eye in eyes) img.Draw(eye, new Bgr(Color.Blue),1); image=img.ToBitmap();

}

还真的实现了,只是效率有点低。。。。再慢慢研究下吧

放上效果图

天下爱情,大抵如斯。

cooskas BLOG

相关文章:

你感兴趣的文章:

标签云: