Exemplo n.º 1
0
        /********************************************最小至托盘 end*******************************************/

        private void button1_Click(object sender, EventArgs e)
        {
            string     id         = "220bd11e-86bf-4c80-8535-8a9c0818c760";
            MysqlUtils mysqlUtils = new MysqlUtils();
            //var result = mysqlUtils.PriciseSelectById(id);
            var result = mysqlUtils.SelectUserFaceByFeature();

            Console.WriteLine(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 图像显示到窗体上,得到每一帧图像,并进行处理(画框)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoSource_Paint(object sender, PaintEventArgs e)
        {
            if (videoSource.IsRunning)// 摄像头运行中
            {
                //得到当前摄像头下的图片
                Bitmap bitmap = videoSource.GetCurrentVideoFrame();
                if (bitmap == null)
                {
                    return;
                }
                Graphics g       = e.Graphics;
                float    offsetX = videoSource.Width * 1f / bitmap.Width;
                float    offsetY = videoSource.Height * 1f / bitmap.Height;
                //检测人脸,得到Rect框(方框)
                ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pVideoEngine, bitmap);
                //得到最大人脸
                ASF_SingleFaceInfo maxFace = FaceUtil.GetMaxFace(multiFaceInfo);
                //得到Rect
                MRECT rect   = maxFace.faceRect;
                float x      = rect.left * offsetX;
                float width  = rect.right * offsetX - x;
                float y      = rect.top * offsetY;
                float height = rect.bottom * offsetY - y;
                //根据Rect进行画框
                g.DrawRectangle(pen, x, y, width, height);

                // 调整摄像头视频框
                if (rect.left.ToString() == "0")
                {
                    this.videoSource.Location = new System.Drawing.Point(this.Width - 1, this.Height - 1);
                }
                else
                {
                    int xVideoSource = (int)(0.5 * (this.Width - this.videoSource.Width));
                    int yVideoSource = (int)(0.5 * (this.Height - this.videoSource.Height));
                    this.videoSource.Location = new System.Drawing.Point(xVideoSource, yVideoSource);
                }
                // this.logBox.AppendText(rect.left.ToString() + "\n");

                // this.videoSource.Show();// 能画框的时候显示摄像头视频框

                if (trackUnit.message != "" && x > 0 && y > 0)
                {
                    //将上一帧检测结果显示到页面上
                    g.DrawString(trackUnit.message, font, brush, x, y + 5);
                }
                //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况
                if (isLock == false)
                {
                    isLock = true;
                    //异步处理提取特征值和比对,不然页面会比较卡
                    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                    {
                        if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0)
                        {
                            try
                            {
                                //提取人脸特征 TODO 这是当前摄像头下的人脸的特征值
                                IntPtr videoFaceFeature = FaceUtil.ExtractFeature(pVideoImageEngine, bitmap, maxFace);
                                float similarity        = 0f;
                                //得到比对结果 star TODO 这边compareFeature中
                                // TODO 数据库中查询出feature

                                MysqlUtils mysqlUtils           = new MysqlUtils();
                                List <byte[]> featureListFromDB = mysqlUtils.SelectUserFaceByFeature();// 数据库查询
                                var v = 0;
                                for (int i = 0; i < featureListFromDB.Count; i++)
                                {
                                    //pFeatureItemFromDB = TabConvert.BytesToIntptr(featureListFromDB[i]);
                                    //GCHandle hObject = GCHandle.Alloc(featureListFromDB[i], GCHandleType.Pinned);
                                    //pFeatureItemFromDB = hObject.AddrOfPinnedObject();


                                    ASF_FaceFeature localFeature = new ASF_FaceFeature();
                                    localFeature.feature         = MemoryUtil.Malloc(featureListFromDB[i].Length);               // 申请本地人脸特征指针
                                    MemoryUtil.Copy(featureListFromDB[i], 0, localFeature.feature, featureListFromDB[i].Length); // source, startIndex, destination, length
                                    localFeature.featureSize = featureListFromDB[i].Length;                                      // 设置本地特征值长度
                                    IntPtr pLocalFeature     = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_FaceFeature>());         // 申请本地特征值指针空间
                                    MemoryUtil.StructureToPtr(localFeature, pLocalFeature);                                      // T t,IntPtr ptr

                                    /*
                                     * IntPtr pFeatureItemFromDB = MemoryUtil.Malloc(MemoryUtil.SizeOf<ASF_FaceFeature>());
                                     * v++;
                                     * Marshal.StructureToPtr(featureListFromDB[i], pFeatureItemFromDB, false);
                                     * Marshal.FreeHGlobal(pFeatureItemFromDB);
                                     */

                                    imagesFeatureList.Add(pLocalFeature);
                                }


                                int result = compareFeatureFromDB(videoFaceFeature, out similarity);
                                if (result > -1)
                                {
                                    // TODO
                                    //将比对结果放到显示消息中,用于最新显示
                                    trackUnit.message       = string.Format("你好,匹配到 {0}号,相似度是 {1}", result, similarity);
                                    labelLoginUserName.Text = result.ToString();
                                    Console.WriteLine(result.ToString() + ":" + similarity.ToString() + "\n");
                                    // TODO
                                    // 相似度不足0.8,窗口正常显示
                                    if (similarity < 0.8)
                                    {
                                        this.Visible     = true;
                                        this.WindowState = FormWindowState.Normal;
                                    }
                                    else// 相似度超过80,自动缩小至系统托盘
                                    {
                                        this.Hide();
                                        this.notifyIcon.Visible = true;
                                    }
                                    // TODO 隔段时间打开托盘
                                    Thread.Sleep(5000);// TODO 设置多长时间再次验证人脸,这边设置了2秒
                                    this.Visible     = true;
                                    this.WindowState = FormWindowState.Normal;
                                }
                                else
                                {
                                    //重置显示消息
                                    trackUnit.message = "";
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            finally
                            {
                                isLock = false;
                            }
                        }
                        isLock = false;
                    }));
                }
            }
        }