1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| using OpenCvSharp; using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace OpenCvSharp_DNN_Demo { public partial class frmMain : Form { public frmMain() { InitializeComponent(); }
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png"; string image_path = "";
DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Now;
StringBuilder sb = new StringBuilder();
Mat image; Mat result_image;
FaceQualityAssessment fqa = new FaceQualityAssessment("model/face-quality-assessment.onnx"); FreeYoloFace face = new FreeYoloFace("model/yolo_free_huge_widerface_192x320.onnx");
private void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = fileFilter; if (ofd.ShowDialog() != DialogResult.OK) return;
pictureBox1.Image = null; pictureBox2.Image = null; textBox1.Text = "";
image_path = ofd.FileName; pictureBox1.Image = new Bitmap(image_path); image = new Mat(image_path); }
private void Form1_Load(object sender, EventArgs e) { image_path = "test_img/1.jpg"; pictureBox1.Image = new Bitmap(image_path); }
private unsafe void button2_Click(object sender, EventArgs e) { if (image_path == "") { return; } textBox1.Text = "检测中,请稍等……"; if (pictureBox2.Image != null) { pictureBox2.Image.Dispose(); } pictureBox2.Image = null; sb.Clear(); Application.DoEvents();
image = new Mat(image_path);
dt1 = DateTime.Now; List<Face> ltFace = face.Detect(image); dt2 = DateTime.Now;
if (ltFace.Count > 0) { sb.AppendLine("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms"); result_image = image.Clone(); foreach (var item in ltFace) { Mat crop_img = new Mat(image, item.rect); float fqa_prob_mean = fqa.Detect(crop_img); crop_img.Dispose(); Cv2.Rectangle(result_image, new OpenCvSharp.Point(item.rect.X, item.rect.Y), new OpenCvSharp.Point(item.rect.X + item.rect.Width, item.rect.Y + item.rect.Height), new Scalar(0, 0, 255), 2); string label = "prob:" + item.prob.ToString("0.00") + " fqa_score:" + fqa_prob_mean.ToString("0.00"); sb.AppendLine(label); Cv2.PutText(result_image, label, new OpenCvSharp.Point(item.rect.X, item.rect.Y - 5), HersheyFonts.HersheySimplex, 1, new Scalar(0, 0, 255), 2); } pictureBox2.Image = new Bitmap(result_image.ToMemoryStream()); textBox1.Text = sb.ToString(); } else { textBox1.Text = "未检测到人脸"; } }
private void pictureBox2_DoubleClick(object sender, EventArgs e) { Common.ShowNormalImg(pictureBox2.Image); }
private void pictureBox1_DoubleClick(object sender, EventArgs e) { Common.ShowNormalImg(pictureBox1.Image); } } }
|