首页 > 其他分享 >一次人脸识别ViewFaceCore使用的经验分享,看我把门店淘汰下来的POS机改成了人脸考勤机

一次人脸识别ViewFaceCore使用的经验分享,看我把门店淘汰下来的POS机改成了人脸考勤机

时间:2024-02-03 10:22:33浏览次数:20  
标签:人脸识别 dto string POS ViewFaceCore userInfo var new null

POS软件是什么?你好意思吗,还在用老掉牙的Winform。

 

 

 

门店被淘汰的POS机

销售终端——POS(point of sale)是一种多功能终端,把它安装在信用卡的特约商户和受理网点中与计算机联成网络,就能实现电子资金自动转账,它具有支持消费、预授权、余额查询和转账等功能,使用起来安全、快捷、可靠。

 

 

 

前言

 万事俱备只欠东风------一个USB摄像头和一个经过改造的人脸识别程序。

 

 下载地址:

GitHub - ViewFaceCore/ViewFaceCore: C# 超简单的离线人脸识别库。( 基于 SeetaFace6 )

开始干活,动手改造。

  1. 程序要支持无人值守,程序启动时自动打开摄像头。超过设定的时间无移动鼠标和敲击键盘,程序自动关闭摄像头,进入“休眠”
  2. 识别人脸成功后记录当前时间作为考勤记录
  3. 人脸信息放在服务器端,桌面程序和服务器端同步人脸信息
  4. 关于不排班实现考勤的思考
  5. 取消消息弹窗来和用户交互。使用能自动关闭的消息弹窗

1.检测超过设定的时间无移动鼠标和敲击键盘,判断是否无人使用。 

 #region 获取键盘和鼠标没有操作的时间 
 [StructLayout(LayoutKind.Sequential)]
 struct LASTINPUTINFO
 {
     [MarshalAs(UnmanagedType.U4)]
     public int cbSize;
     [MarshalAs(UnmanagedType.U4)]
     public uint dwTime;
 }
 [DllImport("user32.dll")]
 private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
 /// <summary>
 /// 获取键盘和鼠标没有操作的时间
 /// </summary>
 /// <returns></returns>
 private static long GetLastInputTime()
 {
     LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
     vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
     if (!GetLastInputInfo(ref vLastInputInfo))
         return 0;
     else
         return Environment.TickCount - (long)vLastInputInfo.dwTime;//单位ms  
 }
 #endregion

