您的位置:首页 > 房产 > 建筑 > .NET C# 使用OpenCV实现人脸识别

.NET C# 使用OpenCV实现人脸识别

2024/12/23 7:41:14 来源:https://blog.csdn.net/szy13323042191/article/details/139995682  浏览:    关键词:.NET C# 使用OpenCV实现人脸识别

.NET C# 使用OpenCV实现模型训练、人脸识别

码图~~~
微信图片_20240626175714

微信图片_20240626175734

1 引入依赖

image-20240626174305168

OpenCvSHarp4 - 4.10.0.20240616

OpenCvSHarp4.runtime.win - 4.10.0.20240616

2 人脸数据存储结构

image-20240626174620053

image-20240626174646845

runtime directory | face | {id}_{name} | *.jpg
id - 不可重复
name - 人名
*.jpg - 人脸照片

3 Demo

3.1 人脸识别训练

//人脸识别器
FaceRecognizer _faceRecongnizer = FisherFaceRecognizer.Create();
//人脸id,name字典
Dictionary<int, string> _faceNameDic = new Dictionary<int, string>();
//人脸数据统一大小
OpenCvSharp.Size _imgSize = new OpenCvSharp.Size(1000, 1000);private void InitializeTrain()
{string baseDir = AppDomain.CurrentDomain.BaseDirectory;string faceDir = baseDir + "face\\";string[] faceImageDirs = Directory.GetDirectories(faceDir, "*_*");//读取人脸数据List<Mat> faceMats = new List<Mat>();List<int> faceIds = new List<int>();foreach (var faceImageDir in faceImageDirs){string[] faceImages = Directory.GetFiles(faceImageDir, "*.jpg");if (faceImages.Length < 1){continue;}DirectoryInfo faceImageDirInfo = new DirectoryInfo(faceImageDir);string[] faceNameArr = faceImageDirInfo.Name.Split('_');int id = int.Parse(faceNameArr[0]);string name = faceNameArr[1];_faceNameDic.Add(id, name);IEnumerable<Mat> mats = faceImages.Select(face =>{Mat mat = new Mat(face, ImreadModes.Grayscale);Cv2.Resize(mat, mat, _imgSize);return mat;});IEnumerable<int> ids = mats.Select(e => id);faceMats.AddRange(mats);faceIds.AddRange(ids);}//训练_faceRecongnizer.Train(faceMats, faceIds);//保存训练数据//_faceRecongnizer.Save("train.xml");
}

3.2 开启摄像头进行人脸识别

//加载人眼、人脸识别训练模型
//这两个是OpenCV官方给出的,在官方库就可以找到
//https://github.com/opencv/opencv/tree/master/data/haarcascades
CascadeClassifier faceFinder = new CascadeClassifier(@"haarcascade_frontalface_default.xml");
CascadeClassifier eyeFinder = new CascadeClassifier(@"haarcascade_eye_tree_eyeglasses.xml");
using (OpenCvSharp.Window window = new OpenCvSharp.Window("video - 按ESC退出"))
//获取camera
using (FrameSource video = Cv2.CreateFrameSource_Camera(0))
using (Mat frame = new Mat())
{while (true){//获取帧video.NextFrame(frame);//进行检测识别OpenCvSharp.Rect[] faceRects = faceFinder.DetectMultiScale(frame);OpenCvSharp.Rect[] eyeRects = eyeFinder.DetectMultiScale(frame);//如果没有检测到人脸,就跳过if (faceRects.Length < 1){continue;}for (int i = 0; i < faceRects.Length; i++){//人脸区域OpenCvSharp.Rect rect = faceRects[i];using (Mat nFrame = frame.Clone()){Mat m1 = new Mat(frame, rect);Cv2.CvtColor(m1, m1, ColorConversionCodes.BGR2GRAY);//设置大小Cv2.Resize(m1, nFrame, _imgSize);//人脸识别_faceRecongnizer.Predict(nFrame, out int id, out double confidence);//置信度confidence = Math.Round(confidence, 2);_faceNameDic.TryGetValue(id, out var name);string label = name == null ? "unknow" : $"{name}  {confidence}";// 在图像上绘制文字Cv2.PutText(frame, label, new OpenCvSharp.Point(rect.Left, rect.Top - 10), HersheyFonts.HersheySimplex, 1.0, new Scalar(0, 0, 255), 2, LineTypes.Link8);}//绘制人脸框Cv2.Rectangle(frame, faceRects[i], new Scalar(0, 0, 255), 1);}//眼部区域if (eyeRects.Length > 1){for (int i = 0; i < eyeRects.Length; i++){//绘制眼部框Cv2.Rectangle(frame, eyeRects[i], new Scalar(255, 0, 0), 1);}}//显示结果window.ShowImage(frame);int v = Cv2.WaitKey(1);//ESC - 27if (v == 27){break;}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com