C#基于yolov3的行人检测
时间:2022-07-23
本文章向大家介绍C#基于yolov3的行人检测,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private const string YoloLibraryName = @"D:darknet-master (1)darknet-masterbuilddarknetx64yolo_cpp_dll.dll";
private const int MaxObjects = 1000;
object ThreadLock = new object();
[DllImport(YoloLibraryName, EntryPoint = "init")]
private static extern int InitializeYolo(string configurationFilename, string weightsFilename, int gpu);
[DllImport(YoloLibraryName, EntryPoint = "detect_image")]
private static extern int DetectImage(uint width, uint height, byte[] pArray, int nSize, ref BboxContainer container);
//[DllImport(YoloLibraryName, EntryPoint = "adddd")]
//private static extern int adddd(int a, int b, ref int result, byte[] pArray);
[DllImport(YoloLibraryName, EntryPoint = "dispose")]
private static extern int DisposeYolo();
[StructLayout(LayoutKind.Sequential)]
public struct BboxContainer
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxObjects)]
public bbox_t[] candidates;
}
[StructLayout(LayoutKind.Sequential)]
public struct bbox_t
{
public UInt32 x, y, w, h; // (x,y) - top-left corner, (w, h) - width & height of bounded box
public float prob; // confidence - probability that the object was found correctly
public UInt32 obj_id; // class of object - from range [0, classes-1]
public UInt32 track_id; // tracking id for video (0 - untracked, 1 - inf - tracked object)
public UInt32 frames_counter;
};
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Task.Run(() => InitYolo());
InitYolo();
}
public static void InitYolo()
{
InitializeYolo(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "yolo-obj.cfg"),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "yolo-obj_best.weights"),
0);
}
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
public List<bbox_t> Detect(int Height, int Width, byte[] imageData)
{
var container = new BboxContainer();
var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
var pnt = Marshal.AllocHGlobal(size);
try
{
Marshal.Copy(imageData, 0, pnt, imageData.Length);
var count = DetectImage((uint)Width, (uint)Height, imageData, imageData.Length, ref container);
if (count == -1)
{
throw new NotSupportedException(" has no OpenCV support");
}
List<bbox_t> result = new List<bbox_t>();
for (int i = 0; i < count; i++)
{
result.Add(container.candidates[i]);
}
return result;
}
catch (Exception exception)
{
Console.WriteLine("Error : ");
Console.WriteLine(exception.Message);
return new List<bbox_t>();
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt); //不释放内存会报错
}
}
private void button1_Click(object sender, EventArgs e)
{
//Image<Bgr, byte> img = new Image<Bgr, byte>(@"C:UsersadminsourcereposWindowsFormsApp1WindowsFormsApp1binx64DebugCamera20200421194241118.jpg");
Image<Gray, byte> img1 = new Image<Gray, byte>(@"Camera20200421194241118.jpg");
int Width, Height;
Width = img1.Width;
Height = img1.Height;
while (Width % 4 != 0)
{
Width++;
}
CvInvoke.Resize(img1, img1, new Size(Width, Height), 0, 0, Inter.Lanczos4);
Matrix<byte> showImage = new Matrix<byte>(img1.Height, img1.Width, 3);
CvInvoke.CvtColor(img1, showImage, ColorConversion.Gray2Bgr);
List<bbox_t> bboxes = new List<bbox_t>();
lock (ThreadLock) //锁线程
{
int byte_size = showImage.Rows * showImage.Cols * 3;
byte[] img_data_in = new byte[byte_size];
Array.Copy(showImage.Mat.GetData(), img_data_in, byte_size);
//bboxes = Detect(showImage.Height, showImage.Width, img_data_in);//方法一
bboxes = Detect(showImage.Height, showImage.Width, showImage.Bytes);//方法二
}
if (bboxes.ToArray().Length != 0)
{
//MessageBox.Show("1111");
}
foreach (var bbox in bboxes)
{
var color_red = new MCvScalar(0, 0, 255); // BGR
var color_green = new MCvScalar(0, 255, 0);
var color_yellow = new MCvScalar(0, 255, 255);
Rectangle rect = new Rectangle((int)bbox.x, (int)bbox.y, (int)bbox.w, (int)bbox.h);
if (bbox.obj_id == 0)
{
CvInvoke.Rectangle(showImage, rect, color_yellow);
}
else if (bbox.obj_id == 2)
{
CvInvoke.Rectangle(showImage, rect, color_green);
}
else
{
CvInvoke.Rectangle(showImage, rect, color_red);
}
}
pictureBox1.Image = showImage.Mat.Bitmap;
}
}
}
运行结果:
yolo_cpp_dll中的yolo_v2_class.cpp需要修改下构造函数detect_image
int detect_image(unsigned int width, unsigned int height, unsigned char* data, const size_t data_length, bbox_t_container &container)
{
cv::Mat image = cv::Mat(height, width, CV_8UC3, data, 3 * width);
//cv::imshow("test_img", image);
//cv::waitKey(1);
std::vector<bbox_t> detection = detector->detect(image);
for (size_t i = 0; i < detection.size() && i < C_SHARP_MAX_OBJECTS; ++i)
container.candidates[i] = detection[i];
return detection.size();;
}
- 执行git push出现"Everything up-to-date"
- linux下EOF写法梳理
- 用AngularJS来实现异步数据的购物车功能设计
- span不如div的地方
- 分布式监控系统Zabbix--完整安装记录(7)-使用percona监控MySQL
- 10x Python开发者必读:本月Python文章TOP 10
- Linux下更换默认yum源为网易yum源的操作记录
- yum源使用的几个报错小总结
- JQuery笔记(一)
- Haproxy和Nginx负载均衡测试效果对比记录
- JQuery笔记(三) jquery的用途
- Heartbeat使用梳理
- JQuery笔记(二) animate支持的属性
- 腾讯叮当首次系统性展示AI能力 开放日展示实战型解决方案
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 超性感的React Hooks(十)useRef
- 七日Python之路--第三天(之初试Django 2-1)
- 超性感的React Hooks(十一)useCallback、useMemo
- 七日Python之路--第二天
- 八、通过断点调试观察JS执行过程
- 传智播客OA项目学习--阶段三(Struts中Action书写)
- 九、函数与函数式编程
- 关于Hibernate懒加载----Spring filter
- 十、详解函数柯里化
- Unable to load file:*struts.xml-[unknown location]
- 使用hooks重新定义antd pro想象力(一)
- 传智播客OA项目学习--阶段三(级联操作)
- 七日Python之路--第十一天
- 使用hooks重构antd pro的想象力(三)我是如何利用hooks干掉redux的
- 七日Python之路--第十天