Ref: https://github.com/emgucv/emgucv/tree/master/Emgu.CV.Example/FacialMouseControl
Windows 10 x64 @ ASUS X450J + Visual Studio 2017 + Emgu 3.2.0.2721
1. 版本更新
private Capture _capture;
private HaarCascade _face;
==>
// https://stackoverflow.com/questions/46410342/c-sharp-emgu-could-not-be-found-capture-and-haarcascade
private VideoCapture _capture;
private CascadeClassifier _face;
2. Form.cs 更改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wk1502
{
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using System.Threading;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
private VideoCapture _capture;
// https://stackoverflow.com/questions/46410342/c-sharp-emgu-could-not-be-found-capture-and-haarcascade
//private HaarCascade _face;
private CascadeClassifier _face;
public Form1()
{
InitializeComponent();
//Read the HaarCascade object
_face = new CascadeClassifier("haarcascade_frontalface_alt2.xml");
if (_capture == null)
{
try
{
_capture = new VideoCapture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
return;
}
}
Application.Idle += ProcessImage;
}
public void ProcessImage(object sender, EventArgs e)
{
using (var imageFrame = _capture.QueryFrame().ToImage<Bgr, Byte>())
{
if (imageFrame != null)
{
var grayframe = imageFrame.Convert<Gray, byte>();
var vfaces = _face.DetectMultiScale(grayframe, 1.1, 10, Size.Empty); //the actual face detection happens here
if (vfaces.Length > 0)
{
Rectangle Maxface = vfaces[0];
int maxw = vfaces[0].Width;
int maxh = vfaces[0].Height;
for (int i = 1; i < vfaces.Length; i++)
{
if (vfaces[i].Width * vfaces[i].Height > maxw * maxh)
{
Maxface = vfaces[i];
maxw = vfaces[i].Width;
maxh = vfaces[i].Height;
}
}
imageFrame.Draw(Maxface, new Bgr(Color.BurlyWood), 3); //the detected face(s) is highlighted here using a box that is drawn around it/them
Point biggestFaceCenter = new Point(Maxface.X + Maxface.Width / 2, Maxface.Y + Maxface.Height / 2);
//draw a green cross at the center of the biggest face
imageFrame.Draw(
new Cross2DF(biggestFaceCenter, Maxface.Width * 0.1f, Maxface.Height * 0.1f),
new Bgr(0, 255, 0), 1);
this.Text = string.Format("pt:{0},{1}, a:{2}", biggestFaceCenter.X, biggestFaceCenter.Y, Maxface.Width*Maxface.Height);
}
}
imageBox1.Image = imageFrame;
}
}
[DllImport("user32.dll")]
private static extern bool GetCursorPos(out System.Drawing.Point lpPoint);
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X, int Y);
public void ReleaseData()
{
if (_capture != null)
_capture.Dispose();
}
private void flipHorizontalButton_Click(object sender, EventArgs e)
{
if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
}
}
}
3. Result
沒有留言:
張貼留言