基于OpenCV人臉識別 并出提取人臉
翻遍了百度只有WPF跟WinForm的,這個可以支持Asp.Net .NetCore,本人用來網頁端上傳頭像,后端先保存網頁傳入的照片再進行照片處理就OK了,
一.首先參考兩個Nuget包
- 1.EmguEx.NET
- 2.Emgu.CV.runtime.windows
二.下載OpenCV訓練好的XML
- haarcascade_frontalface_alt.xml 鏈接 OpenCV

var face = new CascadeClassifier("haarcascade_frontalface_alt.xml");
//加載要識別的圖片
var img = new Image<Bgr, byte>(@"C:\Users\Administrator\Pictures\Saved Pictures\微信圖片_20210401213647.jpg");
//在這一步就已經識別出來了,回傳的是人臉所在的位置和大小
var facesDetected = face.DetectMultiScale(img, 1.1, 10, new Size(50, 50));
//回圈把人臉部分切出來并保存
int count = 0;
var b = img.ToBitmap();
foreach (var item in facesDetected)
{
count++;
var bmpOut = new Bitmap(item.Width, item.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
var g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(0, 0, item.Width, item.Height), new Rectangle(item.X, item.Y, item.Width, item.Height), GraphicsUnit.Pixel);
g.Dispose();
bmpOut.Save($"{count}.png", System.Drawing.Imaging.ImageFormat.Png);
bmpOut.Dispose();
}
//釋放資源退出
b.Dispose();
img.Dispose();
face.Dispose();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/335328.html
標籤:其他
上一篇:LeNet-5 卷積神經網路論文筆記-Gradient-Based Learning Applied to Document Recognition第Ⅰ、Ⅱ部分 筆記
