Program.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Drawing.Imaging;
  4. using System.Drawing;
  5. namespace ConsoleApp2
  6. {
  7. class Program
  8. {
  9. [DllImport("model_infer.dll", EntryPoint = "InitModel")]
  10. public static extern void InitModel(string model_type, string model_filename, string params_filename, string cfg_file);
  11. [DllImport("model_infer.dll", EntryPoint = "ModelPredict")]
  12. public static extern void ModelPredict(byte[] img, int W, int H, int C, IntPtr output, int[] BoxesNum, ref byte label);
  13. [DllImport("model_infer.dll", EntryPoint = "DestructModel")]
  14. public static extern void DestructModel();
  15. static void Main(string[] args)
  16. {
  17. string imgfile = "E:\\PaddleX_deploy\\PaddleX\\dygraph\\deploy\\cpp\\out\\paddle_deploy\\1.png";
  18. string model_type = "det";
  19. string model_filename = "E:\\PaddleX_deploy\\PaddleX\\dygraph\\deploy\\cpp\\out\\paddle_deploy\\yolov3_darknet53_270e_coco1\\model.pdmodel";
  20. string params_filename = "E:\\PaddleX_deploy\\PaddleX\\dygraph\\deploy\\cpp\\out\\paddle_deploy\\yolov3_darknet53_270e_coco1\\model.pdiparams";
  21. string cfg_file = "E:\\PaddleX_deploy\\PaddleX\\dygraph\\deploy\\cpp\\out\\paddle_deploy\\yolov3_darknet53_270e_coco1\\infer_cfg.yml";
  22. InitModel(model_type, model_filename, params_filename, cfg_file);
  23. Bitmap bmp = new Bitmap(imgfile);
  24. byte[] inputData = GetBGRValues(bmp, out int stride);
  25. float[] resultlist = new float[600];
  26. IntPtr results = FloatToIntptr(resultlist);
  27. int[] boxesInfo = new int[1];
  28. Byte[] labellist = new Byte[1000]; //新建字节数组
  29. //第四个参数为输入图像的通道数
  30. ModelPredict(inputData, bmp.Width, bmp.Height, 4, results, boxesInfo, ref labellist[0]);
  31. string strGet = System.Text.Encoding.Default.GetString(labellist, 0, labellist.Length); //将字节数组转换为字符串
  32. Console.WriteLine("labellist: {0}", strGet);
  33. for (int i = 0; i < boxesInfo[0]; i++)
  34. {
  35. int labelindex = Convert.ToInt32(resultlist[i * 6 + 0]);
  36. float score = resultlist[i * 6 + 1];
  37. float left = resultlist[i * 6 + 2];
  38. float top = resultlist[i * 6 + 3];
  39. float width = resultlist[i * 6 + 4];
  40. float height = resultlist[i * 6 + 5];
  41. Console.WriteLine("score: {0}", score);
  42. Console.WriteLine("labelindex: {0}", labelindex);
  43. Console.WriteLine("boxe: {0} {1} {2} {3}", left, top, width, height);
  44. }
  45. DestructModel();
  46. }
  47. // 将Btimap类转换为byte[]类函数
  48. public static byte[] GetBGRValues(Bitmap bmp, out int stride)
  49. {
  50. var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  51. var bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
  52. stride = bmpData.Stride;
  53. var rowBytes = bmpData.Width * Image.GetPixelFormatSize(bmp.PixelFormat) / 8;
  54. var imgBytes = bmp.Height * rowBytes;
  55. byte[] rgbValues = new byte[imgBytes];
  56. IntPtr ptr = bmpData.Scan0;
  57. for (var i = 0; i < bmp.Height; i++)
  58. {
  59. Marshal.Copy(ptr, rgbValues, i * rowBytes, rowBytes);
  60. ptr += bmpData.Stride;
  61. }
  62. bmp.UnlockBits(bmpData);
  63. return rgbValues;
  64. }
  65. public static IntPtr FloatToIntptr(float[] bytes)
  66. {
  67. GCHandle hObject = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  68. return hObject.AddrOfPinnedObject();
  69. }
  70. }
  71. }