OpenVino快速落地部署教程

OpenVino快速落地部署教程

Openvino 是由Intel 开发的专门用于优化和部署人工智能推理的半开源的工具包,主要用于对深度推理做优化 。本教程适用于Yolov5-7.0,直接跑Yolov5为6FPS,使用OpenVino后为30FPS,未来将会出一系列其他模型(Paddle等)的OpenVino部署教程,测试平台------Intel Nuc 11代i5处理器

一、安装OpenVino

进入OpenVino官网

bash 复制代码
https://docs.openvino.ai/2024/get-started/install-openvino.html

选择自己喜欢的下载方式,本教程采用OpenVino-2022.3.1版本

二、模型转换

  1. 通过Yolov5自带的export.py文件将.pt转为.onnx格式

    bash 复制代码
    python3 export.py --weights xxxx/xxxxx.pt --include onnx --batch_size 1 --opset 10
    
    PS:如果出现转换失败的提示,如:opset 10不支持或是onnx版本问题请重新搭建yolov5环境,按照requirements.txt里库的最低版本进行安装
  2. 使用OpenVino工具链将.onnx转为xml、bin模型

    bash 复制代码
    mo --input_model xxx/xxx.onnx
    
    PS:如果openvino环境安装成功将可以在yolov5的环境中直接使用mo命令

PS:转换完成后请一定用模型可视化工具查看转换是否正确

三、采用以下代码快速部署

python 复制代码
import openvino.runtime as ov
import cv2
import numpy as np
import openvino.preprocess as op

class ObjectDetector:
    def __init__(self, model_xml, model_bin, labels, device="CPU"):
        self.core = ov.Core()
        self.model = self.core.read_model(model_xml, model_bin)
        self.labels = labels
        self.preprocess_model()
        self.compiled_model = self.core.compile_model(self.model, device)
        self.infer_request = self.compiled_model.create_infer_request()

    def preprocess_model(self):
        premodel = op.PrePostProcessor(self.model)
        premodel.input().tensor().set_element_type(ov.Type.u8).set_layout(ov.Layout("NHWC")).set_color_format(op.ColorFormat.BGR)
        premodel.input().preprocess().convert_element_type(ov.Type.f32).convert_color(op.ColorFormat.RGB).scale([255., 255., 255.])
        premodel.input().model().set_layout(ov.Layout("NCHW"))
        premodel.output(0).tensor().set_element_type(ov.Type.f32)
        self.model = premodel.build()

    def infer(self, img):
        detections = []
        img_re, dw, dh = self.resizeimg(img, (640, 640))
        input_tensor = np.expand_dims(img_re, 0)
        self.infer_request.infer({0: input_tensor})
        output = self.infer_request.get_output_tensor(0)
        detections = self.process_output(output.data[0])
        return detections

    def process_output(self, detections):
        boxes = []
        class_ids = []
        confidences = []
        for prediction in detections:
            confidence = prediction[4].item()
            if confidence >= 0.6:
                classes_scores = prediction[5:]
                _, _, _, max_indx = cv2.minMaxLoc(classes_scores)
                class_id = max_indx[1]
                if (classes_scores[class_id] > .25):
                    confidences.append(confidence)
                    class_ids.append(class_id)
                    x, y, w, h = prediction[0].item(), prediction[1].item(), prediction[2].item(), prediction[3].item()
                    xmin = x - (w / 2)
                    ymin = y - (h / 2)
                    box = np.array([xmin, ymin, w, h])
                    boxes.append(box)
        indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.5)
        detections = []
        for i in indexes:
            j = i.item()
            detections.append({"class_index": class_ids[j], "confidence": confidences[j], "box": boxes[j]})
        return detections

    def resizeimg(self, image, new_shape):
        old_size = image.shape[:2]
        ratio = float(new_shape[-1] / max(old_size))
        new_size = tuple([int(x * ratio) for x in old_size])
        image = cv2.resize(image, (new_size[1], new_size[0]))
        delta_w = new_shape[1] - new_size[1]
        delta_h = new_shape[0] - new_size[0]
        color = [100, 100, 100]
        new_im = cv2.copyMakeBorder(image, 0, delta_h, 0, delta_w, cv2.BORDER_CONSTANT, value=color)
        return new_im, delta_w, delta_h


if __name__ == "__main__":
    # Example usage:
    labels = [
        "right",
        "warning",
        "left",
        "people",
        "10",
        "pullover",
        "10off",
        "green",
        "red"
    ]
    detector = ObjectDetector("/home/nuc/MyCar/yolov5-7.0/best.xml", "/home/nuc/MyCar/yolov5-7.0/best.bin", labels, "CPU")
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        detections = detector.infer(frame)
        for detection in detections:
            classId = detection["class_index"]
            confidence = detection["confidence"]
            label = labels[classId]
            box = detection["box"]
            area = box[2] * box[3]
            print(f"Detected object: {label}, Confidence: {confidence}, Area: {area}")
    cap.release()
相关推荐
卡梅德生物科技小能手17 分钟前
卡梅德生物科普CD138(多配体蛋白聚糖-1):细胞微环境的“信号枢纽”与机制解析
经验分享·深度学习·生活
zhangfeng113318 分钟前
国家超算中心 昆山站 异构加速卡1 显存16GB详细配置, 海光 Z100SM HCU
linux·网络·深度学习·c#
大奎帝国1 小时前
Segearth-R2-03
深度学习·机器学习·计算机视觉
装不满的克莱因瓶1 小时前
掌握3D CNN模型结构——从时空特征建模到视频理解与医学影像核心架构
人工智能·pytorch·python·深度学习·神经网络·3d·cnn
YOLO数据集集合1 小时前
无人机航拍RGBT双模态行人检测数据集 | 可见光红外对齐 低空小目标检测 多模态计算机视觉基准数据
人工智能·深度学习·目标检测·计算机视觉·无人机
古希腊掌管代码的神THU1 小时前
解析 MiniMax M3 多模态大模型的架构/源码?
人工智能·深度学习·自然语言处理·面试
动物园猫1 小时前
用于实验室智能识别的目标检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·目标检测
君为先-bey1 小时前
LightningDiT----重建与生成:在潜在扩散模型中驯服优化困境
深度学习·扩散模型·视频生成·潜在扩散模型
jay神1 小时前
基于 YOLOv8 + CRNN 的车牌识别系统
深度学习·yolo·目标检测·计算机视觉·车牌识别
装不满的克莱因瓶1 小时前
掌握基于YOLO v5实现车牌目标检测任务的完整流程——从数据到部署的工业级实践
人工智能·python·深度学习·yolo·目标检测·计算机视觉·目标跟踪