Guest User

Untitled

a guest
Dec 2nd, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. using Emgu.CV.UI;
  12. using Emgu.CV;
  13. using Emgu.CV.Structure;
  14. using Emgu.CV.CvEnum;
  15.  
  16.  
  17. using Renci.SshNet;
  18.  
  19. namespace Face_Recognition
  20. {
  21. public partial class NewUser : Form
  22. {
  23.  
  24. public NewUser()
  25. {
  26. InitializeComponent();
  27. User.InitializeDB();
  28. textBox1.Text = (User.getMaxId() + 1).ToString();
  29. button1.Enabled = true;
  30. Application.Idle += new EventHandler(SceneCamera);
  31.  
  32. }
  33. Capture cap;
  34. bool activeCamera = false;
  35. static Image<Gray, Byte> zdjecie, result;
  36. public static Image<Gray, Byte>[] newPhotos = new Image<Gray, byte>[10];
  37. static int CountImage = 1;
  38. static String sciezka;
  39. static String PathNewUser;
  40. String lastID = User.LastID().ToString();
  41. static bool TookFoto = false;
  42. private User currUser;
  43. public static int laID;
  44. // private CascadeClassifier _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_default.xml");
  45. public static bool StartFormNewUser = true;
  46. public void SceneCamera(object sender, EventArgs e)//nowa
  47. {
  48. try
  49. {
  50. if (!activeCamera)
  51. cap = new Capture(Form1.Camera);
  52. activeCamera = true;
  53. Image<Bgr, byte> frame = cap.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); ;
  54. if (frame != null)
  55. using (var imageFrame = frame.Convert<Bgr, Byte>())
  56. {
  57. if (imageFrame != null)
  58. {
  59. var grayframe = imageFrame.Convert<Gray, byte>();
  60. Rectangle[] faces = Form1.Face.DetectMultiScale(grayframe, 1.2, 10, new Size(50, 50), Size.Empty);
  61. foreach (var face in faces)
  62. {
  63. Rectangle fac = face;
  64. fac.X += (int)(fac.Height * 0.1);
  65. fac.Y += (int)(fac.Width * 0.22);
  66. fac.Height -= (int)(fac.Height * 0.3);
  67. fac.Width -= (int)(fac.Width * 0.2);
  68. result = imageFrame.Copy(fac).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
  69. imageFrame.Draw(fac, new Bgr(Color.BurlyWood), 2); //the detected face(s) is highlighted here using a box that is drawn around it/them
  70. }
  71. }
  72. if (imageFrame != null)
  73. pictureBox1.Image = imageFrame.ToBitmap();
  74. else
  75. pictureBox1.Image = frame.ToBitmap();
  76.  
  77. if (result != null)
  78. {
  79. result = result
  80. .Convert<Gray, Byte>()
  81. .Resize(76, 64, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
  82. }
  83. Save();
  84. }
  85. }
  86. catch (Exception Ex)
  87. {
  88. // MessageBox.Show(Ex.Message);
  89. }
  90. }
  91. public void Save()
  92. {
  93. if (TookFoto == true && CountImage <= 10)
  94. {
  95. String s1 = "NewUserFaces/u";
  96. String s2 = CountImage.ToString();
  97. String s3 = ".jpg";
  98. PathNewUser = s1 + s2 + s3;
  99. newPhotos[CountImage - 1] = result;
  100. CountImage++;
  101. result.Save(PathNewUser);
  102. button1.Enabled = false;
  103. pictureBox2.Image = result.ToBitmap();
  104. pictureBox2.Refresh();
  105. button1.Text = "Wait";
  106. }
  107. else
  108. {
  109. button1.Text = "Record Faces";
  110. TookFoto = false;
  111. CountImage = 1;
  112. button1.Enabled = true;
  113. }
  114. }
  115. public static void SSHUpload(String localD, String SaveFoto) //zapisywanie zdjec do przestrzenii dyskowej
  116. {
  117. const String host = "212.237.20.10";
  118. const String username = "root";
  119. const String password = "T{1*z1#oye";
  120. const String remoteDirectory = "/home/photos";
  121. // const String localDirectory = @"c: \users\patryk\desktop\tiger2.jpg";
  122. String localDirectory = localD;
  123. const int port = 22;
  124.  
  125. // textBox1.Text = "Tworzenie klienta i łączenie";
  126.  
  127. using (var client = new SftpClient(host, port, username, password))
  128. {
  129. client.Connect();
  130. client.ChangeDirectory(remoteDirectory);
  131. var listDirectory = client.ListDirectory(remoteDirectory);
  132. using (var fileStream = new FileStream(localDirectory, FileMode.Open))
  133. {
  134. client.BufferSize = 4 * 1024;
  135. client.UploadFile(fileStream, Path.GetFileName(SaveFoto));
  136. // textBox1.Text = "Upload Succesfull";
  137. }
  138. }
  139. }
  140. public void ShowImageForm(Image<Gray, byte> imageSmall, Image<Bgr, byte> imageBig)
  141. {
  142. try
  143. {
  144. if (imageBig != null)
  145. {
  146. pictureBox1.Image = imageBig.ToBitmap(); ;
  147. result = imageSmall;
  148. Save();
  149. }
  150.  
  151. }
  152. catch (Exception Ex)
  153. {
  154. MessageBox.Show(Ex.Message);
  155. }
  156.  
  157. }
  158. public static void SaveImageToData(int setID)
  159. {
  160. try
  161. {
  162. if (newPhotos[0] != null)
  163. {
  164. String s1 = "facesdata/u";
  165. laID = setID;
  166. String s2 = laID.ToString();
  167. char s3 = '/';
  168. int b = 1;
  169. String s5 = ".jpg";
  170. Directory.CreateDirectory(s1 + s2);
  171. sciezka = s1 + s2;
  172. String saveToDataImage = s1 + s2 + s3 + b.ToString() + s5;
  173. String fileImage = laID + s5;
  174.  
  175. foreach (var i in newPhotos)
  176. {
  177. Image<Gray, Byte> faceGrayPic = i
  178. .Convert<Gray, Byte>()
  179. .Resize(76, 64, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
  180. Form1.trainingImages.Add(faceGrayPic);
  181. Form1.Names_List_ID.Add(laID);
  182. String s4 = b.ToString();
  183. b++;
  184. String s6 = s1 + s2 + s3 + s4 + s5;
  185. i.Save(s6);
  186. }
  187. for (int i = 0; i < 10; i++)
  188. newPhotos[i] = null;
  189. SSHUpload(saveToDataImage, fileImage);
  190. }
  191. }
  192. catch (Exception Ex)
  193. {
  194. MessageBox.Show(Ex.Message);
  195. MessageBox.Show("Nie dodano zdjęcia użytkownika, aby uzupełnić brakujące dane, udaj się do Users");
  196. }
  197. }
  198.  
  199. private void button1_Click(object sender, EventArgs e)
  200. {
  201. TookFoto = true;
  202. }
  203.  
  204. private void button2_Click(object sender, EventArgs e)
  205. {
  206. SaveImageToData((User.getMaxId() + 1));
  207. String Id = laID.ToString();
  208. String u = textBox2.Text;
  209. String p = textBox3.Text;
  210. String r = sciezka;
  211. if (String.IsNullOrEmpty(u) || String.IsNullOrEmpty(p))
  212. {
  213. MessageBox.Show("It's empty");
  214. return;
  215. }
  216. currUser = User.Insert(Id, u, p, r);
  217. Form1.TrainFace();
  218. // Application.Idle -= new EventHandler(VideoCamera);
  219. Form1.numberForm = 0;
  220. Application.Idle -= new EventHandler(SceneCamera);
  221. this.Close();
  222. }
  223. private void NewUser_FormClosed(object sender, FormClosedEventArgs e)
  224. {
  225. Form1.numberForm = 0;
  226. //Application.Idle -= new EventHandler(VideoCamera);
  227. }
  228.  
  229. private void button3_Click(object sender, EventArgs e)
  230. {
  231. // label
  232. if (textBox1.Text != "")
  233. {
  234. Form1.serialPort1.WriteLine(textBox1.Text);
  235. //for(int i = 0; i < 100; i++)
  236. int i = 0;
  237. while (true)
  238. {
  239. try
  240. {
  241. string message = Form1.serialPort1.ReadLine();
  242. if (message != "0")
  243. {
  244. i++;
  245. label6.Text = message;
  246. if (i == 2)
  247. return;
  248. }
  249.  
  250. }
  251. catch (TimeoutException) { }
  252. }
  253. }
  254. }
  255.  
  256. private void button1_Click_1(object sender, EventArgs e)
  257. {
  258. TookFoto = true;
  259. }
  260.  
  261. private void LoadAll()
  262. {
  263. List<User> users = User.GetUsers();
  264. }
  265. }
  266. }
Add Comment
Please, Sign In to add comment