FormMain.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. using Emgu.CV.Face;
  2. using Emgu.CV.Structure;
  3. using Emgu.CV.Util;
  4. using Emgu.CV;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.IO;
  15. using System.Windows.Media.Imaging;
  16. using Emgu.CV.CvEnum;
  17. using System.Runtime.CompilerServices;
  18. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  19. using System.Data.SqlClient;
  20. namespace T_FaceRecognizer
  21. {
  22. public partial class FormMain : Form
  23. {
  24. public FormMain()
  25. {
  26. InitializeComponent();
  27. CaptureTimer = new Timer()
  28. {
  29. Interval = Config.TimerResponseValue
  30. };
  31. CaptureTimer.Tick += CaptureTimer_Tick;
  32. }
  33. private void CaptureTimer_Tick(object sender, EventArgs e)
  34. {
  35. ProcessFrame();
  36. }
  37. #region <Переменные>
  38. SqlConnection SCon = new SqlConnection(@"Data Source=213.155.192.79,3002;Initial Catalog=FaceTrackApp;Persist Security Info=True;User ID=u20teresh;Password=bfg2");
  39. public int SelectedCameraID { get; set; } = 0;
  40. Rectangle CurrentFace;
  41. public string UserName { get; set; } = "Лицо не обнаружено";
  42. /// <summary>
  43. /// ID пользователя, который обнаружен
  44. /// </summary>
  45. private string CurrentUserID = string.Empty;
  46. /// <summary>
  47. /// тип операции 1 - вход, 0- выход
  48. /// </summary>
  49. private int TypeOperation = 0;
  50. #endregion
  51. #region <Свойства>
  52. public event PropertyChangedEventHandler PropertyChanged;
  53. private VideoCapture Capture;
  54. private CascadeClassifier HaarCascade;
  55. private Image<Bgr, Byte> BgrFrame = null;
  56. private Image<Gray, Byte> DetectedFace = null;
  57. private List<FaceData> FaceList = new List<FaceData>();
  58. private VectorOfMat ImageList = new VectorOfMat();
  59. private List<string> NameList = new List<string>();
  60. private VectorOfInt LabelList = new VectorOfInt();
  61. private EigenFaceRecognizer recognizer;
  62. private Timer CaptureTimer;
  63. private Timer CaptureTimerIdent;
  64. private bool IsRecognized = false;
  65. #region FaceName
  66. private string faceName;
  67. public string FaceName
  68. {
  69. get { return faceName; }
  70. set
  71. {
  72. faceName = value;
  73. if (faceName == "Лицо не обнаружено")
  74. {
  75. IsRecognized = false;
  76. }
  77. else
  78. {
  79. IsRecognized = true;
  80. }
  81. UserName = faceName;
  82. NotifyPropertyChanged();
  83. }
  84. }
  85. #endregion
  86. #region CameraCaptureImage
  87. private Bitmap cameraCapture;
  88. public Bitmap CameraCapture
  89. {
  90. get { return cameraCapture; }
  91. set
  92. {
  93. cameraCapture = value;
  94. if (TabPages.SelectedIndex == 1)
  95. {
  96. DrawName(cameraCapture, CurrentFace);
  97. PbxIdentification.Image = cameraCapture;
  98. }
  99. else
  100. {
  101. PbxFaces.Image = cameraCapture;
  102. }
  103. NotifyPropertyChanged();
  104. }
  105. }
  106. #endregion
  107. #region CameraCaptureFaceImage
  108. private Bitmap cameraCaptureFace;
  109. public Bitmap CameraCaptureFace
  110. {
  111. get { return cameraCaptureFace; }
  112. set
  113. {
  114. cameraCaptureFace = value;
  115. PbxFaces.Image = cameraCapture;
  116. NotifyPropertyChanged();
  117. }
  118. }
  119. #endregion
  120. #endregion
  121. /// <summary>
  122. /// отрисовка имени над прямоугольком
  123. /// </summary>
  124. private void DrawName(Bitmap cameraCapture, Rectangle face)
  125. {
  126. Pen PenForFace = new Pen(Brushes.Red, 5);
  127. Brush BrushForFace = Brushes.White;
  128. Font MyFont = new Font("Segoe UI Variable Small Semibol; 12pt; style=Bold", 12, FontStyle.Regular);
  129. Brush BrushInfo = Brushes.White;
  130. Pen PenInfo = new Pen(Brushes.White, 4);
  131. Graphics Graph = Graphics.FromImage(cameraCapture);
  132. //Graph.DrawRectangle(PenForFace, face.X, face.Y, face.Width, face.Height);
  133. //позиция отрисовки имени человека
  134. Point PointName = new Point(face.X, face.Y - 25);
  135. Graph.DrawString($"{UserName}", MyFont, BrushForFace, PointName);
  136. }
  137. /// <summary>
  138. /// получение данных с камеры и обработка изображени
  139. /// </summary>
  140. private void ProcessFrame()
  141. {
  142. BgrFrame = Capture.QueryFrame().ToImage<Bgr, Byte>().Flip(FlipType.Horizontal);
  143. if (BgrFrame != null)
  144. {
  145. try
  146. {//for emgu cv bug
  147. Image<Gray, byte> grayframe = BgrFrame.Convert<Gray, byte>();
  148. Rectangle[] faces = HaarCascade.DetectMultiScale(grayframe, 1.2, 10, new System.Drawing.Size(50, 50), new System.Drawing.Size(200, 200));
  149. //detect face
  150. FaceName = "No face detected";
  151. foreach (var face in faces)
  152. {
  153. BgrFrame.Draw(face, new Bgr(255, 255, 0), 2);
  154. DetectedFace = BgrFrame.Copy(face).Convert<Gray, byte>();
  155. FaceRecognition();
  156. break;
  157. }
  158. CameraCapture = BgrFrame.ToBitmap();
  159. }
  160. catch (Exception ex)
  161. {
  162. //todo log
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// распознавание лица
  168. /// </summary>
  169. private void FaceRecognition()
  170. {
  171. if (ImageList.Size != 0)
  172. {
  173. //Eigen Face Algorithm
  174. FaceRecognizer.PredictionResult result = recognizer.Predict(DetectedFace.Resize(100, 100, Inter.Cubic));
  175. FaceName = NameList[result.Label];
  176. CurrentUserID = FaceList[result.Label].UserID;
  177. CameraCaptureFace = DetectedFace.ToBitmap();
  178. }
  179. else
  180. {
  181. FaceName = "Пожалуйста добавьте лицо";
  182. }
  183. }
  184. /// <summary>
  185. /// Convert bitmap to bitmap image for image control
  186. /// </summary>
  187. /// <param name="bitmap">Bitmap image</param>
  188. /// <returns>Image Source</returns>
  189. private Bitmap BitmapToImageSource(Bitmap bitmap)
  190. {
  191. using (MemoryStream memory = new MemoryStream())
  192. {
  193. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
  194. memory.Position = 0;
  195. BitmapImage bitmapimage = new BitmapImage();
  196. bitmapimage.BeginInit();
  197. bitmapimage.StreamSource = memory;
  198. bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
  199. bitmapimage.EndInit();
  200. //конвертация BitmapImage в Bitmap
  201. BitmapEncoder Enc = new BmpBitmapEncoder();
  202. Enc.Frames.Add(BitmapFrame.Create(bitmapimage));
  203. Enc.Save(memory);
  204. Bitmap Btm = new Bitmap(memory);
  205. return new Bitmap(Btm);
  206. }
  207. }
  208. /// <summary>
  209. /// добавление лица в систему
  210. /// </summary>
  211. private void BtnAdd_Click(object sender, EventArgs e)
  212. {
  213. if (DetectedFace == null)
  214. {
  215. MessageBox.Show("Лица не обнаружены!\r\nУбедитесь что лицо находится в кадре и обведено красным прямоугольником.", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Error);
  216. return;
  217. }
  218. string PhotoPath = string.Empty;
  219. //ProgressOperation.Value = 10;
  220. //TslStatus.Text = "Добавление нового пользователя...";
  221. bool Flag = TbxLastname.Text.Trim() == "" || TbxName.Text.Trim() == "" || TbxMaskPhone.Text.Trim() == "" || TbxMaskSnils.Text.Trim() == "";
  222. if (Flag)
  223. {
  224. MessageBox.Show("Заполните все поля и повторите попытку!", "Ошибка добавления!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  225. return;
  226. }
  227. SCon.Open();
  228. string QueryCheckPassport = $@"select UserID
  229. from Users
  230. where Snils = '{TbxMaskSnils.Text.Trim()}'";
  231. SqlCommand CmdCheck = new SqlCommand(QueryCheckPassport, SCon);
  232. SqlDataReader Res = CmdCheck.ExecuteReader();
  233. if (Res.HasRows)
  234. {
  235. MessageBox.Show("Такой пользователь уже зарегистрирован в системе!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Error);
  236. SCon.Close();
  237. return;
  238. }
  239. SCon.Close();
  240. //изменить размеры изображения
  241. DetectedFace = DetectedFace.Resize(100, 100, Inter.Cubic);
  242. //сохранить изображение в папку
  243. DetectedFace.Save(Config.FacePhotosPath + $"{TbxName.Text.Trim()}{TbxMaskSnils.Text.Trim()}" + Config.ImageFileExtension);
  244. PhotoPath = Config.FacePhotosPath + $"{TbxName.Text.Trim()}{TbxMaskSnils.Text.Trim()}" + Config.ImageFileExtension;
  245. //добавление пользователя в БД
  246. SCon.Open();
  247. string QueryAdd = $@"insert into Users (Lastname,Firstname,Patronymic,Phone,Snils,Photo)
  248. values (@last,@first,@patr,@phone,@snils,@photo)
  249. ";
  250. SqlCommand Cmd = new SqlCommand(QueryAdd, SCon);
  251. Cmd.Parameters.AddWithValue("@last", TbxLastname.Text.Trim());
  252. Cmd.Parameters.AddWithValue("@first", TbxName.Text.Trim());
  253. Cmd.Parameters.AddWithValue("@patr", TbxPatronymic.Text.Trim());
  254. Cmd.Parameters.AddWithValue("@phone", TbxMaskPhone.Text.Trim());
  255. Cmd.Parameters.AddWithValue("@snils", TbxMaskSnils.Text.Trim());
  256. Cmd.Parameters.AddWithValue("@photo", PhotoPath);
  257. Cmd.ExecuteNonQuery();//выполнить запрос
  258. SCon.Close();
  259. MessageBox.Show("Успешно сохранено!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Information);
  260. }
  261. /// <summary>
  262. /// получение данных об изображениях
  263. /// </summary>
  264. public void GetFacesList()
  265. {
  266. //haar cascade classifier
  267. if (!File.Exists(Config.HaarCascadePath))
  268. {
  269. string text = "Файл каскада Хаара не обнаружен:\n\n";
  270. text += Config.HaarCascadePath;
  271. DialogResult result = MessageBox.Show(text, "Error",
  272. MessageBoxButtons.OK, MessageBoxIcon.Error);
  273. }
  274. HaarCascade = new CascadeClassifier(Config.HaarCascadePath);
  275. FaceList.Clear();
  276. // Создать директорию, если она отсутствовала
  277. if (!Directory.Exists(Config.FacePhotosPath))
  278. {
  279. Directory.CreateDirectory(Config.FacePhotosPath);
  280. }
  281. // Тренировать изображения
  282. if (ImageList.Size > 0)
  283. {
  284. recognizer = new EigenFaceRecognizer(ImageList.Size);
  285. recognizer.Train(ImageList, LabelList);
  286. }
  287. }
  288. protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
  289. {
  290. var handler = PropertyChanged;
  291. if (handler != null)
  292. handler(this, new PropertyChangedEventArgs(propertyName));
  293. }
  294. private void FormMain_Load(object sender, EventArgs e)
  295. {
  296. //скрытие заголовков вкладок в TabControl
  297. TabPages.Appearance = TabAppearance.FlatButtons;
  298. TabPages.ItemSize = new Size(0, 1);
  299. TabPages.SizeMode = TabSizeMode.Fixed;
  300. TabPages.SelectTab(0);
  301. GetFacesList();
  302. Capture = new VideoCapture(Config.ActiveCameraIndex);
  303. Capture.SetCaptureProperty(CapProp.Fps, 30);
  304. Capture.SetCaptureProperty(CapProp.FrameHeight, 450);
  305. Capture.SetCaptureProperty(CapProp.FrameWidth, 370);
  306. CaptureTimer.Start();
  307. }
  308. private void PbxMenu_Click(object sender, EventArgs e)
  309. {
  310. if(SplitContainer.Panel1.Width != 45)
  311. {
  312. SplitContainer.SplitterDistance = 45;
  313. PanHome.BackColor = Color.FromArgb(243, 243, 243);
  314. PanAdd.BackColor = Color.FromArgb(243, 243, 243);
  315. PanIdentification.BackColor = Color.FromArgb(243, 243, 243);
  316. PanJournal.BackColor = Color.FromArgb(243, 243, 243);
  317. PanSettings.BackColor = Color.FromArgb(243, 243, 243);
  318. }
  319. else
  320. {
  321. SplitContainer.SplitterDistance = 250;
  322. PanHome.BackColor = Color.FromArgb(58, 166, 64);
  323. PanAdd.BackColor = Color.FromArgb(58, 166, 64);
  324. PanIdentification.BackColor = Color.FromArgb(58, 166, 64);
  325. PanJournal.BackColor = Color.FromArgb(58, 166, 64);
  326. PanSettings.BackColor = Color.FromArgb(58, 166, 64);
  327. }
  328. }
  329. private void FormMain_Resize(object sender, EventArgs e)
  330. {
  331. //PbxMenu_Click(sender, e);
  332. }
  333. private void FormMain_ResizeEnd(object sender, EventArgs e)
  334. {
  335. //PbxMenu_Click(sender, e);
  336. }
  337. private void BtnMain_Click(object sender, EventArgs e)
  338. {
  339. TabPages.SelectTab(4);
  340. }
  341. private void BtnAddUser_Click(object sender, EventArgs e)
  342. {
  343. TabPages.SelectTab(0);
  344. }
  345. private void BtnIdent_Click(object sender, EventArgs e)
  346. {
  347. CaptureTimerIdent = new Timer()
  348. {
  349. Interval = Config.TimerResponseValue
  350. };
  351. CaptureTimerIdent.Tick += CaptureTimerIdent_Tick;
  352. try
  353. {
  354. if (BgrFrame != null)
  355. {
  356. BgrFrame = null;
  357. Capture.Dispose();
  358. CaptureTimer.Tick -= CaptureTimer_Tick;
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. MessageBox.Show(ex.Message, "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  364. }
  365. GetFacesListForIdentification();
  366. Capture = new VideoCapture(SelectedCameraID);
  367. //настройка кадров
  368. Capture.SetCaptureProperty(CapProp.Fps, 30);
  369. Capture.SetCaptureProperty(CapProp.FrameHeight, 450);
  370. Capture.SetCaptureProperty(CapProp.FrameWidth, 370);
  371. CaptureTimerIdent.Start();
  372. TabPages.SelectTab(1);
  373. }
  374. private void CaptureTimerIdent_Tick(object sender, EventArgs e)
  375. {
  376. ProcessFrameIndentification();
  377. }
  378. /// <summary>
  379. /// получение данных с камеры и распознавание
  380. /// </summary>
  381. private void ProcessFrameIndentification()
  382. {
  383. BgrFrame = Capture.QueryFrame().ToImage<Bgr, Byte>().Flip(FlipType.Horizontal);
  384. if (BgrFrame != null)
  385. {
  386. try
  387. {//for emgu cv bug
  388. Image<Gray, byte> grayframe = BgrFrame.Convert<Gray, byte>();
  389. Rectangle[] faces = HaarCascade.DetectMultiScale(grayframe, 1.2, 10, new System.Drawing.Size(50, 50), new System.Drawing.Size(200, 200));
  390. FaceName = "Лицо не обнаружено";
  391. foreach (var face in faces)
  392. {
  393. CurrentFace = face;
  394. BgrFrame.Draw(face, new Bgr(53, 23, 247), 2);
  395. DetectedFace = BgrFrame.Copy(face).Convert<Gray, byte>();
  396. FaceRecognition();
  397. break;
  398. }
  399. CameraCapture = BgrFrame.ToBitmap();
  400. }
  401. catch (Exception ex)
  402. {
  403. MessageBox.Show(ex.Message);
  404. }
  405. }
  406. }
  407. private void GetFacesListForIdentification()
  408. {
  409. //файл Хаара
  410. if (!File.Exists(Config.HaarCascadePath))
  411. {
  412. string text = "Не удаётся найти файл данных - каскад Хаара:\n\n";
  413. text += Config.HaarCascadePath;
  414. DialogResult result = MessageBox.Show(text, "Ошибка",
  415. MessageBoxButtons.OK, MessageBoxIcon.Error);
  416. }
  417. HaarCascade = new CascadeClassifier(Config.HaarCascadePath);
  418. FaceList.Clear();
  419. FaceData FaceItem = null;
  420. //создать папку для фото, если она отсутствовала
  421. if (!Directory.Exists(Config.FacePhotosPath))
  422. {
  423. Directory.CreateDirectory(Config.FacePhotosPath);
  424. }
  425. //получить из БД инфо о пользователе (id, имя, фамилия, путь до фото)
  426. SCon.Open();
  427. string QueryGetInfoAboutUser = $@"select UserID ,Users.Lastname, Users.Firstname, Users.Patronymic, Photo
  428. from Users";
  429. SqlCommand Cmd = new SqlCommand(QueryGetInfoAboutUser, SCon);
  430. SqlDataReader Res = Cmd.ExecuteReader();
  431. if (Res.HasRows)
  432. {
  433. while (Res.Read())
  434. {
  435. FaceItem = new FaceData();
  436. FaceItem.FaceImage = new Image<Gray, byte>(Application.StartupPath + "\\" + Res["Photo"].ToString());
  437. FaceItem.PersonName = Res["Firstname"].ToString();
  438. FaceItem.LastName = Res["Lastname"].ToString();
  439. FaceItem.Patronymic = Res["Patronymic"].ToString();
  440. FaceItem.UserID = Res["UserID"].ToString();
  441. FaceList.Add(FaceItem);
  442. }
  443. }
  444. else
  445. {
  446. SCon.Close();
  447. MessageBox.Show("Данные о пользователях отсутствуют!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  448. return;
  449. }
  450. SCon.Close();
  451. int i = 0;
  452. foreach (var face in FaceList)
  453. {
  454. ImageList.Push(face.FaceImage.Mat);
  455. NameList.Add(face.PersonName + " " + face.LastName);
  456. LabelList.Push(new[] { i++ });
  457. }
  458. // Тренировка распознавания
  459. if (ImageList.Size > 0)
  460. {
  461. recognizer = new EigenFaceRecognizer(ImageList.Size);
  462. recognizer.Train(ImageList, LabelList);
  463. }
  464. }
  465. private void BtnIn_Click(object sender, EventArgs e)
  466. {
  467. TypeOperation = 1;
  468. RegisterVisit(TypeOperation);
  469. }
  470. private void RegisterVisit(int typeOperation)
  471. {
  472. if (!IsRecognized)
  473. {
  474. MessageBox.Show("Убедитесь, что лицо находится в кадре и обведено красным прямоугольником!", "Ошибка распознавания!");
  475. return;
  476. }
  477. if (typeOperation == 1)
  478. {
  479. #region Предупреждение при попытке повторной идентификации пользователя за один день
  480. SCon.Open();
  481. string QueryCheckExistsVisit = $@"select Count(ID) as Cnt
  482. from Visits
  483. where UsersID = '{CurrentUserID}' and [Date] = CAST(GETDATE() as date)";
  484. SqlCommand CmdCheckVisit = new SqlCommand(QueryCheckExistsVisit, SCon);
  485. SqlDataReader Res = CmdCheckVisit.ExecuteReader();
  486. Res.Read();
  487. int n = int.Parse(Res["Cnt"].ToString());
  488. if (n >= 1)
  489. {
  490. MessageBox.Show("Сегодня данный пользователь уже прошёл идентификацию при входе!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  491. SCon.Close();
  492. return;
  493. }
  494. SCon.Close();
  495. #endregion
  496. //регистрация посещения
  497. SCon.Open();
  498. string QueryAddVisit = $@"set dateformat dmy
  499. insert into Visits (UsersID,[Date],TimeEntrance)
  500. values ('{CurrentUserID}',CAST(GETDATE() as date),'{DateTime.Now.ToString("HH:mm:ss")}')
  501. ";
  502. SqlCommand Cmd = new SqlCommand(QueryAddVisit, SCon);
  503. Cmd.ExecuteNonQuery();
  504. SCon.Close();
  505. MessageBox.Show("Вход прошёл!");
  506. }
  507. else
  508. {
  509. SCon.Open();
  510. string QueryCheckRecord = $@"set dateformat dmy
  511. select *
  512. from Visits
  513. where UsersID = '1' and [Date] = cast(GETDATE() as date) and TimeExit is null
  514. ";
  515. SqlCommand CmdCheck = new SqlCommand(QueryCheckRecord, SCon);
  516. SqlDataReader Res = CmdCheck.ExecuteReader();
  517. if (!Res.HasRows)
  518. {
  519. MessageBox.Show("Данный пользователь уже вышел!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  520. SCon.Close();
  521. return;
  522. }
  523. SCon.Close();
  524. //!!! Выход пользователя, который не прошёл идентификацию не регистрируется
  525. SCon.Open();
  526. string QueryTimeExit = $@"update Visits
  527. set TimeExit = '{DateTime.Now.ToString("HH:mm:ss")}'
  528. where UsersID = '1' and [Date] = cast(GETDATE() as date)";
  529. SqlCommand Cmd = new SqlCommand(QueryTimeExit, SCon);
  530. Cmd.ExecuteNonQuery();
  531. SCon.Close();
  532. MessageBox.Show("Выход зарегистрирован!");
  533. }
  534. }
  535. private void BtnOut_Click(object sender, EventArgs e)
  536. {
  537. TypeOperation = 0;
  538. RegisterVisit(TypeOperation);
  539. }
  540. }
  541. }