当前位置:   article > 正文

YOLOv8实例分割-使用.Net /.net Core 实例分割

yolov8实例分割

A 背景

最近在学习实例分割,对于小白来说。整理自己学习的过程。

会包含以下几个部署

1:YoloV8部署与demo运行,及相关问题(整理中)

2:YoloV8 自定义数据集训练与及相关要点(整理中)

3:YoloV8 实例分割怎样落地到应用-python方案(整理中)

4:YoloV8 实例分割怎样落地应用-使用.Net /.net Core方案(本文)

基于.Net 实现YoloV8分割模型YoloV8Net.Segment

(本人是小白,内容有参考及引用以下小神,多谢了。 基于.Net6使用YoloV8的分割模型_Heliophyte阳的博客-CSDN博客

B 前言

为什么要使用 .Net /.net Core来实现 YoloV8的实例分割。因我的项目是做图书的图书脊梁图片分割(基于图脊的盘点方案实现)。我的项目都是.net 来实现的,分割只是其中一个小环节。为了程序的统一及方便部署等。

效果及目标

1掩码

2对应分割图(矩形)

 

 3截取分割图对应掩码图

 C 将yolov8 pt模型转为onnx

训练后你会得到best.pt文件(也可以使用官方的 模型文件 如 yolov8x-seg.pt)

  1. import sys
  2. from ultralytics import YOLO
  3. # Create a new YOLO model from scratch
  4. #model = YOLO('yolov8n.yaml')
  5. if __name__ == '__main__':
  6. model = YOLO('你的具体地址/best.pt')
  7. success = model.export(format='onnx')#,opset=12
  8. '''

D 代码要点与调整

1调整分割标签

  1. protected void UseCoCoLabels()
  2. {
  3. var s = new string[] { "book" };
  4. //var s = new string[] { "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" };
  5. UseCustomLabels(s);
  6. }

2根据情况,调整过滤(分割结果)的关健条件(占比)

   public float Overlap { get; protected set; } = 0.65f;//hcrain  0.45f;

3修复掩码的bug 且将掩码区转为透明(我也不太明白 小神这里的操作(后面再分割吧),我只是加工了下)

  1. for (int r = 0; r < nms_results.Length; r++)
  2. {
  3. Mat rgb_mask = Mat.Zeros(new OpenCvSharp.Size(image.Width, image.Height), MatType.CV_8UC3);
  4. Rect box = nms_results[r].Rectangle;
  5. int box_x1 = box.X;
  6. int box_y1 = box.Y;
  7. int box_x2 = box.BottomRight.X;
  8. int box_y2 = box.BottomRight.Y;
  9. // Segment Result
  10. Mat original_mask = nms_results[r].Mask * proto_data;
  11. //Mat original_mask = result.ToList()[10].Mask * proto_data;
  12. Parallel.For(0, original_mask.Cols, col =>
  13. {
  14. original_mask.At<float>(0, col) = Utils.Sigmoid(original_mask.At<float>(0, col));
  15. });
  16. // 1x25600 -> 160x160
  17. Mat reshape_mask = original_mask.Reshape(1, 160);
  18. // scale
  19. //float scale = Math.Max(w, h) / 640f;
  20. int mx1 = Math.Max(0, (int)(box_x1 * gain * 0.25));
  21. int mx2 = Math.Max(0, (int)(box_x2 * gain * 0.25));
  22. int my1 = Math.Max(0, (int)(box_y1 * gain * 0.25));
  23. int my2 = Math.Max(0, (int)(box_y2 * gain * 0.25));
  24. // get roi
  25. Mat mask_roi = new Mat(reshape_mask, new OpenCvSharp.Range(my1, my2), new OpenCvSharp.Range(mx1, mx2));
  26. Mat actual_maskm = new Mat();
  27. Cv2.Resize(mask_roi, actual_maskm, new OpenCvSharp.Size((int)nms_results[r].Rectangle.Width,
  28. (int)nms_results[r].Rectangle.Height));
  29. Cv2.Threshold(actual_maskm, actual_maskm, 0.5, 255, ThresholdTypes.Binary);
  30. // predict
  31. Mat bin_mask = new Mat();
  32. //actual_maskm.ImWrite("actual_maskm.png");
  33. actual_maskm.ConvertTo(bin_mask, MatType.CV_8UC1);
  34. if ((nms_results[r].Rectangle.Y + bin_mask.Rows) >= image.Height)
  35. {
  36. box_y2 = (int)image.Height - 1;
  37. }
  38. if ((box_x1 + bin_mask.Cols) >= image.Width)
  39. {
  40. box_x2 = (int)image.Width - 1;//image.Width = 3
  41. }
  42. // get segment region
  43. Mat mask = Mat.Zeros(new OpenCvSharp.Size((int)image.Width, (int)image.Height), MatType.CV_8UC1);
  44. bin_mask = new Mat(bin_mask, new OpenCvSharp.Range(0, box_y2 - box_y1), new OpenCvSharp.Range(0, box_x2 - box_x1));
  45. Rect roi = new Rect(box_x1, box_y1, box_x2 - box_x1, box_y2 - box_y1);
  46. bin_mask.CopyTo(new Mat(mask, roi));
  47. //segment region colorful
  48. // Cv2.Add(rgb_mask, new Scalar(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255), 255), rgb_mask, mask);
  49. // Cv2.Add(rgb_mask,null, rgb_mask, mask);
  50. nms_results[r].RgbMask = rgb_mask;
  51. }

F 有了分割结果,就看怎样与自己的业务结合处理了,这里对我来说的最重要的是过滤。

过滤无效的书

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/122546
推荐阅读
相关标签
  

闽ICP备14008679号