2.把人脸识别这个用途改成考勤 

        /// <summary>
        /// 窗体加载时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Load(object sender, EventArgs e)
        {
            #region 窗体初始化
            WindowState = FormWindowState.Maximized;
            // 隐藏摄像头画面控件
            VideoPlayer.Visible = false;
            //初始化VideoDevices
            检测摄像头ToolStripMenuItem_Click(null, null);
            //默认禁用拍照按钮
            FormHelper.SetControlStatus(this.ButtonSave, false);
            Text = "WPOS人脸识别&考勤";
            #endregion
            #region TTS
            try
            {
                VoiceUtilHelper = new SpVoiceUtil();
                StartVoiceTaskJob();
            }
            catch (Exception ex)
            {
                byte[] zipfile = (byte[])Properties.Resources.ResourceManager.GetObject("TTSrepair");
                System.IO.File.WriteAllBytes("TTSrepair.zip", zipfile);
                Program.UnZip("TTSrepair.zip", "", "", true);
                #region 语音引擎修复安装
                try
                {
                    MessageBox.Show("初始化语音引擎出错,错误描述:" + ex.Message + Environment.NewLine +
                                "正在运行语音引擎安装程序,请点下一步执行安装!", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    string physicalRoot = AppDomain.CurrentDomain.BaseDirectory;
                    string info1 = Program.Execute("TTSrepair.exe", 3);
                }
                finally
                {
                    System.IO.File.Delete("TTSrepair.zip");
                    Application.Restart();
                }
                #endregion
            }
            #endregion
            #region 自动打开摄像头
            Thread thread = new Thread(() =>
            {
                Thread.Sleep(5000);
                sc.Post(SystemInit, this);
            });
            thread.Start();
            #endregion
            #region Sync face data
            Thread SyncThread = new Thread(() =>
            {
                while (IsWorkEnd == false)
                {
                    var theEmployeeList = SyncServerEmployeeInfomation().Where(r => r.EmpFacialFeature != null).ToList();
                    if (theEmployeeList != null && theEmployeeList.Count > 0)
                    {
                        foreach (var emp in theEmployeeList)
                        {
                            poolExt.Post(emp);
                        }
                    }
                    Thread.Sleep(5000);
                }
            });
            SyncThread.Start();
            #endregion
            #region 自动关闭摄像头线程
            Thread CameraCheckThread = new Thread(() =>
            {
                while (IsWorkEnd == false)
                {
                    if (IsNeedAutoCheck)
                    {
                        long Auto_close_camera_interval = long.Parse(string.IsNullOrEmpty(config.AppSettings.Settings["Auto_close_camera_interval"].Value) ? "60000" : config.AppSettings.Settings["Auto_close_camera_interval"].Value);
                        long ts = GetLastInputTime();
                        if (ts > Auto_close_camera_interval)
                        {
                            IsNeedAutoCheck = false;
                            sc.Post(CheckCameraStatus, this);
                        }
                    }
                    Thread.Sleep(1000);
                }
            });
            CameraCheckThread.Start();
            btnSleep.Enabled = true;
            btnStopSleep.Enabled = true;
            #endregion
        }

 修改识别人脸后做的事情:

 /// <summary>
 /// 持续检测一次人脸,直到停止。
 /// </summary>
 /// <param name="token">取消标记</param>
 private async void StartDetector(CancellationToken token)
 {
     List<double> fpsList = new List<double>();
     double fps = 0;
     Stopwatch stopwatchFPS = new Stopwatch();
     Stopwatch stopwatch = new Stopwatch();
     isDetecting = true;
     try
     {
         if (VideoPlayer == null)
         {
             return;
         }
         while (VideoPlayer.IsRunning && !token.IsCancellationRequested)
         {
             try
             {
                 if (CheckBoxFPS.Checked)
                 {
                     stopwatch.Restart();
                     if (!stopwatchFPS.IsRunning)
                     { stopwatchFPS.Start(); }
                 }
                 Bitmap bitmap = VideoPlayer.GetCurrentVideoFrame(); // 获取摄像头画面 
                 if (bitmap == null)
                 {
                     await Task.Delay(10, token);
                     FormHelper.SetPictureBoxImage(FacePictureBox, bitmap);
                     continue;
                 }
                 if (!CheckBoxDetect.Checked)
                 {
                     await Task.Delay(1000 / 60, token);
                     FormHelper.SetPictureBoxImage(FacePictureBox, bitmap);
                     continue;
                 }
                 List<Models.FaceInfo> faceInfos = new List<Models.FaceInfo>();
                 using (FaceImage faceImage = bitmap.ToFaceImage())
                 {
                     var infos = await faceFactory.Get<FaceTracker>().TrackAsync(faceImage);
                     for (int i = 0; i < infos.Length; i++)
                     {
                         Models.FaceInfo faceInfo = new Models.FaceInfo
                         {
                             Pid = infos[i].Pid,
                             Location = infos[i].Location
                         };
                         if (CheckBoxFaceMask.Checked || CheckBoxFaceProperty.Checked)
                         {
                             Model.FaceInfo info = infos[i].ToFaceInfo();
                             if (CheckBoxFaceMask.Checked)
                             {
                                 var maskStatus = await faceFactory.Get<MaskDetector>().PlotMaskAsync(faceImage, info);
                                 faceInfo.HasMask = maskStatus.Masked;
                             }
                             if (CheckBoxFaceProperty.Checked)
                             {
                                 FaceRecognizer faceRecognizer = null;
                                 if (faceInfo.HasMask)
                                 {
                                     faceRecognizer = faceFactory.GetFaceRecognizerWithMask();
                                 }
                                 else
                                 {
                                     faceRecognizer = faceFactory.Get<FaceRecognizer>();
                                 }
                                 var points = await faceFactory.Get<FaceLandmarker>().MarkAsync(faceImage, info);
                                 float[] extractData = await faceRecognizer.ExtractAsync(faceImage, points);
                                 UserInfo userInfo = CacheManager.Instance.Get(faceRecognizer, extractData);
                                 if (userInfo != null)
                                 {
                                     faceInfo.Name = userInfo.Name;
                                     faceInfo.Age = userInfo.Age;
                                     switch (userInfo.Gender)
                                     {
                                         case GenderEnum.Male:
                                             faceInfo.Gender = Gender.Male;
                                             break;
                                         case GenderEnum.Female:
                                             faceInfo.Gender = Gender.Female;
                                             break;
                                         case GenderEnum.Unknown:
                                             faceInfo.Gender = Gender.Unknown;
                                             break;
                                     }
                                     pool.Post(userInfo);
                                 }
                                 else
                                 {
                                     faceInfo.Age = await faceFactory.Get<AgePredictor>().PredictAgeAsync(faceImage, points);
                                     faceInfo.Gender = await faceFactory.Get<GenderPredictor>().PredictGenderAsync(faceImage, points);
                                 }
                             }
                         }
                         faceInfos.Add(faceInfo);
                     }
                 }
                 using (Graphics g = Graphics.FromImage(bitmap))
                 {
                     #region 绘制当前时间
                     StringFormat format = new StringFormat();
                     format.Alignment = StringAlignment.Center;
                     format.LineAlignment = StringAlignment.Center;
                     g.DrawString($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}", new Font("微软雅黑", 32), Brushes.Green, new Rectangle(0, 0, Width - 32, 188), format);
                     #endregion
                     // 如果有人脸,在 bitmap 上绘制出人脸的位置信息
                     if (faceInfos.Any())
                     {
                         g.DrawRectangles(new Pen(Color.Red, 4), faceInfos.Select(p => p.Rectangle).ToArray());
                         if (CheckBoxDetect.Checked)
                         {
                             for (int i = 0; i < faceInfos.Count; i++)
                             {
                                 StringBuilder builder = new StringBuilder();
                                 if (CheckBoxFaceProperty.Checked)
                                 {
                                     if (!string.IsNullOrEmpty(faceInfos[i].Name))
                                     {
                                         builder.Append(faceInfos[i].Name);
                                     }
                                 }
                                 if (builder.Length > 0)
                                     g.DrawString(builder.ToString(), new Font("微软雅黑", 32), Brushes.Green, new PointF(faceInfos[i].Location.X + faceInfos[i].Location.Width + 24, faceInfos[i].Location.Y));
                             }
                         }
                     }
                     if (CheckBoxFPS.Checked)
                     {
                         stopwatch.Stop();
                         if (numericUpDownFPSTime.Value > 0)
                         {
                             fpsList.Add(1000f / stopwatch.ElapsedMilliseconds);
                             if (stopwatchFPS.ElapsedMilliseconds >= numericUpDownFPSTime.Value)
                             {
                                 fps = fpsList.Average();
                                 fpsList.Clear();
                                 stopwatchFPS.Reset();
                             }
                         }
                         else
                         {
                             fps = 1000f / stopwatch.ElapsedMilliseconds;
                         }
                         g.DrawString($"{fps:#.#} FPS", new Font("微软雅黑", 24), Brushes.Green, new Point(10, 10));
                     }
                 }
                 FormHelper.SetPictureBoxImage(FacePictureBox, bitmap);
             }
             catch (TaskCanceledException)
             {
                 break;
             }
             catch { }
         }
     }
     finally
     {
         isDetecting = false;
     }
 }
 #endregion

 3.把人脸信息放在服务器端,桌面程序和服务器端同步人脸信息 

 

 /// <summary>
 /// 同步人员信息
 /// </summary>
 private List<PlatEmployeeDto> SyncServerEmployeeInfomation()
 {
     List<PlatEmployeeDto> list = new List<PlatEmployeeDto>();
     string url = $"{config.AppSettings.Settings["Platform"].Value}/business/employeemgr/POSSyncEmployeeInfomation";
     try
     {
         string rs = Program.HttpGetRequest(url);
         if (!string.IsNullOrEmpty(rs) && JObject.Parse(rs).Value<int>("code").Equals(200))
         {
             JObject jo = JObject.Parse(rs);
             list = JsonConvert.DeserializeObject<List<PlatEmployeeDto>>(jo["data"].ToString());
         }
     }
     catch (Exception ex)
     {
         if (ex.Message.Contains("无法连接到远程服务器"))
         {
             Thread.Sleep(100);
             ViewFaceCore.Controls.MessageTip.ShowError("无法连接到远程服务器" + Environment.NewLine + "Unable to connect to remote server", 300);
         }
     }
     return list;
 }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                SetUIStatus(false);
                UserInfo userInfo = BuildUserInfo();
                if (userInfo == null)
                {
                    throw new Exception("获取用户基本信息失败!");
                }
                using (DefaultDbContext db = new DefaultDbContext())
                {
                    db.UserInfo.Add(userInfo);
                    if (db.SaveChanges() > 0)
                    {
                        CacheManager.Instance.Refesh();
                        this.Close();
                        _ = Task.Run(() =>
                        {
                            //确保关闭后弹窗
                            Thread.Sleep(100);
                            try
                            {
                                #region Post Data
                                string url = $"{config.AppSettings.Settings["Platform"].Value}/business/employeemgr/PosNewEmployeeRegister";
                                PlatEmployeeDto dto = new PlatEmployeeDto();
                                dto.KeyId = Guid.NewGuid().ToString();
                                dto.EmpNo = userInfo.EmpNo;
                                dto.EmpName = userInfo.Name;
                                dto.EmpSex = (int)userInfo.Gender.ToInt64();
                                dto.Mobile = userInfo.Phone;
                                dto.PositionValue = userInfo.JobPosition.ToString();
                                dto.EmpFacialFeature = _globalUserInfo.Extract;
                                dto.EmpMainPhoto = _globalUserInfo.Image;
                                dto.CreateBy = "Client";
                                dto.CreateTime = DateTime.Now;
                                dto.IsAdmin = "N";
                                dto.Status = 0;
                                dto.FirstPositionLabel = cbxposition.Text;
                                string jsondata = JsonConvert.SerializeObject(dto);
                                string st = Program.PostJsonData(url, jsondata);
                                #endregion 
                                if (!string.IsNullOrEmpty(st) && st.Contains("200"))
                                {
                                    //MessageBox.Show("保存用户信息成功!同步到服务器成功,可到其他门店考勤。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    DialogResult = DialogResult.OK;
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("本地保存用户信息成功!但同步到服务器出错,不能立即到其他门店考勤。" + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                SetUIStatus(false);
            }
        }

4.关于不排班实现考勤的思考 

 

        /// <summary>
        /// 客户端添加attendance考勤明细
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddAttendanceDetails")]
        //[ActionPermissionFilter(Permission = "business:erpattendancedetails:add")]
        [Log(Title = "attendance考勤明细", BusinessType = BusinessType.INSERT)]
        [AllowAnonymous]
        public IActionResult AddAttendanceDetails([FromBody] AttendanceDetailsDto parm)
        {
            var modal = parm.Adapt<AttendanceDetails>().ToCreate(HttpContext);
            if (!string.IsNullOrEmpty(parm.FkStore))
            {
                int storeId = -1;
                int.TryParse(parm.FkStore, out storeId);
                var store = _MerchantStoreService.GetFirst(s => s.Id == storeId);
                if (store == null)
                    return BadRequest();
                modal.FkStore = store.KeyId;
            }
            else
                return BadRequest();
            if (!_AttendanceDetailsService.Any(r => r.AuditDate == parm.AuditDate && r.EmpNo == parm.EmpNo))
            {
                modal.Remark = "上班&clock in";
                var response = _AttendanceDetailsService.AddAttendanceDetails(modal);
                return SUCCESS(response);
            }
            else
            {
                var list = _AttendanceDetailsService.GetList(r => r.AuditDate == parm.AuditDate && r.EmpNo == parm.EmpNo);
                var time1 = list.Max(r => r.AttendanceDatetime);
                if (time1 != null)
                {
                    var ts = DateTime.Now - DateTime.Parse(time1);
                    if (ts.TotalMinutes < 61)
                    {
                        return Ok();
                    }
                    else
                    {
                        modal.Remark = "下班&clock out";
                        var response = _AttendanceDetailsService.AddAttendanceDetails(modal);
                        return SUCCESS(response);
                    }
                } 
                else 
                { 
                    return BadRequest(); 
                }                
            }
        }

5.取消消息弹窗来和用户交互。使用能自动关闭的消息弹窗

 

 

 

这个需要感谢以前在园子里的一位博主的分享他写的控件名字叫"LayeredWindow",对外暴露的类叫“MessageTip”,不好意思已忘记作者。

  

 如果你仔细阅读代码还会发现集成了TTS。反正做得有点像无人值守的一些商业机器。好了,收工了。今天只上半天班。 

 

 

标签:人脸识别,dto,string,POS,ViewFaceCore,userInfo,var,new,null
From: https://www.cnblogs.com/datacool/p/18004303/ViewFaceCore2024

相关文章

  • 【数据库】为什么PostgreSQL第一次查询结果很慢,第二次就变快了
    引言在PostgreSQL数据库中,我们经常会遇到这样的情况:第一次执行某个查询语句时,结果返回的速度非常慢,而当我们再次执行相同的查询语句时,速度却变得非常快。这种现象可能会让我们感到困惑,为什么同样的查询语句,第一次执行会这么慢,而第二次执行却这么快呢?PostgreSQL的查询缓存机制Po......
  • 无涯教程-String.prototype.includes(searchString, position = 0)函数
    该方法确定字符串是否是给定字符串的子字符串。String.prototype.includes-语法str.includes(searchString[,position])searchString-要搜索的子字符串。position    -该字符串中开始搜索searchString的位置;默认为0。String.prototype.includes-返回值t......
  • 无涯教程-String.prototype.startsWith(searchString, position = 0)函数
    该方法确定字符串是否以指定的字符开头。String.prototype.startsWith-语法str.startsWith(searchString[,position])searchString  - 在此字符串开头要搜索的字符。position     - 该字符串中开始搜索searchString的位置;默认为0。String.prototype.s......
  • Linux基础47 Ansible之ad-hoc, 命令模块(command, shell, script), 软件管理模块(yum,
    Ansible之ad-hoc一、什么是ad-hoc1.什么是ad-hocad-hoc简而言之就是“临时命令”,执行完即结束,并不会保存2.ad-hoc使用场景可以用作查看远程机器的进程,或者磁盘,或者拷贝文件3.ad-hoc命令使用[root@m01~]#ansibleweb01-mshell-a'free-m'web01|CHANGED|rc=......
  • ‘utf-8’ codec can’t decode byte 0xe5 in position 1023: unexpected end of data
    使用pycharm在本地调试项目的时候,发现偶尔会出现下面的错误,导致项目无法继续执行下去。但是不适用debug模式,而使用运行模式的时候不会有这样的问题。E:\pycharm_pro\PyCharm2019.2.3\helpers\pydev\_pydevd_bundle\pydevd_comm.pyr=r.decode('utf-8')UnicodeDecodeError:......
  • Linux进程通信-POSIX IPC
    前言LinuxPOSIXIPC的可移植性是不如SystemVIPC的,但是我们只用Linux,并且内核版本高于2.6.6的话就不存在该问题了。也因为POSIXIPC出现的比较晚,借鉴了systemVIPC的长处,规避其短处,使得POSIXIPC的接口更易用。进程间通信的手段很多,除了消息队列、信号量、共享内存,还有信号、so......
  • PostgreSQL从小白到高手教程 - 第44讲:pg流复制部署
       PostgreSQL从小白到专家,是从入门逐渐能力提升的一个系列教程,内容包括对PG基础的认知、包括安装使用、包括角色权限、包括维护管理、、等内容,希望对热爱PG、学习PG的同学们有帮助,欢迎持续关注CUUGPG技术大讲堂。 第44讲:流复制部署 PostgreSQL第44讲:2月3日(周六)19......
  • Flex布局,和position布局,实现上下固定中间部分滚动
    Flex布局实现上下固定中间部分滚动:<divclass="parent"><divclass="header">header--固定</div><divclass="content"><p>content--滚动</p><p>内容部分</p><p>内容部分<......
  • Docker 导出容器到 docker-compose.yml 文件
    Docker导出容器到docker-compose.yml 文件问题:docker-compose.yml文件丢失解决办法:docker的事用docker解决背景:很多容器都是dockerrun运行的,想要转换为docker-compose.yml1.测试备份:dockerrun--rm-v/var/run/docker.sock:/var/run/docker.sockdockerproxy.com/re......
  • Python requests.get所有参数顺序、Python requests.post所有参数顺序
    request.get所有参数顺序:url(必选)、params、allow_redirects、auth、cert、cookies、headers、proxies、stream、timeout、verify -------------------------------------------------------------------------------------------------------------------------------------......