-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
326 lines (284 loc) · 8.89 KB
/
MainForm.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
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Collections;
using System.ComponentModel;
namespace iRecognizeYou
{
/// <summary>
/// GUI programiku
/// </summary>
public partial class Forma : Form
{
#region Constructor & fields
/// <summary>
/// Obiekt odpowiadający za rozpoznawanie
/// </summary>
private Recognizer rec;
/// <summary>
/// Obiekt odpowiadający za kamerkę
/// </summary>
private Capture cap;
private Boolean stop = false;
private Thread trainThread;
/// <summary>
/// Wprowadzenie nowej twarzy wymaga przeładowania. Można to ominąć stosując
/// bezpośrednio funkcje z OpenCV bez wrappera - tutaj nie ma sensu
/// </summary>
private Boolean needToReload = false;
#endregion
#region Constructor
/// <summary>
/// Domyślny konstruktor - przygotowuje interfejs
/// </summary>
public Forma( )
{
InitializeComponent();
Region = System.Drawing.Region.FromHrgn(
CreateRoundRectRgn(
0,
0,
Width - 10,
Height - 10,
Width-50,
Height-50));
foundPerson.Visible = false;
foundPersonName.Visible = false;
foundPerson.Left = this.Width / 2 - foundPerson.Width / 2;
}
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
#endregion
#region Moving
private bool dragging = false;
private Point dragCursorPoint;
private Point dragFormPoint;
private void FormMain_MouseDown( object sender, MouseEventArgs e )
{
dragging = true;
dragCursorPoint = Cursor.Position;
dragFormPoint = this.Location;
}
private void FormMain_MouseMove( object sender, MouseEventArgs e )
{
if (dragging)
{
Point dif = Point.Subtract(Cursor.Position, new Size(dragCursorPoint));
this.Location = Point.Add(dragFormPoint, new Size(dif));
}
}
private void FormMain_MouseUp( object sender, MouseEventArgs e )
{
dragging = false;
}
#endregion
#region Interface and camera aquisition
/// <summary>
/// Zatrzymywanie rozpoznawania twarzy
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void exit_stop_Click( object sender, MouseEventArgs e )
{
if (stop)
{
stop = false;
exitButton.Text = "Exit";
exitButton.BackColor = Color.WhiteSmoke;
foundPerson.Visible = false;
foundPersonName.Visible = false;
timerRecognize.Stop();
timerRecognize.Enabled = false;
if (cap != null)
cap.Dispose();
return;
}
Application.Exit();
}
/// <summary>
/// Pokazanie info o programie
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void info_Click( object sender, MouseEventArgs e )
{
MessageBox.Show("iRecognizeYou\nRozpoznawanie twarzy - Face recognition\nMarek Bar\nKoło Naukowe Informatyków PWSTE w Jarosławiu.");
}
/// <summary>
/// Uruchamianie rozpoznawania twarzy
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void recognize_Click( object sender, MouseEventArgs e )
{
cap = new Capture(0);//połącz się z kamerką
if (cap == null)
return;//no camera
//sprawdź czy nie potrzeba przeładować obiektu rozpoznawania twarzy
if (needToReload)
{
needToReload = false;//oznacz, że potrzeba
rec = null;//porzuć stary, aby móc utworzyć nowy
}
if (rec == null)rec = new Recognizer();//jak nie istnieje to utwórz
foundPersonName.Visible = true;
foundPerson.Visible = true;
timerRecognize.Enabled = true;
timerRecognize.Start();
stop = true;
exitButton.Text = "Stop";
exitButton.BackColor = Color.Red;
}
/// <summary>
/// Uruchom dodawanie do bazy
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void train_Click( object sender, MouseEventArgs e )
{
trainThread = new Thread(
delegate( )
{
Application.Run(new Learn());
needToReload = true;
});
trainThread.Start();
}
/// <summary>
/// Odbieranie klatek z kamerki i przekazywanie od obiektu, który zajmuje się wykrywaniem i rozpoznawaniem twary.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerRecognize_Tick( object sender, EventArgs e )
{
using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
{
if (nextFrame != null)
{
foundPerson.Image = nextFrame.ToBitmap();//podgląd
String answer = rec.recognize(nextFrame);
foundPersonName.Text = answer;
}//if
}//using
}
#endregion
#region Adding faces to database from files
/// <summary>
/// Choose folder from which you want to analyse images and maybe detect faces.
/// Images must be named as "name surname number" - like a "John Smith 1"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void detectFacesFromFolder(object sender,EventArgs e){
chooseDir.RootFolder = Environment.SpecialFolder.Desktop;
chooseDir.Description = "Choose folder with faces/face in" +
"images which will be added to face database" +
" - only *.jpg files will be checked";
if(chooseDir.ShowDialog() == DialogResult.OK){
String dir = chooseDir.SelectedPath;
progressBar.Maximum = Tools.fileFilter("jpg,png,gif,bmp", dir).Length;
progressBar.Minimum = 0;
if (progressBar.Maximum == 0)
{
MessageBox.Show("No images with *.jpg extension found - " +
"it is not accepted to use other image file format");
return;
}
progressBar.Visible = true;
addImagesWorker.RunWorkerAsync(dir);
}//if folder chosen
}
/// <summary>
/// Adding to face database new faces - background worker
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addImagesWorker_DoWork( object sender, DoWorkEventArgs e )
{
String dir = (String)e.Argument;
String [] files = Tools.fileFilter("jpg,png,gif,bmp", dir);
var haar = new HaarCascade("haarcascade_frontalface_default.xml");
int count = 0;
foreach (String filename in files)
{
var image = new Image<Gray, Byte>(dir + "\\" + filename);
Image<Gray, byte> img = image.Convert<Gray, byte>();
var faces = haar.Detect(img, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(img.Width / 8, img.Height / 8));
Rectangle rect = new Rectangle();
if (faces.Length > 0)
{
rect = faces [0].rect;
for (int i = 1; i < faces.Length; i++)
{
if (faces [i].rect.Width > rect.Width &&
faces [i].rect.Height > rect.Height)
{
rect = faces [i].rect;
}
}//find the biggest face in detected
img.ROI = rect;
Image<Gray, Byte> face = img.Clone();
Image<Gray, Byte> faceResized = face.Resize(100, 100,
Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
if (faceResized != null)
{
var splitted = filename.Substring(0, filename.Length - 4).Split(' ');
int tmp;
if(int.TryParse(splitted[2],out tmp)){
faceResized.Save(Tools.createFileName(splitted [0], splitted [1], int.Parse(splitted [2])));
}
}//if resized
}//if found any face
count++;
addImagesWorker.ReportProgress(count, DateTime.Now);
}//check each jpg file
}
/// <summary>
/// Reporting adding faces progress
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addImagesWorker_ProgressChanged( object sender, ProgressChangedEventArgs e )
{
progressBar.Value = e.ProgressPercentage;
}
/// <summary>
/// Report end of adding new faces to database
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void addImagesWorker_RunWorkerCompleted( object sender,RunWorkerCompletedEventArgs e )
{
progressBar.Visible = false;
String dir = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments) + "\\iRecognizeYou";
if(Directory.Exists(dir))
System.Diagnostics.Process.Start(dir);
}//function
#endregion
public void showEigenFaces( object sender, EventArgs e )
{
if (rec!=null)
rec.saveToDesktopEigenImages();
}
public void showEigenValues( object sender, EventArgs e )
{
if(rec!=null)rec.saveToDesktopEigenValues();
}
}//class
}//ns