Exemplo n.º 1
0
        public void videoProcess(object obj)
        {
            string vedioFile = (string)obj;

            if (FaceApi.OpenVideo(vedioFile))
            {
                FaceApi.ImageDllcallBack dllcall = new FaceApi.ImageDllcallBack(CsharpCall);
                if (textBox1.Text == "" || textBox2.Text == "")
                {
                    while (!stop && FaceApi.GetVideoFrame(dllcall, false))
                    {
                        ;
                    }
                }
                else
                {
                    while (!stop && FaceApi.GetVideoFrame(dllcall, true, Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)))
                    {
                        ;
                    }
                }
                CreateBitmap(null);
                FaceApi.CloseVideo();
            }
            else
            {
                MessageBox.Show("视频打开失败!");
            }
        }
Exemplo n.º 2
0
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
     //DialogResult result = MessageBox.Show("你确定要关闭吗!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
     //if (result == DialogResult.OK)
     //{
     //    e.Cancel = false;  //点击OK
     //    closing = true;
     //btnClose_Click(null, null);
     //saveFaceData();
     //DelModel();
     //}
     //else
     //{
     //    e.Cancel = true;
     //}
     if (!closing)
     {
         closing = true;
         CloseThread();
         if (addIDThread != null && addIDThread.ThreadState != ThreadState.Stopped)
         {
             addIDThread.Abort();
             addIDThread.Join();
             addIDThread = null;
         }
         if (addIDAllThread != null && addIDAllThread.ThreadState != ThreadState.Stopped)
         {
             addIDAllThread.Abort();
             addIDAllThread.Join();
             addIDAllThread = null;
         }
         if (matchingThread != null && matchingThread.ThreadState != ThreadState.Stopped)
         {
             matchingThread.Abort();
             matchingThread.Join();
             matchingThread = null;
         }
         if (thread != null && thread.ThreadState != ThreadState.Stopped)
         {
             thread.Abort();
             thread.Join();
             thread = null;
         }
         if (daemonThread != null && daemonThread.ThreadState != ThreadState.Stopped)
         {
             daemonThread.Join();
             daemonThread = null;
         }
     }
     CloseCamera();
     //while (daemonThread != null && daemonThread.ThreadState != ThreadState.Stopped) ;
     saveFaceData();
     try
     {
         FaceApi.DelModel();
     }
     catch { }
 }
Exemplo n.º 3
0
 private void screenbutton_Click(object sender, EventArgs e)
 {
     if (comboBox1.SelectedIndex == 0)
     {
         Hide();
     }
     FaceApi.ImageDllcallBack d = new FaceApi.ImageDllcallBack(GetScreen);
     FaceApi.CatchFrame(d, false);
 }
