-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFaceDetect.cs
More file actions
59 lines (52 loc) · 1.74 KB
/
FaceDetect.cs
File metadata and controls
59 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Emgu.CV;
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace HeadTrack
{
class FaceDetect
{
private CascadeClassifier face = null;
Stopwatch watch;
Mat _grayFrame;
Rectangle roi;
public FaceDetect()
{
_grayFrame = new Mat();
face = new CascadeClassifier("D:/Codes/Lab/HeadTracking/HeadTrack/haarcascade_frontalface_default.xml");
roi = new Rectangle(0,0,0,0);
}
public Rectangle detectFace(Mat frame, bool debug = false)
{
if (debug)
watch = Stopwatch.StartNew();
CvInvoke.CvtColor(frame, _grayFrame, ColorConversion.Bgr2Gray);
CvInvoke.EqualizeHist(_grayFrame, _grayFrame);
Rectangle[] facesDetected = face.DetectMultiScale(
_grayFrame,
1.1,
6,
new System.Drawing.Size(70, 70));
foreach (Rectangle face in facesDetected)
{
//CvInvoke.Rectangle(frame, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2);
if (roi.Height * roi.Width < face.Height * face.Width)
roi = face;
}
if (facesDetected.Count() == 0) roi.Height = 0;
if (debug)
{
Console.WriteLine("find {0} faces!", facesDetected.Count());
watch.Stop();
Console.WriteLine("time elapse : {0}", watch.ElapsedMilliseconds);
}
return roi;
}
}
}