当前位置:   article > 正文

C# 人脸检测 人脸比对 活体检测 口罩检测 年龄预测 性别预测 眼睛状态检测_c# facedetector

c# facedetector

目录

介绍

效果

代码 

下载 


介绍

基于以下开源软件做了一个Demo 

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

效果

代码 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ViewFaceCore.Core;
using ViewFaceCore.Model;
using System.Drawing;
using System.Diagnostics;

namespace ViewFaceCoreDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

        private void btnSelect_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.ImageLocation = ofd.FileName;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox2.ImageLocation = ofd.FileName;
        }

        /// <summary>
        /// 人脸检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            FaceDetector faceDetector = new FaceDetector();
            Bitmap bitmap = new Bitmap(pictureBox1.Image);
            FaceImage faceImage = bitmap.ToFaceImage();
            FaceInfo[] infos = faceDetector.Detect(faceImage);
            if (infos.Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            //Console.WriteLine("识别到的人脸数量:" + infos.Length + " 个人脸信息:\n");
            //Console.WriteLine("No.\t人脸置信度\t位置信息");
            //for (int i = 0; i < infos.Length; i++)
            //{
            //    Console.WriteLine(String.Format("{0}\t{1}\t{2}", i, infos[i].Score, infos[i].Location));
            //}
            //画方框,标记人脸
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.DrawRectangles(new Pen(Color.Red, 2), infos.Select(p => new RectangleF(p.Location.X, p.Location.Y, p.Location.Width, p.Location.Height)).ToArray());
            }
            pictureBox1.Image = bitmap;
        }

        /// <summary>
        /// 活体检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
            FaceDetector faceDetector = new FaceDetector();
            FaceLandmarker faceMark = new FaceLandmarker();
            FaceAntiSpoofing faceAntiSpoofing = new FaceAntiSpoofing();
            if (faceDetector.Detect(bitmap).Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            var info = faceDetector.Detect(bitmap).First();
            var markPoints = faceMark.Mark(bitmap, info);
            Stopwatch sw = Stopwatch.StartNew();
            sw.Start();
            var result = faceAntiSpoofing.AntiSpoofing(bitmap, info, markPoints);
            msg = "活体检测,结果:" + result.Status + ",清晰度:" + result.Clarity + ",真实度:" + result.Reality + ",耗时:" + sw.ElapsedMilliseconds + "ms";
            richTextBox1.Text += msg + "\r\n";
            Console.WriteLine(msg);
            sw.Stop();
        }

        /// <summary>
        /// 人脸比对
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            String msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            if (pictureBox2.Image == null)
            {
                msg = "图片2不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            Stopwatch sw = Stopwatch.StartNew();
            sw.Start();
            var faceImage0 = new Bitmap(pictureBox1.Image).ToFaceImage();
            var faceImage1 = new Bitmap(pictureBox2.Image).ToFaceImage();
            //检测人脸信息
            FaceDetector faceDetector = new FaceDetector();
            FaceInfo[] infos0 = faceDetector.Detect(faceImage0);
            FaceInfo[] infos1 = faceDetector.Detect(faceImage1);
            if (infos0.Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                sw.Stop();
                return;
            }
            if (infos1.Length == 0)
            {
                msg = "图片2未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                sw.Stop();
                return;
            }
            //标记人脸位置
            FaceLandmarker faceMark = new FaceLandmarker();
            FaceMarkPoint[] points0 = faceMark.Mark(faceImage0, infos0[0]);
            FaceMarkPoint[] points1 = faceMark.Mark(faceImage1, infos1[0]);
            //提取特征值
            FaceRecognizer faceRecognizer = new FaceRecognizer();
            float[] data0 = faceRecognizer.Extract(faceImage0, points0);
            float[] data1 = faceRecognizer.Extract(faceImage1, points1);
            //对比特征值
            bool isSelf = faceRecognizer.IsSelf(data0, data1);
            //计算相似度
            float similarity = faceRecognizer.Compare(data0, data1);
            msg = "识别到的人脸是否为同一人:" + isSelf + ",相似度:" + similarity + ",对比耗时:" + sw.ElapsedMilliseconds + "ms";
            richTextBox1.Text += msg + "\r\n";
            Console.WriteLine(msg);
            sw.Stop();
        }

        /// <summary>
        /// 口罩检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
            FaceDetector faceDetector = new FaceDetector();
            MaskDetector maskDetector = new MaskDetector();
            if (faceDetector.Detect(bitmap).Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            var info = faceDetector.Detect(bitmap).First();
            PlotMaskResult plotMaskResult = maskDetector.PlotMask(bitmap, info);
            if (plotMaskResult.Masked)
            {
                richTextBox1.Text += "口罩:是 \r\n";
            }
            else
            {
                richTextBox1.Text += "口罩:否 \r\n";
            }
            Console.WriteLine(msg);
        }

        /// <summary>
        /// 年龄预测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
            FaceDetector faceDetector = new FaceDetector();
            if (faceDetector.Detect(bitmap).Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            var info = faceDetector.Detect(bitmap).First();
            AgePredictor agePredictor = new AgePredictor();
            FaceLandmarker faceMark = new FaceLandmarker();
            var markPoints = faceMark.Mark(bitmap, info);
            int age = agePredictor.PredictAge(bitmap, markPoints);
            richTextBox1.Text += "年龄:" + age + " \r\n";
            Console.WriteLine(msg);
        }

        /// <summary>
        /// 性别预测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
            FaceDetector faceDetector = new FaceDetector();
            if (faceDetector.Detect(bitmap).Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            var info = faceDetector.Detect(bitmap).First();
            GenderPredictor genderPredictor = new GenderPredictor();
            FaceLandmarker faceMark = new FaceLandmarker();
            var markPoints = faceMark.Mark(bitmap, info);
            Gender gender = genderPredictor.PredictGender(bitmap, markPoints);
            richTextBox1.Text += "性别:" + gender + " \r\n";
            Console.WriteLine(msg);
        }

        /// <summary>
        /// 眼睛状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            string msg = "";
            if (pictureBox1.Image == null)
            {
                msg = "图片1不能为空";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
            FaceDetector faceDetector = new FaceDetector();
            if (faceDetector.Detect(bitmap).Length == 0)
            {
                msg = "图片1未检测到人脸";
                richTextBox1.Text += msg + "\r\n";
                return;
            }
            var info = faceDetector.Detect(bitmap).First();
            EyeStateDetector eyeStateDetector = new EyeStateDetector();
            FaceLandmarker faceMark = new FaceLandmarker();
            var markPoints = faceMark.Mark(bitmap, info);
            EyeStateResult eyeStateResult = eyeStateDetector.Detect(bitmap, markPoints);
            richTextBox1.Text += "眼睛状态:" + eyeStateResult + " \r\n";
            Console.WriteLine(msg);
        }
    }
}
 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ViewFaceCore.Core;
  9. using ViewFaceCore.Model;
  10. using System.Drawing;
  11. using System.Diagnostics;
  12. namespace ViewFaceCoreDemo
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  21. private void btnSelect_Click(object sender, EventArgs e)
  22. {
  23. OpenFileDialog ofd = new OpenFileDialog();
  24. ofd.Filter = fileFilter;
  25. if (ofd.ShowDialog() != DialogResult.OK) return;
  26. pictureBox1.ImageLocation = ofd.FileName;
  27. }
  28. private void button5_Click(object sender, EventArgs e)
  29. {
  30. OpenFileDialog ofd = new OpenFileDialog();
  31. ofd.Filter = fileFilter;
  32. if (ofd.ShowDialog() != DialogResult.OK) return;
  33. pictureBox2.ImageLocation = ofd.FileName;
  34. }
  35. /// <summary>
  36. /// 人脸检测
  37. /// </summary>
  38. /// <param name="sender"></param>
  39. /// <param name="e"></param>
  40. private void button2_Click(object sender, EventArgs e)
  41. {
  42. string msg = "";
  43. if (pictureBox1.Image == null)
  44. {
  45. msg = "图片1不能为空";
  46. richTextBox1.Text += msg + "\r\n";
  47. return;
  48. }
  49. FaceDetector faceDetector = new FaceDetector();
  50. Bitmap bitmap = new Bitmap(pictureBox1.Image);
  51. FaceImage faceImage = bitmap.ToFaceImage();
  52. FaceInfo[] infos = faceDetector.Detect(faceImage);
  53. if (infos.Length == 0)
  54. {
  55. msg = "图片1未检测到人脸";
  56. richTextBox1.Text += msg + "\r\n";
  57. return;
  58. }
  59. //Console.WriteLine("识别到的人脸数量:" + infos.Length + " 个人脸信息:\n");
  60. //Console.WriteLine("No.\t人脸置信度\t位置信息");
  61. //for (int i = 0; i < infos.Length; i++)
  62. //{
  63. // Console.WriteLine(String.Format("{0}\t{1}\t{2}", i, infos[i].Score, infos[i].Location));
  64. //}
  65. //画方框,标记人脸
  66. using (Graphics g = Graphics.FromImage(bitmap))
  67. {
  68. g.DrawRectangles(new Pen(Color.Red, 2), infos.Select(p => new RectangleF(p.Location.X, p.Location.Y, p.Location.Width, p.Location.Height)).ToArray());
  69. }
  70. pictureBox1.Image = bitmap;
  71. }
  72. /// <summary>
  73. /// 活体检测
  74. /// </summary>
  75. /// <param name="sender"></param>
  76. /// <param name="e"></param>
  77. private void button3_Click(object sender, EventArgs e)
  78. {
  79. string msg = "";
  80. if (pictureBox1.Image == null)
  81. {
  82. msg = "图片1不能为空";
  83. richTextBox1.Text += msg + "\r\n";
  84. return;
  85. }
  86. FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
  87. FaceDetector faceDetector = new FaceDetector();
  88. FaceLandmarker faceMark = new FaceLandmarker();
  89. FaceAntiSpoofing faceAntiSpoofing = new FaceAntiSpoofing();
  90. if (faceDetector.Detect(bitmap).Length == 0)
  91. {
  92. msg = "图片1未检测到人脸";
  93. richTextBox1.Text += msg + "\r\n";
  94. return;
  95. }
  96. var info = faceDetector.Detect(bitmap).First();
  97. var markPoints = faceMark.Mark(bitmap, info);
  98. Stopwatch sw = Stopwatch.StartNew();
  99. sw.Start();
  100. var result = faceAntiSpoofing.AntiSpoofing(bitmap, info, markPoints);
  101. msg = "活体检测,结果:" + result.Status + ",清晰度:" + result.Clarity + ",真实度:" + result.Reality + ",耗时:" + sw.ElapsedMilliseconds + "ms";
  102. richTextBox1.Text += msg + "\r\n";
  103. Console.WriteLine(msg);
  104. sw.Stop();
  105. }
  106. /// <summary>
  107. /// 人脸比对
  108. /// </summary>
  109. /// <param name="sender"></param>
  110. /// <param name="e"></param>
  111. private void button4_Click(object sender, EventArgs e)
  112. {
  113. String msg = "";
  114. if (pictureBox1.Image == null)
  115. {
  116. msg = "图片1不能为空";
  117. richTextBox1.Text += msg + "\r\n";
  118. return;
  119. }
  120. if (pictureBox2.Image == null)
  121. {
  122. msg = "图片2不能为空";
  123. richTextBox1.Text += msg + "\r\n";
  124. return;
  125. }
  126. Stopwatch sw = Stopwatch.StartNew();
  127. sw.Start();
  128. var faceImage0 = new Bitmap(pictureBox1.Image).ToFaceImage();
  129. var faceImage1 = new Bitmap(pictureBox2.Image).ToFaceImage();
  130. //检测人脸信息
  131. FaceDetector faceDetector = new FaceDetector();
  132. FaceInfo[] infos0 = faceDetector.Detect(faceImage0);
  133. FaceInfo[] infos1 = faceDetector.Detect(faceImage1);
  134. if (infos0.Length == 0)
  135. {
  136. msg = "图片1未检测到人脸";
  137. richTextBox1.Text += msg + "\r\n";
  138. sw.Stop();
  139. return;
  140. }
  141. if (infos1.Length == 0)
  142. {
  143. msg = "图片2未检测到人脸";
  144. richTextBox1.Text += msg + "\r\n";
  145. sw.Stop();
  146. return;
  147. }
  148. //标记人脸位置
  149. FaceLandmarker faceMark = new FaceLandmarker();
  150. FaceMarkPoint[] points0 = faceMark.Mark(faceImage0, infos0[0]);
  151. FaceMarkPoint[] points1 = faceMark.Mark(faceImage1, infos1[0]);
  152. //提取特征值
  153. FaceRecognizer faceRecognizer = new FaceRecognizer();
  154. float[] data0 = faceRecognizer.Extract(faceImage0, points0);
  155. float[] data1 = faceRecognizer.Extract(faceImage1, points1);
  156. //对比特征值
  157. bool isSelf = faceRecognizer.IsSelf(data0, data1);
  158. //计算相似度
  159. float similarity = faceRecognizer.Compare(data0, data1);
  160. msg = "识别到的人脸是否为同一人:" + isSelf + ",相似度:" + similarity + ",对比耗时:" + sw.ElapsedMilliseconds + "ms";
  161. richTextBox1.Text += msg + "\r\n";
  162. Console.WriteLine(msg);
  163. sw.Stop();
  164. }
  165. /// <summary>
  166. /// 口罩检测
  167. /// </summary>
  168. /// <param name="sender"></param>
  169. /// <param name="e"></param>
  170. private void button1_Click(object sender, EventArgs e)
  171. {
  172. string msg = "";
  173. if (pictureBox1.Image == null)
  174. {
  175. msg = "图片1不能为空";
  176. richTextBox1.Text += msg + "\r\n";
  177. return;
  178. }
  179. FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
  180. FaceDetector faceDetector = new FaceDetector();
  181. MaskDetector maskDetector = new MaskDetector();
  182. if (faceDetector.Detect(bitmap).Length == 0)
  183. {
  184. msg = "图片1未检测到人脸";
  185. richTextBox1.Text += msg + "\r\n";
  186. return;
  187. }
  188. var info = faceDetector.Detect(bitmap).First();
  189. PlotMaskResult plotMaskResult = maskDetector.PlotMask(bitmap, info);
  190. if (plotMaskResult.Masked)
  191. {
  192. richTextBox1.Text += "口罩:是 \r\n";
  193. }
  194. else
  195. {
  196. richTextBox1.Text += "口罩:否 \r\n";
  197. }
  198. Console.WriteLine(msg);
  199. }
  200. /// <summary>
  201. /// 年龄预测
  202. /// </summary>
  203. /// <param name="sender"></param>
  204. /// <param name="e"></param>
  205. private void button6_Click(object sender, EventArgs e)
  206. {
  207. string msg = "";
  208. if (pictureBox1.Image == null)
  209. {
  210. msg = "图片1不能为空";
  211. richTextBox1.Text += msg + "\r\n";
  212. return;
  213. }
  214. FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
  215. FaceDetector faceDetector = new FaceDetector();
  216. if (faceDetector.Detect(bitmap).Length == 0)
  217. {
  218. msg = "图片1未检测到人脸";
  219. richTextBox1.Text += msg + "\r\n";
  220. return;
  221. }
  222. var info = faceDetector.Detect(bitmap).First();
  223. AgePredictor agePredictor = new AgePredictor();
  224. FaceLandmarker faceMark = new FaceLandmarker();
  225. var markPoints = faceMark.Mark(bitmap, info);
  226. int age = agePredictor.PredictAge(bitmap, markPoints);
  227. richTextBox1.Text += "年龄:" + age + " \r\n";
  228. Console.WriteLine(msg);
  229. }
  230. /// <summary>
  231. /// 性别预测
  232. /// </summary>
  233. /// <param name="sender"></param>
  234. /// <param name="e"></param>
  235. private void button7_Click(object sender, EventArgs e)
  236. {
  237. string msg = "";
  238. if (pictureBox1.Image == null)
  239. {
  240. msg = "图片1不能为空";
  241. richTextBox1.Text += msg + "\r\n";
  242. return;
  243. }
  244. FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
  245. FaceDetector faceDetector = new FaceDetector();
  246. if (faceDetector.Detect(bitmap).Length == 0)
  247. {
  248. msg = "图片1未检测到人脸";
  249. richTextBox1.Text += msg + "\r\n";
  250. return;
  251. }
  252. var info = faceDetector.Detect(bitmap).First();
  253. GenderPredictor genderPredictor = new GenderPredictor();
  254. FaceLandmarker faceMark = new FaceLandmarker();
  255. var markPoints = faceMark.Mark(bitmap, info);
  256. Gender gender = genderPredictor.PredictGender(bitmap, markPoints);
  257. richTextBox1.Text += "性别:" + gender + " \r\n";
  258. Console.WriteLine(msg);
  259. }
  260. /// <summary>
  261. /// 眼睛状态
  262. /// </summary>
  263. /// <param name="sender"></param>
  264. /// <param name="e"></param>
  265. private void button8_Click(object sender, EventArgs e)
  266. {
  267. string msg = "";
  268. if (pictureBox1.Image == null)
  269. {
  270. msg = "图片1不能为空";
  271. richTextBox1.Text += msg + "\r\n";
  272. return;
  273. }
  274. FaceImage bitmap = new Bitmap(pictureBox1.Image).ToFaceImage();
  275. FaceDetector faceDetector = new FaceDetector();
  276. if (faceDetector.Detect(bitmap).Length == 0)
  277. {
  278. msg = "图片1未检测到人脸";
  279. richTextBox1.Text += msg + "\r\n";
  280. return;
  281. }
  282. var info = faceDetector.Detect(bitmap).First();
  283. EyeStateDetector eyeStateDetector = new EyeStateDetector();
  284. FaceLandmarker faceMark = new FaceLandmarker();
  285. var markPoints = faceMark.Mark(bitmap, info);
  286. EyeStateResult eyeStateResult = eyeStateDetector.Detect(bitmap, markPoints);
  287. richTextBox1.Text += "眼睛状态:" + eyeStateResult + " \r\n";
  288. Console.WriteLine(msg);
  289. }
  290. }
  291. }

下载 

Demo下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/229395
推荐阅读
相关标签
  

闽ICP备14008679号