识别效果
演示视频
使用方法
这是 YoloV3 和 Caffe 模型中的实现用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCVCSharpDNN.Impl;
using OpenCVCSharpDNN;
using System.IO;
using System.Drawing;
namespace OpenCVCSharp.Example
{
class Program
{
static void Main(string[] args)
{
//包含模型和配置文件的目录
string dir = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "data");
// YoloV3模型
string model = System.IO.Path.Combine(dir, "yolov3.weights");
string cfg = System.IO.Path.Combine(dir, "yolov3.cfg");
string labelsYolo = System.IO.Path.Combine(dir, "coco.names");
//人脸检测模型
string modelFace = System.IO.Path.Combine(dir, "yolov3-wider_16000.weights");
string cfgFace = System.IO.Path.Combine(dir, "yolov3-face.cfg");
//性别分类模型
string modelGenderCaffe = System.IO.Path.Combine(dir, "gender_net.caffemodel");
string cfgGenderCaffe = System.IO.Path.Combine(dir, "deploy_gender.prototxt");
//图片路径
string testImage = System.IO.Path.Combine(dir, "1.bmp");//friends.jpg
using (NetYoloV3 yoloV3 = new NetYoloV3())
using (NetYoloV3 yoloV3Faces = new NetYoloV3())
using (NetCaffeAgeGender caffeGender = new NetCaffeAgeGender())
using (Bitmap bitmap = new Bitmap(testImage))
using (Bitmap resultImage = new Bitmap(testImage))
{
//初始化模型
yoloV3.Initialize(model, cfg, labelsYolo);
yoloV3Faces.Initialize(modelFace, cfgFace, new string[] { "faces" });
caffeGender.Initialize(modelGenderCaffe, cfgGenderCaffe, new string[] { "Male", "Female" });
//获取 YoloV3 的结果
NetResult[] resultPersons = yoloV3.Detect(bitmap, labelsFilters: new string[] { "person" });//检测人
//获取 YoloV3 人脸检测的结果
NetResult[] resultFaces = yoloV3Faces.Detect(bitmap);//检测人脸
using (Graphics canvas = Graphics.FromImage(resultImage))
{
Font font = new Font(FontFamily.GenericSansSerif, 15);
//推理性别,绘制标签及人脸边界框
foreach (NetResult item in resultFaces)
{
//为每张脸创建感兴趣区域
using (Bitmap roi = (Bitmap)bitmap.Clone(item.Rectangle, bitmap.PixelFormat))
{
NetResult resultGender = caffeGender.Detect(roi).FirstOrDefault();//检测人脸区域推理性别
//绘制标签
canvas.DrawString($"{resultGender.Label} {resultGender.Probability:0.0%}",
font,
new SolidBrush(Color.Green),
item.Rectangle.X - font.GetHeight(), item.Rectangle.Y - font.GetHeight());
}
canvas.DrawRectangle(new Pen(Color.Red, 2), item.Rectangle);
}
canvas.Save();
}
resultImage.Save(Path.Combine(dir, "result.jpg"));//保存识别结果
}
}
}
}
预训练模型
您可以在以下链接中下载预训练模型:
|YoloV3|
https://pjreddie.com/darknet/yolo/
|YoloV3 Faces|
http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/index.html
|Caffe Age and Gender Classification|
https://talhassner.github.io/home/publication/2015_CVPR
参考:
https://github.com/julian9012/OpenCVCSharpDNN
https://drive.google.com/drive/folders/1oj9p04mPjbbCbq1qSK8ChMjOhMLMpk42