Advertisement
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 Emgu.CV;
- using Emgu.CV.Structure;
- using Emgu.CV.CvEnum;
- using System.IO;
- using System.Diagnostics;
- using System.Data.SqlClient;
- namespace ViolaJones
- {
- public partial class FaceRecognition : Form
- {
- Image<Bgr, Byte> currentFrame;
- Capture grabber;
- HaarCascade face;
- HaarCascade eye;
- MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
- Image<Gray, byte> result, TrainedFace = null;
- Image<Gray, byte> gray = null;
- List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
- List<string> labels = new List<string>();
- List<string> NamePersons = new List<string>();
- int ContTrain, NumLabels, t;
- string name, names = null;
- string textname = null;
- string textimage = null;
- SqlConnection Connection = new SqlConnection();
- SqlDataAdapter DataSames;
- SqlDataAdapter DataAdapter;
- SqlDataAdapter DataCount;
- DataTable TSTable = new DataTable();
- private string datacount()
- {
- string dups = "";
- int cnt = 0;
- int clm = 0;
- string namep = "";
- string txt = "";
- int idp = 0;
- DataCount = new SqlDataAdapter("select nim,nama,kelas from image order by nim asc ", Connection);
- SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataCount);
- dups ="";
- DataTable TSSames = new DataTable();
- DataCount.Fill(TSSames);
- if (TSSames.Rows.Count > 0)
- {
- cnt = TSSames.Rows.Count * 5;
- clm = TSSames.Rows.Count - 1;
- int np = 0;
- foreach (DataRow row in TSSames.Rows)
- {
- namep = TSSames.Rows[np]["nim"].ToString();
- for (int i = 1; i < 6; i++)
- {
- txt = txt + namep + "%";
- }
- np=np+1;
- }
- dups = cnt.ToString() + "%" + txt ;
- }
- return dups;
- }
- private string dataimage()
- {
- string dups = "";
- int cnt = 0;
- int clm = 0;
- string namep = "";
- string txt = "";
- DataCount = new SqlDataAdapter("select nim,nama,kelas from image order by nim asc ", Connection);
- SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataCount);
- dups = "";
- DataTable TSSames = new DataTable();
- DataCount.Fill(TSSames);
- if (TSSames.Rows.Count > 0)
- {
- cnt = TSSames.Rows.Count * 5;
- clm = TSSames.Rows.Count - 1;
- int np = 0;
- foreach (DataRow row in TSSames.Rows)
- {
- namep = TSSames.Rows[np]["nim"].ToString();
- for (int i = 1; i < 6; i++)
- {
- string curFile = Application.StartupPath + "/gambar/" + namep + i + ".bmp";
- if (File.Exists(curFile))
- {
- txt = txt + namep + i + "%";
- }
- else
- {
- txt = txt + namep + "1%";
- }
- }
- np = np + 1;
- }
- dups = cnt.ToString() + "%" + txt;
- }
- return dups;
- }
- private void datamhs( string nhs)
- {
- string nim = null;
- DataCount = new SqlDataAdapter("select nim,nama,kelas from image where nim='" + nhs +"'", Connection);
- SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataCount);
- DataTable TSSames = new DataTable();
- DataCount.Fill(TSSames);
- if (TSSames.Rows.Count > 0)
- {
- nim = TSSames.Rows[0]["nim"].ToString();
- label10.Text = nim;
- label1.Text = TSSames.Rows[0]["nama"].ToString();
- label6.Text = TSSames.Rows[0]["kelas"].ToString();
- pictureBox1.Image = null;
- pictureBox1.Image = Image.FromFile(Application.StartupPath + "/gambar/" + nim + "1.bmp");
- }
- }
- public FaceRecognition()
- {
- InitializeComponent();
- }
- private void FaceRecognition_Load(object sender, EventArgs e)
- {
- Connection.ConnectionString = "Server=IK-PC; Initial Catalog=viola; Integrated Security=true";
- Connection.Open();
- string ns = datacount();
- face = new HaarCascade("haarcascade_frontalface_default.xml");
- txtthreshold.Enabled = true;
- txtthreshold.Text = "3000";
- try
- {
- textname = datacount();
- textimage = dataimage();
- string Labelsinfo = textname;
- string[] Labels = Labelsinfo.Split('%');
- NumLabels = Convert.ToInt16(Labels[0]);
- ContTrain = NumLabels;
- string imgnameinfo = textimage;
- string[] ImgName = imgnameinfo.Split('%');
- string LoadFaces;
- for (int tf = 1; tf < NumLabels + 1; tf++)
- {
- LoadFaces = ImgName[tf] + ".bmp";
- trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/gambar/" + LoadFaces));
- labels.Add(Labels[tf]);
- }
- }
- catch (Exception ex)
- {
- //MessageBox.Show(e.ToString());
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- grabber = new Capture();
- grabber.QueryFrame();
- Application.Idle += new EventHandler(FrameGrabber);
- button1.Enabled = false;
- button2.Enabled = true;
- }
- void FrameGrabber(object sender, EventArgs e)
- {
- NamePersons.Add("");
- int thres = 10;
- if (txtthreshold.Text != "")
- {
- thres = Convert.ToInt16(txtthreshold.Text);
- }
- currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- gray = currentFrame.Convert<Gray, Byte>();
- MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
- face,
- 1.2,
- 10,
- Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
- new Size(20, 20));
- foreach (MCvAvgComp f in facesDetected[0])
- {
- t = t + 1;
- result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
- currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);
- if (trainingImages.ToArray().Length != 0)
- {
- DateTime start = DateTime.Now;
- MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
- EigenObjectRecognize recognizer = new EigenObjectRecognize(
- trainingImages.ToArray(),
- labels.ToArray(),
- 3000,
- ref termCrit);
- name = recognizer.Recognize(result);
- currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));
- lblkecepatan .Text = Math.Round((DateTime.Now - start).TotalSeconds , 3).ToString() + " seconds.";
- }
- NamePersons[t - 1] = name;
- NamePersons.Add("");
- }
- t = 0;
- string nim = null;
- for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
- {
- names = names + NamePersons[nnn] + ", ";
- nim = NamePersons[0];
- }
- imageBoxFrameGrabber.Image = currentFrame;
- label4.Text = names;
- label10.Text = "";
- label1.Text = "";
- label6.Text = "";
- pictureBox1.Image = null;
- if (nim != "")
- {
- datamhs(nim);
- }
- else
- {
- label10.Text = "";
- label1.Text = "";
- label6.Text = "";
- pictureBox1.Image = null;
- }
- names = "";
- NamePersons.Clear();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- MessageBox.Show(trainingImages.Count.ToString());
- }
- private void button2_Click_1(object sender, EventArgs e)
- {
- Connection.Close();
- this.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement