Advertisement
enos

recognitioon

Aug 13th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.29 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 Emgu.CV;
  10. using Emgu.CV.Structure;
  11. using Emgu.CV.CvEnum;
  12. using System.IO;
  13. using System.Diagnostics;
  14. using System.Data.SqlClient;
  15.  
  16. namespace ViolaJones
  17. {
  18.     public partial class FaceRecognition : Form
  19.     {
  20.         Image<Bgr, Byte> currentFrame;
  21.         Capture grabber;
  22.         HaarCascade face;
  23.         HaarCascade eye;
  24.         MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);
  25.         Image<Gray, byte> result, TrainedFace = null;
  26.         Image<Gray, byte> gray = null;
  27.         List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();
  28.         List<string> labels = new List<string>();
  29.         List<string> NamePersons = new List<string>();
  30.         int ContTrain, NumLabels, t;
  31.         string name, names = null;
  32.         string textname = null;
  33.         string textimage = null;
  34.         SqlConnection Connection = new SqlConnection();
  35.         SqlDataAdapter DataSames;
  36.         SqlDataAdapter DataAdapter;
  37.         SqlDataAdapter DataCount;
  38.         DataTable TSTable = new DataTable();
  39.  
  40.         private string datacount()
  41.         {
  42.             string dups = "";
  43.             int cnt = 0;
  44.             int clm = 0;
  45.             string namep = "";
  46.             string txt = "";
  47.             int idp = 0;
  48.            
  49.             DataCount = new SqlDataAdapter("select nim,nama,kelas  from image order by nim asc ", Connection);
  50.            
  51.             SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataCount);
  52.             dups ="";
  53.             DataTable TSSames = new DataTable();
  54.             DataCount.Fill(TSSames);
  55.             if (TSSames.Rows.Count > 0)
  56.             {
  57.                 cnt = TSSames.Rows.Count * 5;
  58.                 clm = TSSames.Rows.Count - 1;
  59.                 int np = 0;
  60.                 foreach (DataRow row in TSSames.Rows)
  61.                 {
  62.                     namep = TSSames.Rows[np]["nim"].ToString();
  63.                     for (int i = 1; i < 6; i++)
  64.                     {
  65.                         txt = txt + namep + "%";
  66.                     }
  67.                     np=np+1;
  68.                 }
  69.  
  70.                
  71.  
  72.                 dups = cnt.ToString() + "%" + txt ;
  73.             }
  74.             return dups;
  75.         }
  76.  
  77.         private string dataimage()
  78.         {
  79.             string dups = "";
  80.             int cnt = 0;
  81.             int clm = 0;
  82.             string namep = "";
  83.             string txt = "";
  84.             DataCount = new SqlDataAdapter("select nim,nama,kelas  from image order by nim asc ", Connection);
  85.             SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataCount);
  86.             dups = "";
  87.             DataTable TSSames = new DataTable();
  88.             DataCount.Fill(TSSames);
  89.             if (TSSames.Rows.Count > 0)
  90.             {
  91.                 cnt = TSSames.Rows.Count * 5;
  92.                 clm = TSSames.Rows.Count - 1;
  93.                 int np = 0;
  94.                 foreach (DataRow row in TSSames.Rows)
  95.                 {
  96.                     namep = TSSames.Rows[np]["nim"].ToString();
  97.                     for (int i = 1; i < 6; i++)
  98.                     {
  99.                         string curFile = Application.StartupPath + "/gambar/" + namep + i + ".bmp";
  100.                         if (File.Exists(curFile))
  101.                         {
  102.                             txt = txt + namep + i + "%";
  103.                         }
  104.                         else
  105.                         {
  106.                             txt = txt + namep + "1%";
  107.                         }
  108.  
  109.                        
  110.                     }
  111.                     np = np + 1;
  112.                 }
  113.                 dups = cnt.ToString() + "%" + txt;
  114.             }
  115.             return dups;
  116.         }
  117.  
  118.          private void datamhs( string nhs)
  119.         {
  120.             string nim = null;
  121.             DataCount = new SqlDataAdapter("select nim,nama,kelas  from image where nim='" + nhs +"'", Connection);
  122.             SqlCommandBuilder CommandBuilder = new SqlCommandBuilder(DataCount);
  123.             DataTable TSSames = new DataTable();
  124.             DataCount.Fill(TSSames);
  125.             if (TSSames.Rows.Count > 0)
  126.             {
  127.                 nim = TSSames.Rows[0]["nim"].ToString();
  128.                 label10.Text = nim;
  129.                 label1.Text = TSSames.Rows[0]["nama"].ToString();
  130.                 label6.Text = TSSames.Rows[0]["kelas"].ToString();
  131.  
  132.                 pictureBox1.Image = null;
  133.                 pictureBox1.Image = Image.FromFile(Application.StartupPath + "/gambar/" + nim + "1.bmp");
  134.             }
  135.         }
  136.  
  137.         public FaceRecognition()
  138.         {
  139.             InitializeComponent();
  140.            
  141.         }
  142.  
  143.         private void FaceRecognition_Load(object sender, EventArgs e)
  144.         {
  145.             Connection.ConnectionString = "Server=IK-PC; Initial Catalog=viola; Integrated Security=true";
  146.             Connection.Open();
  147.             string ns = datacount();
  148.             face = new HaarCascade("haarcascade_frontalface_default.xml");
  149.             txtthreshold.Enabled = true;
  150.             txtthreshold.Text = "3000";
  151.             try
  152.             {
  153.                 textname = datacount();
  154.                 textimage = dataimage();
  155.                 string Labelsinfo = textname;
  156.                 string[] Labels = Labelsinfo.Split('%');
  157.                 NumLabels = Convert.ToInt16(Labels[0]);
  158.                 ContTrain = NumLabels;
  159.                 string imgnameinfo = textimage;
  160.                 string[] ImgName = imgnameinfo.Split('%');
  161.                 string LoadFaces;
  162.  
  163.                 for (int tf = 1; tf < NumLabels + 1; tf++)
  164.                 {
  165.                     LoadFaces = ImgName[tf] + ".bmp";
  166.                     trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/gambar/" + LoadFaces));
  167.                     labels.Add(Labels[tf]);
  168.  
  169.                 }
  170.  
  171.             }
  172.             catch (Exception ex)
  173.             {
  174.                 //MessageBox.Show(e.ToString());
  175.             }
  176.         }
  177.  
  178.         private void button1_Click(object sender, EventArgs e)
  179.         {
  180.             grabber = new Capture();
  181.             grabber.QueryFrame();
  182.             Application.Idle += new EventHandler(FrameGrabber);
  183.             button1.Enabled = false;
  184.             button2.Enabled = true;
  185.            
  186.         }
  187.         void FrameGrabber(object sender, EventArgs e)
  188.         {
  189.             NamePersons.Add("");
  190.             int thres = 10;
  191.             if (txtthreshold.Text != "")
  192.             {
  193.                 thres = Convert.ToInt16(txtthreshold.Text);
  194.             }
  195.             currentFrame = grabber.QueryFrame().Resize(320, 240, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
  196.  
  197.             gray = currentFrame.Convert<Gray, Byte>();
  198.  
  199.             MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
  200.           face,
  201.           1.2,
  202.           10,
  203.           Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
  204.           new Size(20, 20));
  205.  
  206.             foreach (MCvAvgComp f in facesDetected[0])
  207.             {
  208.                 t = t + 1;
  209.                 result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
  210.                 currentFrame.Draw(f.rect, new Bgr(Color.Red), 2);
  211.  
  212.  
  213.                 if (trainingImages.ToArray().Length != 0)
  214.                 {
  215.                     DateTime start = DateTime.Now;
  216.                     MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
  217.  
  218.                     EigenObjectRecognize recognizer = new EigenObjectRecognize(
  219.                        trainingImages.ToArray(),
  220.                        labels.ToArray(),
  221.                        3000,
  222.                        ref termCrit);
  223.  
  224.                     name = recognizer.Recognize(result);
  225.  
  226.                     currentFrame.Draw(name, ref font, new Point(f.rect.X - 2, f.rect.Y - 2), new Bgr(Color.LightGreen));
  227.  
  228.                     lblkecepatan .Text = Math.Round((DateTime.Now - start).TotalSeconds , 3).ToString() + " seconds.";
  229.  
  230.                 }
  231.  
  232.                 NamePersons[t - 1] = name;
  233.                 NamePersons.Add("");
  234.  
  235.             }
  236.             t = 0;
  237.  
  238.             string nim = null;
  239.             for (int nnn = 0; nnn < facesDetected[0].Length; nnn++)
  240.             {
  241.                 names = names + NamePersons[nnn] + ", ";
  242.                 nim = NamePersons[0];
  243.             }
  244.             imageBoxFrameGrabber.Image = currentFrame;
  245.             label4.Text = names;
  246.             label10.Text = "";
  247.             label1.Text = "";
  248.             label6.Text = "";
  249.             pictureBox1.Image = null;
  250.             if (nim != "")
  251.             {
  252.                 datamhs(nim);
  253.             }
  254.             else
  255.             {
  256.                 label10.Text = "";
  257.                 label1.Text = "";
  258.                 label6.Text = "";
  259.                 pictureBox1.Image = null;
  260.             }
  261.  
  262.             names = "";
  263.             NamePersons.Clear();
  264.  
  265.         }
  266.  
  267.         private void button2_Click(object sender, EventArgs e)
  268.         {
  269.             MessageBox.Show(trainingImages.Count.ToString());
  270.         }
  271.  
  272.         private void button2_Click_1(object sender, EventArgs e)
  273.         {
  274.             Connection.Close();
  275.             this.Close();
  276.         }
  277.     }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement