无法从 emgucv 中的视频中检测到人脸
Posted
技术标签:
【中文标题】无法从 emgucv 中的视频中检测到人脸【英文标题】:Not able to detect face from video in emgucv 【发布时间】:2018-10-15 01:59:00 【问题描述】:通过在 C# 中使用 Emgucv,我能够从图像中检测人脸,但无法从视频中检测人脸。在我的解决方案中,视频正在播放,但未检测到人脸。
我的代码如下:
namespace Emgucv33Apps
public partial class FormFaceDetection : Form
VideoCapture capture;
bool Pause = false;
// Image<Bgr, byte> imgInput;
public FormFaceDetection()
InitializeComponent();
private void openToolStripMenuItem_Click(object sender, EventArgs e)
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
capture = new VideoCapture(ofd.FileName);
Mat m = new Mat();
capture.Read(m);
pictureBox1.Image = m.Bitmap;
private void DetectFaceHaar(Image<Bgr, byte> img)
try
string facePath = Path.GetFullPath(@"../../data/haarcascade_frontalface_default.xml");
string eyePath = Path.GetFullPath(@"../../data/haarcascade_eye.xml");
CascadeClassifier classifierFace = new CascadeClassifier(facePath);
CascadeClassifier classifierEye = new CascadeClassifier(eyePath);
var imgGray = img.Convert<Gray, byte>().Clone();
Rectangle[] faces = classifierFace.DetectMultiScale(imgGray, 1.1, 4);
foreach (var face in faces)
img.Draw(face, new Bgr(0, 0, 255), 2);
imgGray.ROI = face;
Rectangle[]eyes= classifierEye.DetectMultiScale(imgGray, 1.1, 4);
foreach (var eye in eyes)
var e = eye;
e.X += face.X;
e.Y += face.Y;
img.Draw(e, new Bgr(0, 255, 0), 2);
pictureBox1.Image = img.Bitmap;
pictureBox2.Image = img.Bitmap;
catch (Exception ex)
throw new Exception(ex.Message);
private async void pauseToolStripMenuItem_Click(object sender, EventArgs e)
if (capture == null)
return;
try
while (true)
Mat m = new Mat();
capture.Read(m);
if (!m.IsEmpty)
pictureBox1.Image = m.Bitmap;
DetectFaceHaar(m.ToImage<Bgr, byte>());
double fps = capture.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps);
await Task.Delay(1000 / Convert.ToInt32(fps));
else
break;
catch (Exception ex)
MessageBox.Show(ex.Message);
提前致谢!!
【问题讨论】:
请描述您的具体和孤立问题。你试过调试这个吗?有什么例外吗?您的算法是否按预期工作? 【参考方案1】:首先,您必须为此过程创建一个事件,并且您需要获取视频的每一帧并检查每一帧以进行人脸检测。使用 VideoCapture 类中的 QueryFrame 方法,您可以将每个帧作为图像处理并检测人脸。
例子
private VideoCapture m_videoCapture;
public MainWindow()
InitializeComponent();
try
m_videoCapture = new VideoCapture("controlcam.avi");
Application.Idle += onProcessFrame;
catch (NullReferenceException ex)
MessageBox.Show(ex.Message);
private void onProcessFrame(Object sender, EventArgs e)
Image<Bgr, Byte> frameImage = m_videoCapture.QueryFrame().ToImage<Bgr, Byte>();
// Call your function or write your code here.
DetectFaceHaar(frameImage);
private void DetectFaceHaar(Image<Bgr, byte> img)
try
string facePath = Path.GetFullPath(@"../../data/haarcascade_frontalface_default.xml");
string eyePath = Path.GetFullPath(@"../../data/haarcascade_eye.xml");
CascadeClassifier classifierFace = new CascadeClassifier(facePath);
CascadeClassifier classifierEye = new CascadeClassifier(eyePath);
var imgGray = img.Convert<Gray, byte>().Clone();
Rectangle[] faces = classifierFace.DetectMultiScale(imgGray, 1.1, 4);
foreach (var face in faces)
img.Draw(face, new Bgr(0, 0, 255), 2);
imgGray.ROI = face;
Rectangle[] eyes = classifierEye.DetectMultiScale(imgGray, 1.1, 4);
foreach (var eye in eyes)
var e = eye;
e.X += face.X;
e.Y += face.Y;
img.Draw(e, new Bgr(0, 255, 0), 2);
pictureBox1.Image = img.Bitmap;
pictureBox2.Image = img.Bitmap;
catch (Exception ex)
throw new Exception(ex.Message);
【讨论】:
以上是关于无法从 emgucv 中的视频中检测到人脸的主要内容,如果未能解决你的问题,请参考以下文章