/// <summary> /// 人脸检测 /// </summary> private void FaceDetect() { Dictionary <string, object> options = new Dictionary <string, object>(); //options["face_field"] = "age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype"; // var imageBase64 = Common.BitmapToBase64(bitmap); var jObject = face.Detect(imageBase64, "BASE64", options); var error_code = int.Parse(jObject["error_code"].ToString()); if (error_code > 0) { faceSearch = null; } else { var result = jObject["result"]; faceDetect = JsonConvert.DeserializeObject <FaceDetectResultModel>(result.ToString()); } }
/// <summary> /// 摄像头初始化 /// </summary> private void VideoLoaded() { try { this.imageHeader.Source = Common.BitmapToBitmapSource(Properties.Resources.noimage); this.imageAni.Source = Common.BitmapToBitmapSource(Properties.Resources.face); this.userID.Text = ""; this.userName.Text = ""; this.workTime.Text = ""; this.machineID.Text = ""; _VideoSource = Video.GetVideoSource(); //获取摄像头参数 if (null == _VideoSource) { System.Windows.MessageBox.Show("没有检测到摄像头"); this.Close(); return; } this.VideoPlayer.VideoSource = _VideoSource; this.VideoPlayer.Start(); //线程控制人脸采集 Task.Factory.StartNew(() => { Task.Delay(500).Wait(); while (!cancellationTokenSource.IsCancellationRequested) { try { if (_VideoSource == null || !_VideoSource.IsRunning) { //usb接口异常,重新连接摄像头 this.Dispatcher.BeginInvoke(new Action(() => { _VideoSource = Video.GetVideoSource(); if (_VideoSource != null) { _VideoSource.Stop(); _VideoSource.WaitForStop(); this.VideoPlayer.Stop(); this.VideoPlayer.VideoSource = _VideoSource; this.VideoPlayer.Start(); } }), null); } if (Globals.IsOnline) { bitmap = this.VideoPlayer.GetCurrentVideoFrame(); if (bitmap == null) { continue; } lock (lockObj) { imageBase64 = Common.BitmapToBase64(bitmap); i = 200; var result = FaceSearch(); if (result) { FaceDetect(); this.Dispatcher.BeginInvoke(new Action(PictureControl), null); cancellationTokenSource.Cancel(); } else { faceDetect = null; if (!FaceCollect()) { bitmap.Dispose(); imageBase64 = null; } } } } else { Thread.Sleep(200); if (!ArcFace.Api.Init(out string msg, AppId, DKey, RKey, 50, 500, FaceDataPath)) { System.Windows.MessageBox.Show(msg); this.Close(); return; } var img = this.VideoPlayer.GetCurrentVideoFrame(); if (img == null) { continue; } bool result = ArcFace.Api.FaceMatch(img); this.Dispatcher.BeginInvoke(new Action <bool>(PictureCapture), new object[] { result }); if (_RegisterIndex != -1 && ArcFace.Api.CacheFaceResults.Items.Count > 0) { if (result && ArcFace.Api.CacheFaceResults[_RegisterIndex].Score != 0) { img.Dispose(); cancellationTokenSource.Cancel(); } if (string.IsNullOrEmpty(ArcFace.Api.CacheFaceResults[_RegisterIndex].ID) && ArcFace.Api.CacheFaceResults[_RegisterIndex].Score == 0 && ArcFace.Api.CacheFaceResults[_RegisterIndex].Rectangle != null) { try { if (bitmap == null) { bitmap = img.Clone(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), img.PixelFormat); img.Dispose(); this.Dispatcher.BeginInvoke(new Action(FaceRegister), null); //cancellationTokenSource.Cancel(); } } catch (Exception ex) { //throw ex; } } else { img.Dispose(); } } else { img.Dispose(); } } //检测到用户,使用UserGet接口 //未检测到用户,使用FaceDetect接口,并注册用户 Thread.Sleep(100); } catch (Exception ex) { //throw; } } }, cancellationTokenSource.Token); } catch (Exception) { throw; } }