WebCamViewForm.cs
6.25 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
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
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using ImageFilters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoyaSignup
{
public partial class WebCamViewForm : Form
{
int RetangleHeight = 410;
int RetangleWidth = 280;
List<Capture> webCams;
Dictionary<Capture, Image<Bgr, byte>> currentFrames = new Dictionary<Emgu.CV.Capture, Image<Bgr, byte>>();
Image<Bgr, Byte> currentFrame;
Image<Bgr, Byte> currentFullFrame;
HaarCascade face;
Image<Gray, byte> gray = null;
Dictionary<Capture, decimal> scores = new Dictionary<Capture, decimal>();
public WebCamViewForm(List<Capture> webCams)
{
InitializeComponent();
this.webCams = webCams;
}
private void WebCamViewForm_Load(object sender, EventArgs e)
{
foreach(Capture cam in webCams)
{
ImageBox box = new ImageBox();
box.Size = new Size(640, 360);
box.Tag = cam;
/*
FlowLayoutPanel flowPanel = new FlowLayoutPanel();
flowPanel.FlowDirection = FlowDirection.LeftToRight;
flowPanel.Size = new Size(300, 410);
ImageBox box2 = new ImageBox();
box2.Size = new Size(280, 410);
box2.Tag = cam;
Label label = new Label();
label.Text = "0";
flowPanel.Controls.Add(box2);
flowPanel.Controls.Add(label);
*/
CroppedImageControl croppedImageCtrl = new CroppedImageControl();
flowLayoutPanel1.Controls.Add(box);
flowLayoutPanel2.Controls.Add(croppedImageCtrl);
}
timer1.Enabled = true;
timer1.Start();
face = new HaarCascade("haarcascade_frontalface_default.xml");
//Application.Idle += FrameGrapper;
}
private void FrameGrapper(object sender, EventArgs e)
{
for(int i=0; i < webCams.Count; i++)
{
Capture cam = webCams[i];
Image<Bgr, byte> currentFullFrame = cam.QueryFrame();
/*
lock (currentFrames)
{
if (currentFrames.ContainsKey(cam))
currentFrames[cam] = currentFullFrame;
else
currentFrames.Add(cam, currentFullFrame);
}*/
if (i <= flowLayoutPanel1.Controls.Count && flowLayoutPanel1.Controls[i] is ImageBox)
{
currentFrame = currentFullFrame.Resize(640, 360, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); //640x360
ImageBox box = (ImageBox)flowLayoutPanel1.Controls[i];
box.Image = currentFrame;
//Dictionary<Image<Bgr, byte>, decimal> croppedImages = new Dictionary<Image<Bgr, byte>, decimal>();
decimal score = 0;
gray = currentFrame.Convert<Gray, Byte>();
MCvAvgComp[][] facesDetected = null;
if (gray != null)
{
//Face Detector
facesDetected = gray.DetectHaarCascade(
face,
1.2,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(100, 100));
}
//Action for each element detected
if (facesDetected != null && facesDetected[0].Length > 0)
{
MCvAvgComp f = facesDetected[0][0];
decimal width = ((decimal)f.rect.Width) * 2.9M;
decimal height = width * 1.464285M;
int x = (f.rect.X + f.rect.Width / 2) * 2;
x = (int)(x - width / 2);
int y = (f.rect.Y + f.rect.Height / 2) * 2;
y = (int)(y - height / 2);
int x1 = (int)(x + width / 2);
if (x1 >= 1280)
x1 = 1279;
int y1 = (int)(y + height / 2);
if (y1 >= 720)
y1 = 719;
decimal distance = (decimal)Math.Sqrt(Math.Pow((double)(x1 - 640), 2) + Math.Pow((double)(y1 - 360), 2));
score += distance;
if (x + width <= 1280 && y + height <= 720 && x >= 0 && y >= 0)
{
Debug.WriteLine("[webcamviewform] Found a face in frame");
Rectangle cropRectangle = new Rectangle(x, y, (int)width, (int)height);
currentFullFrame.Draw(cropRectangle, new Bgr(Color.Red), 2);
Image<Bgr, byte> croppedImage = currentFullFrame.Copy(cropRectangle);
croppedImage = croppedImage.Resize(280, 410, INTER.CV_INTER_CUBIC);
//Bitmap latestFrame = BitmapFilter.Crop(croppedImage.Bitmap, RetangleWidth, RetangleHeight, ImageFilters.BitmapFilter.AnchorPosition.Free);
if(flowLayoutPanel2.Controls != null && flowLayoutPanel2.Controls.Count > i && flowLayoutPanel2.Controls[i] is CroppedImageControl)
{
CroppedImageControl croppedImageCtrl = (CroppedImageControl)flowLayoutPanel2.Controls[i];
croppedImageCtrl.SetScore(score);
croppedImageCtrl.SetPicture(croppedImage.Bitmap);
}
}
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
FrameGrapper(sender, null);
}
}
}