-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceCapture.cs
330 lines (274 loc) · 11.1 KB
/
FaceCapture.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Cuda;
using Emgu.CV.Structure;
using Emgu.CV.VideoSurveillance;
using Emgu.CV.Util;
using Emgu.Util;
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace Ungu_CV1
{
public delegate void FaceCapturedEventHandler(object sender, Face face);
public delegate void ImageCapturedEventHandler(object sender);
public class FaceCapture
{
private Capture capture;
private MotionHistory _motionHistory;
private BackgroundSubtractor foregroundDetector;
public string FaceTrainingFile { get; set; }
public string EyeTrainingFile { get; set; }
public double Scale { get; set; }
public int Neighbors { get; set; }
public int FaceMinSize { get; set; }
public int FaceMaxSize { get; set; }
public List<Face> Faces { get; set; }
public double motionHistoryDuration = 1.0;
public double maxDelta = 0.05;
public double minDelta = 0.5;
public int frameCount = 0;
/// <summary>
/// average movement of pixels weighted smoothed .75 / .25 new
/// </summary>
public double averagetotalPixelCount =1;
private IFindFaces FaceDetector;
public bool HasCuda = true;
public Image<Bgr, Byte> ImageFrameLast;
public Image<Bgr, Byte> ImageMotionLast;
public Image<Bgr, byte> ImageForeGroundMaskLast;
public event FaceCapturedEventHandler FaceCaptured;
public event ImageCapturedEventHandler ImageCaptured;
public FaceCapture(string faceTrainingFile, string eyeTrainingFile)
: this(faceTrainingFile, eyeTrainingFile, 1.1, 10, 100)
{
}
public FaceCapture(string faceTrainingFile, string eyeTrainingFile, double scale, int neighbors, int minSize)
{
loadConfig();
FaceTrainingFile = faceTrainingFile;
EyeTrainingFile = eyeTrainingFile;
Scale = scale;
Neighbors = neighbors;
FaceMinSize = minSize;
FaceMaxSize = 200;
try
{
if (HasCuda && CudaInvoke.HasCuda)
{
FaceDetector = new FaceDetectCuda();
}
else
{
FaceDetector = new FaceDetect();
}
}
catch (Exception errCuda)
{
Console.WriteLine("ERROR - FaceCapture CudaInvoke.HasCuda errCuda: " + errCuda);
FaceDetector = new FaceDetect();
}
_motionHistory = new MotionHistory(
motionHistoryDuration, //in second, the duration of motion history you wants to keep
maxDelta, //in second, maxDelta for cvCalcMotionGradient
minDelta); //in second, minDelta for cvCalcMotionGradient
//capture = new Capture();
}
private void loadConfig()
{
}
public void StartCapture()
{
Console.WriteLine("StartCapture");
Faces = new List<Face>();
capture = new Capture();
_motionHistory = new MotionHistory(1.0, 0.05, 0.5);
Application.Idle += ProcessFrame;
}
public void StopCapture()
{
Console.WriteLine("StopCapture");
Application.Idle -= ProcessFrame;
capture.Dispose();
capture = null;
if (this.Faces.Count > 4)
{
this.Faces.RemoveAt(0);
this.Faces.RemoveAt(0);
}
}
public List<Face> GetFaces(int numFaces)
{
return GetFaces(numFaces, 75);
}
public List<Face> GetFaces(int numFaces, int minScore)
{
int frameCount = 0;
capture = new Capture();
_motionHistory = new MotionHistory(1.0, 0.05, 0.5);
List<Face> foundfaces = new List<Face>();
while (foundfaces.Count() < numFaces)
{
Mat mat = capture.QueryFrame();
Image<Bgr, Byte> ImageFrame = mat.ToImage<Bgr, Byte>();
frameCount = frameCount + 1;
MotionInfo motion = this.GetMotionInfo(mat);
List<Face> detectedFaces = FaceDetector.FindFaces(ImageFrame, this.FaceTrainingFile, this.EyeTrainingFile, this.Scale, this.Neighbors, this.FaceMinSize);
if (frameCount > 2)
{
foreach (Face face in detectedFaces)
{
face.MotionObjects = motion.MotionObjects;
face.MotionPixels = motion.MotionPixels;
if (face.FaceScore > minScore)
{
foundfaces.Add(face);
}
}
}
}
capture.Dispose();
capture = null;
return foundfaces;
}
//public async Task<List<Face>> GetFacesAsync(int numFaces)
//{
// return await Task.Run(() => this.GetFaces(numFaces));
//}
//public async Task<List<Face>> GetFacesAsync(int numFaces, int minScore)
//{
// return await Task.Run(() => this.GetFaces(numFaces, minScore));
//}
public Face GetFace()
{
return this.GetFace(75);
}
public Face GetFace(int minScore)
{
List<Face> foundfaces = new List<Face>();
foundfaces = this.GetFaces(1, minScore);
return foundfaces.FirstOrDefault();
}
//public async Task<Face> GetFaceAsync()
//{
// return await Task.Run(() => this.GetFace());
//}
//public async Task<Face> GetFaceAsync(int minScore)
//{
// return await Task.Run(() => this.GetFace(minScore));
//}
public Face GetBestCapturedFace()
{
return GetBestCapturedFaces(1).FirstOrDefault();
}
public List<Face> GetBestCapturedFaces(int facecount)
{
return Faces.OrderByDescending(f => f.FaceScore).Take(facecount).ToList();
}
/// <summary>
/// main element of capturing the camera and playing back.
/// </summary>
/// <param name="sender"></param>
/// <param name="arg"></param>
private void ProcessFrame(object sender, EventArgs arg)
{
Mat mat = capture.QueryFrame();
Image<Bgr, Byte> ImageFrame = mat.ToImage<Bgr, Byte>();
if ((frameCount++)%17==0)
Console.WriteLine("FaceCapture ProcessFrame start frameCount=" + frameCount + " datetime" + DateTime.Now);
if (ImageCaptured != null)
{
ImageFrameLast = ImageFrame;
ImageCaptured(this);
}
MotionInfo motion = this.GetMotionInfo(mat);
List<Face> FoundFaces = FaceDetector.FindFaces(ImageFrame, this.FaceTrainingFile, this.EyeTrainingFile, this.Scale, this.Neighbors, this.FaceMinSize);
foreach (Face face in FoundFaces)
{
face.MotionObjects = motion.MotionObjects;
face.MotionPixels = motion.MotionPixels;
if (FaceCaptured != null)
{
FaceCaptured(this, face);
}
Faces.Add(face);
}
}
private MotionInfo GetMotionInfo(Mat image)
{
Mat _forgroundMask = new Mat();
Mat _segMask = new Mat();
MotionInfo motionInfoObj = new MotionInfo();
double minArea, angle, objectCount, totalPixelCount;
double overallangle = 0;
double motionPixelCount =0;
int motionArea =0;
totalPixelCount = 0;
objectCount = 0;
minArea = 800;
if (foregroundDetector == null)
{
foregroundDetector = new BackgroundSubtractorMOG2();
}
foregroundDetector.Apply(image, _forgroundMask);
_motionHistory.Update(_forgroundMask);
ImageForeGroundMaskLast = _forgroundMask.ToImage<Bgr, byte>();
#region get a copy of the motion mask and enhance its color
double[] minValues, maxValues;
Point[] minLoc, maxLoc;
_motionHistory.Mask.MinMax(out minValues, out maxValues, out minLoc, out maxLoc);
Mat motionMask = new Mat();
using (ScalarArray sa = new ScalarArray(255.0 / maxValues[0]))
CvInvoke.Multiply(_motionHistory.Mask, sa, motionMask, 1, DepthType.Cv8U);
//Image<Gray, Byte> motionMask = _motionHistory.Mask.Mul(255.0 / maxValues[0]);
#endregion
//create the motion image
Image<Bgr, Byte> motionImage = new Image<Bgr, byte>(motionMask.Size);
//display the motion pixels in blue (first channel)
//motionImage[0] = motionMask;
CvInvoke.InsertChannel(motionMask, motionImage, 0);
//Threshold to define a motion area, reduce the value to detect smaller motion
minArea = 100;
//storage.Clear(); //clear the storage
Rectangle[] rects;
using (VectorOfRect boundingRect = new VectorOfRect())
{
_motionHistory.GetMotionComponents(_segMask, boundingRect);
rects = boundingRect.ToArray();
}
//iterate through each of the motion component
foreach (Rectangle comp in rects)
{
int area = comp.Width * comp.Height;
//reject the components that have small area;
_motionHistory.MotionInfo(_forgroundMask, comp, out angle, out motionPixelCount);
if (area < minArea) continue;
else
{
overallangle = overallangle + angle;
totalPixelCount = totalPixelCount + motionPixelCount;
objectCount = objectCount + 1;
motionArea = motionArea + area;
}
// find the angle and motion pixel count of the specific area
////Draw each individual motion in red
//DrawMotion(motionImage, comp, angle, new Bgr(Color.Red));
}
motionInfoObj.MotionArea = motionArea;
motionInfoObj.OverallAngle = overallangle;
motionInfoObj.BoundingRect = rects;
motionInfoObj.TotalMotions = rects.Length;
motionInfoObj.MotionObjects = objectCount;
motionInfoObj.MotionPixels = totalPixelCount;
averagetotalPixelCount = 0.75 * averagetotalPixelCount + 0.25 * totalPixelCount;
if ( Math.Abs(averagetotalPixelCount - totalPixelCount) / averagetotalPixelCount > 0.59)
Console.WriteLine(" GetMotionInfo - Total Motions found: " + rects.Length + "; Motion Pixel count: " + totalPixelCount);
return motionInfoObj;
}
}
}