public JObject face_detect_using_aip(string userid, Bitmap bmap) { //client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY); //client.Timeout = 6000; string image = bitmap2BASE64(bmap); var imageType = "BASE64"; var options = new Dictionary <string, object> { { "max_face_num", 1 }, { "face_field", "age,beauty,expression,faceshape,gender,glasses,race,qualities" } }; renew(); return(client.Detect(image, imageType, options)); }
/// <summary> /// 人脸检测,进行红框绘制 /// </summary> /// <returns></returns> private void FaceCheck(object objImagePath) { try { string strImagePath = (string)objImagePath; var client = new Baidu.Aip.Face.Face(appKey, sKey); var image = Convert.ToBase64String(File.ReadAllBytes(strImagePath)); var imageType = "BASE64"; var options = new Dictionary <string, object> { { "max_face_num", 5 } }; // 带参数调用人脸检测 var result = client.Detect(image, imageType, options); Console.WriteLine(result); //如果有人脸,则将图片加载到识别结果中 if (int.Parse(result["result"]["face_num"].ToString()) > 0) { this.BeginInvoke(new Action(() => { //清空之前的查询结果 this.listView1.Items.Clear(); //直接从内存中拿数据,不用再读写磁盘数据,即将base64转为stream byte[] imageBytes = Convert.FromBase64String(image); using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) { ms.Write(imageBytes, 0, imageBytes.Length); this.pictureBox2.Image = Image.FromStream(ms, true); //根据返回的人脸数据进行红框的绘制 Image img = pictureBox2.Image; var faceArr = JsonConvert.DeserializeObject <List <FaceCheckResult> >(result["result"]["face_list"].ToString()); string strAllUsers = ""; foreach (var item in faceArr) { Graphics g = Graphics.FromImage(img); Pen pen = new Pen(Color.Red, 3); pen.DashStyle = DashStyle.Solid; g.RotateTransform(item.location.rotation); g.TranslateTransform((float)item.location.left, (float)item.location.top, MatrixOrder.Append); g.DrawRectangle(pen, new Rectangle(0, 0, (int)item.location.width, (int)item.location.height)); g.Dispose(); strAllUsers += FaceSearch(item); } if (!string.IsNullOrEmpty(strAllUsers)) { Thread tVoice = new Thread(VoiceBroadcast); tVoice.Start("你好," + strAllUsers); } }; })); } } catch (Exception exp) { return; } }