當前位置: 妍妍網 > 碼農

C#基於yolov3的行人檢測

2024-02-18碼農
  • 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;namespaceWindowsFormsApp1{publicpartial classForm1 : Form {privateconststring YoloLibraryName = @"D:\darknet-master (1)\darknet-master\build\darknet\x64\yolo_cpp_dll.dll";privateconstint MaxObjects = 1000;object ThreadLock = newobject(); [DllImport(YoloLibraryName, EntryPoint = "init")]privatestaticexternintInitializeYolo(string configurationFilename, string weightsFilename, int gpu); [DllImport(YoloLibraryName, EntryPoint = "detect_image")]privatestaticexternintDetectImage(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")]privatestaticexternintDisposeYolo(); [StructLayout(LayoutKind.Sequential)]publicstruct BboxContainer { [MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxObjects)]public bbox_t[] candidates; } [StructLayout(LayoutKind.Sequential)]publicstruct bbox_t {public UInt32 x, y, w, h; // (x,y) - top-left corner, (w, h) - width & height of bounded boxpublicfloat prob; // confidence - probability that the object was found correctlypublic 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; };publicForm1() { InitializeComponent(); }privatevoidForm1_Load(object sender, EventArgs e) {//Task.Run(() => InitYolo()); InitYolo(); }publicstaticvoidInitYolo() { 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) {thrownew 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);returnnew List<bbox_t>(); }finally {// Free the unmanaged memory. Marshal.FreeHGlobal(pnt); //不釋放記憶體會報錯 } }privatevoidbutton1_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 = newbyte[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); // BGRvar 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); }elseif (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

    intdetect_image(unsignedint width, unsignedint height, unsignedchar* data, constsize_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();;}