YOLO-World是一个融合了实时目标检测与增强现实(AR)技术的创新平台,旨在将现实世界与数字世界无缝对接。该平台以YOLO(You Only Look Once)算法为核心,实现了对视频中物体的快速准确识别,并通过AR技术将虚拟元素与真实场景相结合,为用户带来沉浸式的交互体验。在本文中,我们将结合OpenVINO™ C# API 使用最新发布的OpenVINO™ 2024.0部署 YOLO-World实现实时开放词汇对象检测:
2024年3月7日,英特尔发布了开源 OpenVINO™ 2024.0 工具包,用于在各种硬件上优化和部署人工智能推理。OpenVINO™ 是英特尔出色的开源 AI 工具包,不仅可以在 x86_64 CPU 上加速 AI 推断,还可以在 ARM CPU 和其他架构、英特尔集成显卡和独立显卡等硬件上加速 AI 推断,包括最近推出的 NPU 插件,用于利用新酷睿超 “Meteor Lake “系统芯片中的英特尔神经处理单元。 OpenVINO™ 2024.0 更注重生成式人工智能(GenAI),为 TensorFlow 句子编码模型提供了更好的开箱即用体验,支持专家混合(MoE)。同时还提高了 LLM 的 INT4 权重压缩质量,增强了 LLM 在英特尔 CPU 上的性能,简化了 Hugging Face 模型的优化和转换,并改进了其他 Hugging Face 集成。
OpenVINO™ C# API 是一个 OpenVINO™ 的 .Net wrapper,应用最新的 OpenVINO™ 库开发,通过 OpenVINO™ C API 实现 .Net 对 OpenVINO™ Runtime 调用,使用习惯与 OpenVINO™ C++ API 一致。OpenVINO™ C# API 由于是基于 OpenVINO™ 开发,所支持的平台与 OpenVINO™ 完全一致,具体信息可以参考 OpenVINO™。通过使用 OpenVINO™ C# API,可以在 .NET、.NET Framework等框架下使用 C# 语言实现深度学习模型在指定平台推理加速。
from ultralytics import YOLO # Initialize a YOLO-World model model = YOLO('yolov8s-worldv2.pt') # Define custom classes model.set_classes(["person", "bus"]) # Export the model model.export(format='onnx')
Mat image = Cv2.ImRead(tb_input_path.Text); Mat mat = new Mat(); Cv2.CvtColor(image, mat, ColorConversionCodes.BGR2RGB); mat = OpenVinoSharp.Extensions.process.Resize.letterbox_img(mat, (int)input_shape[2], out factor); mat = Normalize.run(mat, true); float[] input_data = Permute.run(mat);
DetResult postprocess(float[] result, int categ_nums, float factor) { Mat result_data = new Mat(4 + categ_nums, 8400, MatType.CV_32F,result); result_data = result_data.T(); // Storage results list List<Rect> position_boxes = new List<Rect>(); List<int> classIds = new List<int>(); List<float> confidences = new List<float>(); // Preprocessing output results for (int i = 0; i < result_data.Rows; i++) { Mat classesScores = new Mat(result_data, new Rect(4, i, categ_nums, 1)); Point maxClassIdPoint, minClassIdPoint; double maxScore, minScore; // Obtain the maximum value and its position in a set of data Cv2.MinMaxLoc(classesScores, out minScore, out maxScore, out minClassIdPoint, out maxClassIdPoint); // Confidence level between 0 ~ 1 // Obtain identification box information if (maxScore > 0.25) { float cx = result_data.At<float>(i, 0); float cy = result_data.At<float>(i, 1); float ow = result_data.At<float>(i, 2); float oh = result_data.At<float>(i, 3); int x = (int)((cx - 0.5 * ow) * factor); int y = (int)((cy - 0.5 * oh) * factor); int width = (int)(ow * factor); int height = (int)(oh * factor); Rect box = new Rect(); box.X = x; box.Y = y; box.Width = width; box.Height = height; position_boxes.Add(box); classIds.Add(maxClassIdPoint.X); confidences.Add((float)maxScore); } } // NMS non maximum suppression int[] indexes = new int[position_boxes.Count]; float score = float.Parse(tb_score.Text); float nms = float.Parse(tb_nms.Text); CvDnn.NMSBoxes(position_boxes, confidences, score, nms, out indexes); DetResult re = new DetResult(); // for (int i = 0; i < indexes.Length; i++) { int index = indexes[i]; re.add(classIds[index], confidences[index], position_boxes[index]); } return re; }
以上就是使用 OpenVINO™ C# API 部署YOLO-World模型的关键代码,具体代码可以下载项目源码进行查看。