[Winform]基于Emgu.CV人脸识别
摘要
“OpenCV是一个开源的计算机视觉库。OpenCV采用C/C++语言编写,可以运行在Linux/Windows/Mac等操作系统上。OpenCV还提供了Python、Ruby、MATLAB以及其他语言的接口。OpenCV的一个目标是构建一个简单易用的计算机视觉框架,以帮助开发人员更便捷地设计更复杂得计算机视觉相关应用程序。OpenCV包含的函数有500多个,覆盖了计算机视觉的许多应用。
Emgu
Emgu CV是将OpenCV使用.net编程语言(C#)封装成的.net库,使用Emgu CV就可以在.net平台上调用OpenCV的功能,同时,Emgu CV也是开源的。
Emgu CV官网:http://www.emgu.com
从官网上你可以下载最新版本,我采用的是3.2.0.2682版本的。
安装成功之后,打开目录
打开解决方案,你可以看到很多demo,可以根据自己需求研究下
新建winform程序,添加引用,Emgu安装目录下的dll
并将所需的文件从Emgu的bin目录下拷贝到你的程序debug下
测试核心代码
public partial class MainFrm : Form { /// <summary> /// 摄像头 /// </summary> private VideoCapture _capture = null; private Mat _frame; private string _exePath = AppDomain.CurrentDomain.BaseDirectory; private string _trainedFacePath = string.Empty; /// <summary> /// 3.5s 保存一次图片 /// </summary> private const double Interval = 3.5; long detectionTime; List<Rectangle> faces = new List<Rectangle>(); List<Rectangle> eyes = new List<Rectangle>(); /// <summary> /// 上次保存图片时间 /// </summary> private DateTime _lastSaveDt = DateTime.Now; /// <summary> /// 窗口坐标 /// </summary> private Point _frmPoint; /// <summary> /// 是否抓取到人脸 /// </summary> private bool _isHavePersonFace = false; public MainFrm() { InitializeComponent(); this.TopMost = true; CheckForIllegalCrossThreadCalls = false; _frmPoint = new Point(); } private void MainFrm_Load(object sender, EventArgs e) { //无边框 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; //不出现在任务栏 this.ShowInTaskbar = false; //去掉滑轮放大或者缩小 this.VideoImageBox.Enabled = false; this.MouseDown += MainFrm_MouseDown; this.MouseMove += MainFrm_MouseMove; _trainedFacePath = _exePath + "trainedfaces"; this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 1,
Screen.PrimaryScreen.WorkingArea.Height - this.Height); if (!Directory.Exists(_trainedFacePath)) { Directory.CreateDirectory(_trainedFacePath); } CvInvoke.UseOpenCL = false; try { _capture = new VideoCapture(); _frame = new Mat(); _capture.ImageGrabbed += ProcessFrame; _capture.Start(); } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } void MainFrm_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point myPosittion = MousePosition; myPosittion.Offset(-_frmPoint.X, -_frmPoint.Y); Location = myPosittion; } } void MainFrm_MouseDown(object sender, MouseEventArgs e) { _frmPoint.X = e.X; _frmPoint.Y = e.Y; } private void ReleaseData() { if (_capture != null) _capture.Dispose(); } private void ProcessFrame(object sender, EventArgs e) { if (_capture != null && _capture.Ptr != IntPtr.Zero) { bool isOk = _capture.Retrieve(_frame, 0); VideoImageBox.Image = _frame; if (isOk && !_isHavePersonFace) { Run(_frame); } } } private void UploadFile(string filePath) { try { FileInfo file = new FileInfo(filePath); FileLogHelper.WriteFileLog(file.Name); } catch (Exception ex) { FileLogHelper.WriteFileLog(ex); } } private void Run(Mat mat) { IImage image = mat; DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; string path = Path.Combine(_trainedFacePath, fileName); foreach (Rectangle face in faces) { try { _isHavePersonFace = true; CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 1); using (Bitmap bm = new Bitmap(face.Width, face.Height)) using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(image.Bitmap, new Rectangle(0, 0, face.Width, face.Height),
new Rectangle(face.X + 1, face.Y + 1, face.Width - 1, face.Height - 1), GraphicsUnit.Pixel); bm.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); } break; } catch (Exception ex) { FileLogHelper.WriteFileLog(ex); } } _isHavePersonFace = false; } }
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2016-07-20 [c#]获取exchange中的图片