-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainForm.cs
589 lines (551 loc) · 20.1 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
using AForge.Video.DirectShow;
using DlibDotNet;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Threading;
using System.Collections;
namespace Target_Recognition_Console
{
public partial class MainForm : Form
{
private FilterInfoCollection CaptureDevices; //设备列表
private VideoCaptureDevice captureDevice; //摄像头设备
private VideoCapabilities[] videoCapabilities; //摄像头能力列表
private VideoCapabilities videoCapabilitie; //单一摄像头能力(分辨率等)
public bool isface = false;
public bool istarget = true;
public bool isfacestart = false;
public bool iscarstart = false;
public bool isyolostart = false;
public FaceDetection fd;
public CarRecognition cr;
public Box box = new Box();
public int dot = 0;
public int dot2 = 0;
public int yolopic = 0;
public string videoFilePath;
public string imgFilePath;
public string imgFolderPath;
public string[] imglist;
public bool isimg = false;
public bool isimgfile = false;
public bool isplay = false;
public int photo = 1;
public MainForm()
{
InitializeComponent();
}
public void output(string str, int type)
{
textBoxinfo.BeginInvoke(new Action(() =>
{
if (type == 1)
{
textBoxinfo.Text += DateTime.Now + " [信息] " + str + "\r\n";
}
else
{
textBoxinfo.Text += DateTime.Now + " [警告] " + str + "\r\n";
}
textBoxinfo.SelectionStart = textBoxinfo.Text.Length;
textBoxinfo.ScrollToCaret();
}));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否退出软件?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
if (captureDevice != null)
{
captureDevice.Stop();
}
CaptureDevices.Clear();
videoSourcePlayer.VideoSource = null;
videoSourcePlayer.Stop();
}
catch (Exception) { }
Process.GetCurrentProcess().Kill();
Application.Exit();
}
else
{
e.Cancel = true;
}
}
private void comboBoxcam_SelectedIndexChanged(object sender, EventArgs e)
{
//选择摄像头后
comboBoxreso.Items.Clear(); //先清理上次选择的摄像头支持的分辨率
//获取摄像头设备
FilterInfo filterInfo = CaptureDevices[comboBoxcam.SelectedIndex];
captureDevice = new VideoCaptureDevice(filterInfo.MonikerString);
//获取所选择的摄像头分辨率列表并添加
videoCapabilities = captureDevice.VideoCapabilities;
foreach (VideoCapabilities capabilitie in videoCapabilities)
{
comboBoxreso.Items.Add(capabilitie.FrameSize.Width.ToString() +
"×" + capabilitie.FrameSize.Height.ToString());
}
//选中默认分辨率 触发coBox_Resolution_SelectedIndexChanged()
if (comboBoxreso.Items.Count > 0)
{
comboBoxreso.SelectedIndex = 0;
}
}
private void comboBoxreso_SelectedIndexChanged(object sender, EventArgs e)
{
//获取选择的分辨率,选择时没有关闭摄像头时进行关闭
videoCapabilitie = videoCapabilities[comboBoxreso.SelectedIndex];
buttonopencam.Enabled = true;
buttonphoto.Enabled = true;
if (videoSourcePlayer.IsRunning)
{
captureDevice.Stop();
videoSourcePlayer.VideoSource = null;
videoSourcePlayer.VideoSource = null;
buttonopencam.Text = "打开";
}
}
private void buttonopencam_Click(object sender, EventArgs e)
{
dot2 = 0;
if (buttonopencam.Text == "打开")
{
captureDevice.VideoResolution = videoCapabilitie;
videoSourcePlayer.VideoSource = captureDevice;
captureDevice.SimulateTrigger();
videoSourcePlayer.Start();
output("已打开摄像头。", 1);
buttonopencam.Text = "关闭";
buttonphoto.Enabled = true;
comboBoxcam.Enabled = false;
comboBoxreso.Enabled = false;
}
else
{
try
{
videoSourcePlayer.Stop();
}
catch (Exception) { }
videoSourcePlayer.VideoSource = null;
output("已关闭摄像头。", 1);
buttonopencam.Text = "打开";
buttonphoto.Enabled = false;
comboBoxreso.Enabled = true;
comboBoxcam.Enabled = true;
}
}
private void buttonopenvideo_Click(object sender, EventArgs e)
{
dot2 = 0;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "视频文件(*.avi;*.mp4)|*.avi;*.mp4";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
videoFilePath = openFileDialog.FileName;
output("已载入视频:" + Path.GetFileName(videoFilePath), 1);
}
}
private void buttonplayvideo_Click(object sender, EventArgs e)
{
// 活体对应视频路径的文件作为视频源
FileVideoSource videoSource = new FileVideoSource(videoFilePath);
videoSourcePlayer.VideoSource = videoSource;
videoSourcePlayer.Start();
output("已开始播放。", 1);
buttonphoto.Enabled = true;
isplay = true;
}
private void buttonpause_Click(object sender, EventArgs e)
{
if (isplay)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}
else
{
MessageBox.Show("没有正在播放的视频!");
}
}
private void buttonphoto_Click(object sender, EventArgs e)
{
pictureBoxphoto.Image = videoSourcePlayer.GetCurrentVideoFrame();
output("已进行拍照。", 1);
}
private void radioButtonface_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonface.Checked)
{
isface = true;
}
}
private void radioButtontarget_CheckedChanged(object sender, EventArgs e)
{
if (radioButtontarget.Checked)
{
isface = false;
istarget = false;
}
}
private void radioButtongo_CheckedChanged(object sender, EventArgs e)
{
if (radioButtongo.Checked)
{
isface = false;
istarget = true;
}
}
private void buttonload_Click(object sender, EventArgs e)
{
if (isface)
{
OpenFileDialog dlgOpenFile = new OpenFileDialog { Filter = "模型(*.dat)|*.dat" };
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
string path = dlgOpenFile.FileName;
fd = new FaceDetection(path);
output("已载入模型:" + dlgOpenFile.FileName, 1);
buttonstart.Enabled = true;
}
}
else
{
OpenFileDialog dlgOpenFile = new OpenFileDialog { Filter = "模型(*.dat)|*.dat" };
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
string path = dlgOpenFile.FileName;
cr = new CarRecognition(path);
output("已载入模型:" + dlgOpenFile.FileName, 1);
buttonstart.Enabled = true;
}
}
}
private void buttonstart_Click(object sender, EventArgs e)
{
if (radioButtonrealtime.Checked)
{
//实时检测
buttonstop.Enabled = true;
if (videoSourcePlayer.IsRunning)
{
if (isface)
{
isfacestart = true;
if (Dlib.IsSupportCuda)
{
output("正在使用GPU。", 1);
}
output("开始进行人脸检测。", 1);
}
else
{
iscarstart = true;
output("开始进行目标检测。", 1);
}
buttonstart.Enabled = false;
}
else
{
MessageBox.Show("请先打开视频源。", "警告");
}
}
else
{
//非实时检测
if (!isface)
{
dot2++;
CarDetection();
output("[Dlib]帧" + dot2.ToString() + ":目标数量为" + textBoxnum.Text + "。", 1);
}
else
{
dot2++;
FaceDetection();
output("[Dlib]帧" + dot2.ToString() + ":目标数量为" + textBoxnum.Text + "。", 1);
}
}
}
private void buttonstop_Click(object sender, EventArgs e)
{
if (isface)
{
isfacestart = false;
output("停止进行人脸检测。", 1);
}
else
{
iscarstart = false;
output("停止进行目标检测。", 1);
}
buttonstop.Enabled = false;
buttonstart.Enabled = true;
}
private void buttonexit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否退出软件?", "询问", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
if (captureDevice != null)
{
captureDevice.Stop();
}
CaptureDevices.Clear();
videoSourcePlayer.VideoSource = null;
videoSourcePlayer.Stop();
}
catch (Exception) { }
Process.GetCurrentProcess().Kill();
Application.Exit();
}
}
public void Face()
{
ThreadStart threadStartf = new ThreadStart(FaceDetection);
Thread threadf = new Thread(threadStartf);
threadf.Start();
}
private void FaceDetection()
{
try
{
//获取拍摄的图像
Bitmap img = (Bitmap)videoSourcePlayer.GetCurrentVideoFrame().Clone();
int numFaces = 0;
//进行人脸识别以及图像显示,更新界面的人脸识别数目
pictureBox.Image = fd.FaceDetectionFromImage(img, out numFaces);
textBoxnum.Text = numFaces.ToString();
}
catch (Exception ex)
{
output(ex.Message, 2);
}
}
public void Car()
{
ThreadStart threadStartc = new ThreadStart(CarDetection);
Thread threadc = new Thread(threadStartc);
threadc.Start();
}
private void CarDetection()
{
//调用目标检测
try
{
//获取拍摄的图像
Bitmap img = (Bitmap)videoSourcePlayer.GetCurrentVideoFrame().Clone();
int numCars = 0;
//进行目标识别以及图像显示,更新界面的人脸识别数目
if (!istarget)
{
pictureBox.Image = cr.CarRecognitionFromImage(img, out numCars);
}
else
{
pictureBox.Image = cr.TargetDetection(img, out numCars);
}
textBoxnum.Text = numCars.ToString();
}
catch (Exception ex)
{
output(ex.TargetSite + "|" + ex.Message, 2);
}
}
private void timer_Tick(object sender, EventArgs e)
{
//每秒调用一次
timer.Interval = 1000;
if (iscarstart)
{
dot++;
CarDetection();
output("[Dlib]帧" + dot.ToString() + ":目标数量为" + textBoxnum.Text + "。", 1);
}
else if (isfacestart)
{
dot++;
FaceDetection();
output("[Dlib]帧" + dot.ToString() + ":目标数量为" + textBoxnum.Text + "。", 1);
}
else if (isyolostart)
{
dot++;
YOLO();
output("[YOLO]帧" + dot.ToString() + ":目标数量为" + textBoxnum.Text + "。", 1);
}
else
{
dot = 0;
}
}
private void buttonloadyolo_Click(object sender, EventArgs e)
{
//载入模型
ThreadStart threadStart = new ThreadStart(BoxInit);
Thread thread = new Thread(threadStart);
thread.Start();
}
public void BoxInit()
{
//初始化
output("正在初始化……", 1);
box.Initilize();
output("初始化完成。", 1);
buttonstartyolo.BeginInvoke(new Action(() =>
{
buttonstartyolo.Enabled = true;
}));
}
private void buttonstartyolo_Click(object sender, EventArgs e)
{
if (radioButtonrealtime.Checked)
{
//实时检测
//开始检测
buttonstopyolo.Enabled = true;
if (videoSourcePlayer.IsRunning)
{
output("开始进行目标检测。", 1);
isyolostart = true;
}
buttonstartyolo.Enabled = false;
}
else
{
//非实时检测
dot2++;
YOLO();
output("[YOLO]帧" + dot2.ToString() + ":目标数量为" + textBoxnum.Text + "。", 1);
}
}
private void buttonstopyolo_Click(object sender, EventArgs e)
{
//停止检测
isyolostart = false;
output("停止进行目标检测。", 1);
buttonstopyolo.Enabled = false;
buttonstartyolo.Enabled = true;
}
public void YOLO()
{
//检测当前图片
Bitmap img = (Bitmap)videoSourcePlayer.GetCurrentVideoFrame().Clone();
yolopic++;
string pictureName = yolopic.ToString() + ".jpg";
using (MemoryStream mem = new MemoryStream())
{
img.Save(pictureName, img.RawFormat);
img.Dispose();
}
DataTable dt = box.Convert(pictureName);
DrawPic(img, dt);
textBoxnum.Text = dt.Rows.Count.ToString();
File.Delete(pictureName);
}
public void DrawPic(Bitmap img, DataTable dt)
{
//绘制标注
Graphics gps = Graphics.FromImage(img);
for (int i = 0; i < dt.Rows.Count; i++)
{
gps.DrawRectangle(new Pen(Color.Red, 1),Convert.ToInt32(dt.Rows[i]["X"]), Convert.ToInt32(dt.Rows[i]["Y"]), Convert.ToInt32(dt.Rows[i]["Width"]), Convert.ToInt32(dt.Rows[i]["Height"]));
gps.DrawString(dt.Rows[i]["Type"].ToString(), Font, Brushes.Black, new PointF(20, 10));
}
pictureBox.Image = img;
}
private void MainForm_Load(object sender, EventArgs e)
{
//窗口启动项
Console.SetOut(new TextBoxWriter(textBoxinfo));
pictureBoxphoto.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
//获取摄像头并添加到coBox_CamList
CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo filterInfo in CaptureDevices)
{
comboBoxcam.Items.Add(filterInfo.Name);
}
output("已加载摄像头列表。", 1);
timer.Start();
CheckCuda();
}
private void buttonsavephoto_Click(object sender, EventArgs e)
{
if (pictureBoxphoto.Image != null)
{
Image img = pictureBoxphoto.Image;
string path = Path.Combine(Application.StartupPath, DateTime.Now.DayOfYear.ToString() + "-" + photo.ToString() + ".jpeg");
img.Save(path,System.Drawing.Imaging.ImageFormat.Jpeg);
output("已保存图片至" + path, 1);
photo++;
}
else
{
MessageBox.Show("没有可保存的照片!");
}
}
private void buttonopenimg_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "图片文件(*.jpg;*.jpeg)|*.jpg;*.jpeg";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
imgFilePath = openFileDialog.FileName;
output("已载入图片:" + Path.GetFileName(imgFilePath), 1);
labelopen.Text = "图片";
pictureBox.Image = Image.FromFile(imgFilePath);
}
}
private void buttonopeniimgf_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.ShowNewFolderButton = false;
folderBrowserDialog.Description = "请选择包含图片的文件夹。";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
imgFolderPath = folderBrowserDialog.SelectedPath;
output("已载入文件夹:" + Path.GetDirectoryName(imgFilePath), 1);
labelopen.Text = "文件夹";
imglist = GetFiles(imgFolderPath);
pictureBox.Image = Image.FromFile(imglist[0]);
}
}
public string[] GetFiles(string dir)
{
string[] files = Directory.GetFiles(dir);
ArrayList alst = new ArrayList();
foreach (string file in files)
{
string exname = file.Substring(file.LastIndexOf(".") + 1);
if (".jpg|.jpeg".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)
{
FileInfo fi = new FileInfo(file);
alst.Add(fi.FullName);
}
}
return (string[])alst.ToArray(typeof(string));
}
private void buttonabout_Click(object sender, EventArgs e)
{
MessageBox.Show("本程序由Hdu李呤泽开发,仅用于课堂学习。", "关于");
}
public void CheckCuda()
{
//检查是否支持CUDA
if (Dlib.IsSupportCuda)
{
textBoxcuda.Text = "已启用";
textBoxcuda.ForeColor = Color.Green;
}
}
}
}