点击蓝字
关注我们,让开发变得更有趣
OpenVINO™
OpenVINO™ 2025 C#支持
开源的支持项目来自颜国进老师贡献,已经被 OpenVINO™ 官方收录,项目网址:
https://gitee.com/linbei_cht/OpenVINO-CSharp-API
安装非常容易,只要在 VS2022 里面点击一下即可安装。最新版本已经是 OpenVINO™ 2025 支持。
OpenVINO™
YOLO11实例分割
YOLO11 是 YOLOv5 跟 YOLOv8 作者推出最新升级版本模型,支持分类、检测、分割、姿态评估、OBB。这里以 YOLO11 实例分割模型为例,演示 OpenVINO™ C# 如何运行,YOLO11-seg 模型的输入与输出。
代码是我在 OpenVINO-CSharp-API 作者开源的 YOLOv8 对象检测的代码基础上修改而成。调用检测代码如下:
public void Detect(){ // Set the video path and model path string video_path = "D:/images/video/play_scoers.mp4"; string model_path = "D:/python/yolov5-7.0/yolo11n-seg.onnx"; // Create a new Core object and read the model Core core = new Core(); Model model = core.read_model(model_path); CompiledModel compiled_model = core.compile_model(model, "GPU"); // Create a list of InferRequest objects Listrequests = new List{ compiled_model.create_infer_request(), compiled_model.create_infer_request() }; // Create a new VideoCapture object and read the video VideoCapture capture = new VideoCapture(video_path); if (!capture.IsOpened()) { Console.WriteLine("Error: Video not found!"); return; } Mat frame = new Mat(); Mat next_frame = new Mat(); capture.Read(frame); float scale = 0.0f; float[] input_data = preprocess(frame, out scale); requests[0].get_input_tensor().set_data(input_data); requests[0].start_async(); Stopwatch sw = new Stopwatch(); float[] total_infs = new float[3]; ListclassList = File.ReadAllLines("D:/python/yolov5-7.0/classes.txt").Select(line => line.Trim()).ToList(); while (true) { if (!capture.Read(next_frame)) { break; } sw.Restart(); input_data = preprocess(frame, out scale); requests[1].get_input_tensor().set_data(input_data); requests[1].start_async(); requests[0].wait(); float[] det_data = requests[0].get_tensor("output0").get_data<float>(8400 * 116); float[] seg_data = requests[0].get_tensor("output1").get_data<float>(32 * 160*160); Mat rgb_mask = new Mat(frame.Size(), frame.Type()); DetResult result = postprocess(det_data, seg_data, scale, rgb_mask); sw.Stop(); total_infs[0] = sw.ElapsedMilliseconds; Cv2.PutText(frame, "Inference: " + (1000.0 / total_infs[0]).ToString("0.00") + "FPS " + (total_infs[0]).ToString("0.00") + "ms", new OpenCvSharp.Point(20, 40), HersheyFonts.HersheyPlain, 2, new Scalar(255, 0, 255), 2); result.update_lable(classList); Visualize.draw_det_result(result, frame); Cv2.AddWeighted(frame, 0.5, rgb_mask, 0.5, 0, frame); Cv2.ImShow("C# YOLO11-OpenVINO-Seg演示 - OpenCV学堂", frame); // Press 'ESC' to exit the program if (Cv2.WaitKey(1) == 27) { break; } swap(requests); frame = next_frame; rgb_mask.Release(); }}
运行结果如下:
OpenVINO™
后处理实现细节
这个实现最大的坑在后处理部分,要基于全局编码信息乘以每个检测 BOX 区域的编码信息,才可以解码得到每个 BOX 对象的掩膜。实现的代码如下:
Mat roi_mask = roi_masks[index];Mat m = roi_mask * mask_info;for (int col = 0; col < m.Cols; col++){ m.At<float>(0, col) = sigmoid_function(m.At<float>(0, col));}
最后根据得到掩膜直接设置 BOX 区域的颜色即可,代码如下:
rgb_mask[box].SetTo(new Scalar(0, 0, 255), box_m);re.add(classIds[index], confidences[index], positionBoxes[index]);
然后把得到 RGB 彩色掩膜图像跟 BOX 框绘制图像相加记得到最终输出结果图像。
OpenVINO™
---------------------------------------
*OpenVINO and the OpenVINO logo are trademarks of Intel Corporation or its subsidiaries.
-----------------------------
OpenVINO 中文社区
微信号 : openvinodev
B站:OpenVINO中文社区
“开放、开源、共创”
致力于通过定期举办线上与线下的沙龙、动手实践及开发者交流大会等活动,促进人工智能开发者之间的交流学习。
○ 点击 “ 在看 ”,让更多人看见