Exemplo n.º 4
0
        public void GetFeatureVec(object obj)
        {
            FeatureData feature = (FeatureData)obj;

            using (Bitmap bitmapSource = feature.localRead ? new Bitmap(feature.localFileName) : videoSourcePlayer.GetCurrentVideoFrame())
            {
                if (bitmapSource != null)
                {
                    //Bitmap bitmapSource = BitmapHelper.ScaleToSize(bitmapVideo, 256, 256);
                    float[]    vec        = new float[FaceApi.FeatureVecLen()];
                    int[]      landmark   = new int[FaceApi.LandMarkLen()];
                    BitmapData bitmapData = bitmapSource.LockBits(new Rectangle(0, 0, bitmapSource.Width, bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    while (isDetectrunning)
                    {
                        Console.WriteLine("waitting Detect");
                    }
                    ;
                    isDetectrunning = true;
                    bool success = FaceApi.FindFacePtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, out int x1, out int y1, out int width, out int height, landmark);
                    isDetectrunning = false;
                    bitmapSource.UnlockBits(bitmapData);
                    if (success)
                    {
                        bitmapData = bitmapSource.LockBits(new Rectangle(x1, y1, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                        for (int idx = 0; idx < landmark.Length / 2; ++idx)
                        {
                            landmark[idx * 2]     -= x1;
                            landmark[idx * 2 + 1] -= y1;
                        }
                        while (isFeaRunning)
                        {
                            Console.WriteLine("waitting FeaRunning");
                        }
                        ;
                        isFeaRunning = true;
                        success      = FaceApi.FeatureVecPtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, landmark, vec);
                        isFeaRunning = false;
                        bitmapSource.UnlockBits(bitmapData);
                    }
                    if (success)
                    {
                        float sum = 0;
                        for (int idx = 0; idx < vec.Length; ++idx)
                        {
                            sum += vec[idx];
                        }
                        SetIDBox(feature, sum + "", vec, bitmapSource.Clone(new Rectangle(x1, y1, width, height), PixelFormat.Format24bppRgb));
                    }
                    else
                    {
                        MessageBox.Show("添加数据失败!");
                    }
                }
            }
        }
Exemplo n.º 5
0
 private void button3_Click(object sender, EventArgs e)
 {
     try
     {
         label3.Text = FaceApi.FaceRecognition(label1.Text, label2.Text) + "";
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 6
0
        public void Matching(object obj)
        {
            MatchData matchData = (MatchData)obj;

            if (matchData.bitmapSource != null)
            {
                float[]    vec        = new float[FaceApi.FeatureVecLen()];
                BitmapData bitmapData = matchData.bitmapSource.LockBits(new Rectangle(0, 0, matchData.bitmapSource.Width, matchData.bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                while (isFeaRunning)
                {
                    Console.WriteLine("Matching is Waitting");
                }
                isFeaRunning = true;
                bool success = FaceApi.FeatureVecPtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, matchData.landmark, vec);
                isFeaRunning = false;
                matchData.bitmapSource.UnlockBits(bitmapData);
                double maxdata = 0;
                int    maxidx1 = 0;
                int    maxidx2 = 0;
                int    index   = 0;
                foreach (FaceData face in facedata)
                {
                    for (int idx = 0; idx < face.feavec.Count; ++idx)
                    {
                        double result = FaceApi.Distance(vec, face.feavec[idx]);
                        if (result > maxdata)
                        {
                            maxdata = result;
                            maxidx2 = idx;
                            maxidx1 = index;
                        }
                        Console.WriteLine(result + "");
                    }
                    index += 1;
                }
                if (maxdata > threshold)
                {
                    if (!closing)
                    {
                        string path = imageDir + "/" + facedata[maxidx1].name + "/" + (maxidx2 + 1) + ".jpg";
                        SetMatchBox(BitmapHelper.ReadImageFile(path), "匹配ID:" + facedata[maxidx1].name /*maxdata + ""*/);
                    }
                }
                else
                {
                    if (!closing)
                    {
                        SetMatchBox(null, "");
                    }
                }
            }
            matchData.Dispose();
        }
Exemplo n.º 7
0
 public Form1()
 {
     InitializeComponent();
     try
     {
         FaceApi.CreateLoadModels();
     }
     catch
     {
         MessageBox.Show("模型导入失败!");
     }
 }
Exemplo n.º 8
0
 public void CreateBitmap(Image img)
 {
     if (pictureBox1.InvokeRequired)//调用和创建该控件的线程是不同的线程,必须调用Invoke方法
     {
         if (close)
         {
             return;
         }
         //创建该方法的委托实例
         SetBitmapCallback d = new SetBitmapCallback(CreateBitmap);
         //调用该委托实例,并传递参数,参数为object类型,使用this调用Invoke(this为当前窗体,是创建该窗体控件的线程)
         Invoke(d, new object[] { img });//this指定创建该控件的线程来Invoke(调用)
     }
     else//调用与创建该控件的线程是同一个线程
     {
         if (img != null)
         {
             Bitmap bitmap = new Bitmap(img);
             if (process)
             {
                 BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                 int        success    = FaceApi.DrawFaces(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3);
                 bitmap.UnlockBits(bitmapData);
                 if (pictureBox1.BackgroundImage != null)
                 {
                     pictureBox1.BackgroundImage.Dispose();
                     pictureBox1.BackgroundImage = null;
                 }
                 pictureBox1.BackgroundImage = bitmap;
             }
             else
             {
                 if (pictureBox1.BackgroundImage != null)
                 {
                     pictureBox1.BackgroundImage.Dispose();
                     pictureBox1.BackgroundImage = null;
                 }
                 pictureBox1.BackgroundImage = bitmap;
             }
         }
         else
         {
             if (pictureBox1.BackgroundImage != null)
             {
                 pictureBox1.BackgroundImage.Dispose();
                 pictureBox1.BackgroundImage = null;
             }
         }
     }
 }
Exemplo n.º 9
0
 private void button2_Click(object sender, EventArgs e)
 {
     if (pictureBox1.BackgroundImage != null)
     {
         Bitmap     bitmap     = new Bitmap(image);
         BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
         int        success    = FaceApi.DrawFaces(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3);
         bitmap.UnlockBits(bitmapData);
         if (success == 0)
         {
             pictureBox1.BackgroundImage.Dispose();
             pictureBox1.BackgroundImage = bitmap;
         }
         else
         {
             bitmap.Dispose();
             MessageBox.Show("绘制人脸失败!");
         }
     }
     else
     {
         MessageBox.Show("请添加图片!");
     }
 }
Exemplo n.º 10
0
 public void play_video()
 {
     flag = true;
     SetTestBox("正在进行人脸检测");
     while (flag)
     {
         try
         {
             using (Bitmap bitmapSource = videoSourcePlayer.GetCurrentVideoFrame())
             {
                 if (bitmapSource != null)
                 {
                     if (!isDetectrunning)
                     {
                         BitmapData bitmapData = bitmapSource.LockBits(new Rectangle(0, 0, bitmapSource.Width, bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                         int[]      landmark   = new int[FaceApi.LandMarkLen()];
                         isDetectrunning = true;
                         bool success = FaceApi.FindFacePtr_(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, out int x1, out int y1, out int x2, out int y2, landmark);
                         isDetectrunning = false;
                         bitmapSource.UnlockBits(bitmapData);
                         if (success)
                         {
                             if (!closing)
                             {
                                 if (facedata.Count != 0)
                                 {
                                     if (matchingThread == null || matchingThread.ThreadState != ThreadState.Running)
                                     {
                                         for (int idx = 0; idx < landmark.Length / 2; ++idx)
                                         {
                                             landmark[idx * 2]     -= x1;
                                             landmark[idx * 2 + 1] -= y1;
                                         }
                                         MatchData matchData = new MatchData(bitmapSource.Clone(new Rectangle(x1, y1, x2 - x1, y2 - y1), PixelFormat.Format24bppRgb), landmark);
                                         matchingThread = new Thread(new ParameterizedThreadStart(Matching));
                                         matchingThread.Start(matchData);
                                     }
                                 }
                                 SetSearchBox(bitmapSource.Clone(new Rectangle(x1, y1, x2 - x1, y2 - y1), PixelFormat.Format24bppRgb));
                                 SetBox("0");
                             }
                         }
                         else
                         {
                             if (!closing)
                             {
                                 SetBox("-6");
                             }
                         }
                     }
                     else
                     {
                         if (!closing)
                         {
                             SetBox("-6");
                         }
                     }
                 }
                 else
                 {
                     break;
                 }
             }
         }
         catch (Exception ex)
         {
             if (!closing)
             {
                 SetBox("摄像头异常:" + ex.Message);
             }
         }
     }
     if (!closing)
     {
         SetBox("连接摄像头");
         SetTestBox("已停止人脸检测");
     }
 }
Exemplo n.º 11
0
 private void loadFaceData()
 {
     facedata = new List <FaceData>();
     if (!Directory.Exists(fileDir))
     {
         Directory.CreateDirectory(fileDir);
     }
     if (!Directory.Exists(imageDir))
     {
         Directory.CreateDirectory(imageDir);
     }
     if (File.Exists(filePath))
     {
         using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
         {
             String   line;
             FaceData face = new FaceData();
             while ((line = sr.ReadLine()) != null)
             {
                 String[] data = line.Split(' ');
                 if (data[0] == "ID")
                 {
                     face.name = data[1];
                 }
                 else if (data[0] == "end")
                 {
                     facedata.Add(face);
                     face = new FaceData();
                 }
                 else
                 {
                     float[] vec = new float[FaceApi.FeatureVecLen()];
                     for (int idx = 0; idx < vec.Length; ++idx)
                     {
                         vec[idx] = Convert.ToSingle(data[idx]);
                     }
                     face.feavec.Add(vec);
                 }
             }
         }
         if (facedata.Count != 0)
         {
             foreach (FaceData face in facedata)
             {
                 TreeNode[] nodes = new TreeNode[face.feavec.Count];
                 int        idx   = 0;
                 foreach (float[] fea in face.feavec)
                 {
                     float sum = 0;
                     foreach (float single in fea)
                     {
                         sum += single;
                     }
                     nodes[idx] = new TreeNode(sum + "");
                     idx       += 1;
                 }
                 TreeNode treeNode = new TreeNode(face.name, nodes);
                 treeView1.Nodes.Add(treeNode);
             }
         }
     }
 }