【发布时间】: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);
}
}
}
}
提前致谢!!
【问题讨论】:
-
请描述您的具体和孤立问题。你试过调试这个吗?有什么例外吗?您的算法是否按预期工作?
标签: c# video-processing emgucv face-detection