Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using Emgu.CV.UI;
- using Emgu.CV;
- using Emgu.CV.Structure;
- using Emgu.CV.CvEnum;
- using Renci.SshNet;
- namespace Face_Recognition
- {
- public partial class NewUser : Form
- {
- public NewUser()
- {
- InitializeComponent();
- User.InitializeDB();
- textBox1.Text = (User.getMaxId() + 1).ToString();
- button1.Enabled = true;
- Application.Idle += new EventHandler(SceneCamera);
- }
- Capture cap;
- bool activeCamera = false;
- static Image<Gray, Byte> zdjecie, result;
- public static Image<Gray, Byte>[] newPhotos = new Image<Gray, byte>[10];
- static int CountImage = 1;
- static String sciezka;
- static String PathNewUser;
- String lastID = User.LastID().ToString();
- static bool TookFoto = false;
- private User currUser;
- public static int laID;
- // private CascadeClassifier _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_default.xml");
- public static bool StartFormNewUser = true;
- public void SceneCamera(object sender, EventArgs e)//nowa
- {
- try
- {
- if (!activeCamera)
- cap = new Capture(Form1.Camera);
- activeCamera = true;
- Image<Bgr, byte> frame = cap.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC); ;
- if (frame != null)
- using (var imageFrame = frame.Convert<Bgr, Byte>())
- {
- if (imageFrame != null)
- {
- var grayframe = imageFrame.Convert<Gray, byte>();
- Rectangle[] faces = Form1.Face.DetectMultiScale(grayframe, 1.2, 10, new Size(50, 50), Size.Empty);
- foreach (var face in faces)
- {
- Rectangle fac = face;
- fac.X += (int)(fac.Height * 0.1);
- fac.Y += (int)(fac.Width * 0.22);
- fac.Height -= (int)(fac.Height * 0.3);
- fac.Width -= (int)(fac.Width * 0.2);
- result = imageFrame.Copy(fac).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- imageFrame.Draw(fac, new Bgr(Color.BurlyWood), 2); //the detected face(s) is highlighted here using a box that is drawn around it/them
- }
- }
- if (imageFrame != null)
- pictureBox1.Image = imageFrame.ToBitmap();
- else
- pictureBox1.Image = frame.ToBitmap();
- if (result != null)
- {
- result = result
- .Convert<Gray, Byte>()
- .Resize(76, 64, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- }
- Save();
- }
- }
- catch (Exception Ex)
- {
- // MessageBox.Show(Ex.Message);
- }
- }
- public void Save()
- {
- if (TookFoto == true && CountImage <= 10)
- {
- String s1 = "NewUserFaces/u";
- String s2 = CountImage.ToString();
- String s3 = ".jpg";
- PathNewUser = s1 + s2 + s3;
- newPhotos[CountImage - 1] = result;
- CountImage++;
- result.Save(PathNewUser);
- button1.Enabled = false;
- pictureBox2.Image = result.ToBitmap();
- pictureBox2.Refresh();
- button1.Text = "Wait";
- }
- else
- {
- button1.Text = "Record Faces";
- TookFoto = false;
- CountImage = 1;
- button1.Enabled = true;
- }
- }
- public static void SSHUpload(String localD, String SaveFoto) //zapisywanie zdjec do przestrzenii dyskowej
- {
- const String host = "212.237.20.10";
- const String username = "root";
- const String password = "T{1*z1#oye";
- const String remoteDirectory = "/home/photos";
- // const String localDirectory = @"c: \users\patryk\desktop\tiger2.jpg";
- String localDirectory = localD;
- const int port = 22;
- // textBox1.Text = "Tworzenie klienta i łączenie";
- using (var client = new SftpClient(host, port, username, password))
- {
- client.Connect();
- client.ChangeDirectory(remoteDirectory);
- var listDirectory = client.ListDirectory(remoteDirectory);
- using (var fileStream = new FileStream(localDirectory, FileMode.Open))
- {
- client.BufferSize = 4 * 1024;
- client.UploadFile(fileStream, Path.GetFileName(SaveFoto));
- // textBox1.Text = "Upload Succesfull";
- }
- }
- }
- public void ShowImageForm(Image<Gray, byte> imageSmall, Image<Bgr, byte> imageBig)
- {
- try
- {
- if (imageBig != null)
- {
- pictureBox1.Image = imageBig.ToBitmap(); ;
- result = imageSmall;
- Save();
- }
- }
- catch (Exception Ex)
- {
- MessageBox.Show(Ex.Message);
- }
- }
- public static void SaveImageToData(int setID)
- {
- try
- {
- if (newPhotos[0] != null)
- {
- String s1 = "facesdata/u";
- laID = setID;
- String s2 = laID.ToString();
- char s3 = '/';
- int b = 1;
- String s5 = ".jpg";
- Directory.CreateDirectory(s1 + s2);
- sciezka = s1 + s2;
- String saveToDataImage = s1 + s2 + s3 + b.ToString() + s5;
- String fileImage = laID + s5;
- foreach (var i in newPhotos)
- {
- Image<Gray, Byte> faceGrayPic = i
- .Convert<Gray, Byte>()
- .Resize(76, 64, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- Form1.trainingImages.Add(faceGrayPic);
- Form1.Names_List_ID.Add(laID);
- String s4 = b.ToString();
- b++;
- String s6 = s1 + s2 + s3 + s4 + s5;
- i.Save(s6);
- }
- for (int i = 0; i < 10; i++)
- newPhotos[i] = null;
- SSHUpload(saveToDataImage, fileImage);
- }
- }
- catch (Exception Ex)
- {
- MessageBox.Show(Ex.Message);
- MessageBox.Show("Nie dodano zdjęcia użytkownika, aby uzupełnić brakujące dane, udaj się do Users");
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- TookFoto = true;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- SaveImageToData((User.getMaxId() + 1));
- String Id = laID.ToString();
- String u = textBox2.Text;
- String p = textBox3.Text;
- String r = sciezka;
- if (String.IsNullOrEmpty(u) || String.IsNullOrEmpty(p))
- {
- MessageBox.Show("It's empty");
- return;
- }
- currUser = User.Insert(Id, u, p, r);
- Form1.TrainFace();
- // Application.Idle -= new EventHandler(VideoCamera);
- Form1.numberForm = 0;
- Application.Idle -= new EventHandler(SceneCamera);
- this.Close();
- }
- private void NewUser_FormClosed(object sender, FormClosedEventArgs e)
- {
- Form1.numberForm = 0;
- //Application.Idle -= new EventHandler(VideoCamera);
- }
- private void button3_Click(object sender, EventArgs e)
- {
- // label
- if (textBox1.Text != "")
- {
- Form1.serialPort1.WriteLine(textBox1.Text);
- //for(int i = 0; i < 100; i++)
- int i = 0;
- while (true)
- {
- try
- {
- string message = Form1.serialPort1.ReadLine();
- if (message != "0")
- {
- i++;
- label6.Text = message;
- if (i == 2)
- return;
- }
- }
- catch (TimeoutException) { }
- }
- }
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- TookFoto = true;
- }
- private void LoadAll()
- {
- List<User> users = User.GetUsers();
- }
- }
- }
Add Comment
Please, Sign In to add comment