/// <summary> /// 识别图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.InitialDirectory = "D:\\"; dialog.Filter = "所有文件|*.*"; dialog.RestoreDirectory = true; dialog.FilterIndex = 1; if (dialog.ShowDialog() == DialogResult.OK) { string filename = dialog.FileName; try { var image = File.ReadAllBytes(filename); // 如果有可选参数 var options = new Dictionary <string, object> { { "max_face_num", 2 }, { "face_fields", "age,qualities,beauty" } }; var result = client.Detect(image, options); textBox1.Text = result.ToString(); FaceDetectInfo detect = JsonHelper.DeserializeObject <FaceDetectInfo>(result.ToString()); } catch (Exception ex) { } } }
/// <summary> /// 人脸检测 /// </summary> public void Detect(object image) { if (image != null && image is Bitmap) { try { var img = (Bitmap)image; var imgByte = Bitmap2Byte(img); if (imgByte != null) { // 如果有可选参数 var options = new Dictionary <string, object> { { "max_face_num", 2 }, { "face_fields", "age,qualities,beauty" } }; var result = client.Detect(imgByte, options); FaceDetectInfo detect = JsonHelper.DeserializeObject <FaceDetectInfo>(result.ToString()); if (detect != null && detect.result_num > 0) { ageText.Text = detect.result[0].age.TryToString(); this.location = detect.result[0].location; StringBuilder sb = new StringBuilder(); if (detect.result[0].qualities != null) { if (detect.result[0].qualities.blur >= 0.7) { sb.AppendLine("人脸过于模糊"); } if (detect.result[0].qualities.completeness >= 0.4) { sb.AppendLine("人脸不完整"); } if (detect.result[0].qualities.illumination <= 40) { sb.AppendLine("灯光光线质量不好"); } if (detect.result[0].qualities.occlusion != null) { if (detect.result[0].qualities.occlusion.left_cheek >= 0.8) { sb.AppendLine("左脸颊不清晰"); } if (detect.result[0].qualities.occlusion.left_eye >= 0.6) { sb.AppendLine("左眼不清晰"); } if (detect.result[0].qualities.occlusion.mouth >= 0.7) { sb.AppendLine("嘴巴不清晰"); } if (detect.result[0].qualities.occlusion.nose >= 0.7) { sb.AppendLine("鼻子不清晰"); } if (detect.result[0].qualities.occlusion.right_cheek >= 0.8) { sb.AppendLine("右脸颊不清晰"); } if (detect.result[0].qualities.occlusion.right_eye >= 0.6) { sb.AppendLine("右眼不清晰"); } if (detect.result[0].qualities.occlusion.chin >= 0.6) { sb.AppendLine("下巴不清晰"); } if (detect.result[0].pitch >= 20) { sb.AppendLine("俯视角度太大"); } if (detect.result[0].roll >= 20) { sb.AppendLine("脸部应该放正"); } if (detect.result[0].yaw >= 20) { sb.AppendLine("脸部应该放正点"); } } } if (detect.result[0].location.height <= 100 || detect.result[0].location.height <= 100) { sb.AppendLine("人脸部分过小"); } textBox4.Text = sb.ToString(); if (textBox4.Text.IsNull()) { textBox4.Text = "OK"; } } } } catch (Exception ex) { ClassLoger.Error("Form1.image", ex); } } }