相关文章推荐
眼睛小的足球  ·  【机器学习-因果推断】grf ...·  2 年前    · 
听话的牛腩  ·  将Laravel刀片模板渲染为没有转义字符和 ...·  2 年前    · 
满身肌肉的梨子  ·  基于长短时记忆网络的医疗设备故障智能诊断研究 ...·  2 年前    · 
单身的皮带  ·  Oracle中like与regexp_lik ...·  2 年前    · 
瘦瘦的野马  ·  python 每个月最后一天-掘金·  2 年前    · 
Code  ›  C#基于yolov3的行人检测开发者社区
bbox
https://cloud.tencent.com/developer/article/1682076
千杯不醉的路灯
2 年前
zls365

C#基于yolov3的行人检测

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
zls365
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > C#基于yolov3的行人检测

C#基于yolov3的行人检测

作者头像
zls365
发布 于 2020-08-19 11:47:53
1.1K 0
发布 于 2020-08-19 11:47:53
举报
文章被收录于专栏: CSharp编程大全 CSharp编程大全 CSharp编程大全
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-master\build\darknet\x64\yolo_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"),
        [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);
                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:\Users\admin\source\repos\WindowsFormsApp1\WindowsFormsApp1\bin\x64\Debug\Camera20200421194241118.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);
                    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);
 
推荐文章
眼睛小的足球  ·  【机器学习-因果推断】grf 广义随机森林包函数列表(R)vs EconML(Python) - 知乎
2 年前
听话的牛腩  ·  将Laravel刀片模板渲染为没有转义字符和换行的字符串?-腾讯云开发者社区-腾讯云
2 年前
满身肌肉的梨子  ·  基于长短时记忆网络的医疗设备故障智能诊断研究 - PMC
2 年前
单身的皮带  ·  Oracle中like与regexp_like的用法_ITPUB博客
2 年前
瘦瘦的野马  ·  python 每个月最后一天-掘金
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号