首页 > 其他分享 >OpenVino快速落地部署教程

OpenVino快速落地部署教程

时间:2024-08-27 15:37:20浏览次数:7  
标签:OpenVino 落地 self 教程 detections new model size

OpenVino快速落地部署教程

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

一、安装OpenVino

进入OpenVino官网

https://docs.openvino.ai/2024/get-started/install-openvino.html

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

二、模型转换

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

    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模型

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

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

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

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()

标签:OpenVino,落地,self,教程,detections,new,model,size
From: https://www.cnblogs.com/SkyXZ/p/18382813

相关文章

  • Mockito教程(单测mock)
    1Mockito介绍[3]1.1Mockito是什么?Mockito是mocking框架,它让你用简洁的API做测试。而且Mockito简单易学,它可读性强和验证语法简洁。1.2为什么需要Mock测试驱动的开发(TDD)要求我们先写单元测试,再写实现代码。在写单元测试的过程中,我们往往会遇到要测试的类有很多依赖,这些......
  • AntFlow系列教程之流程提交
    AntFlow为笔者基于activiti深度定制的一款简单易用的开源低代码流程引擎,类似钉钉工作流.详细介绍可以查看企业级仿钉钉低代码工作流引擎开源啦.项目刚开源不久,希望喜欢的大佬们多点赞关注.后面除了会写文章介绍AntFlow的使用,还会写文章介绍activiti8的使用.流程的操......
  • delphi初级教程之delphi断点调试一
    所谓断点,就是在程序代码的某一行上设置一个标记,程序执行到这里将暂停,由调试器接管对程序的控制。使用断点与使用【RuntoCursor】命令有些相似,都是执行到某一行后暂停。不同的是,程序中可以设置多个断点并且能够给断点设置条件。断点通常设置在有疑点的区域。在遇到断点之前,程序......
  • 【开源分享】PHP工单管理系统源码 带搭建教程
    一、设备报修工作内容1.工单管理:设备报修系统可以将设备故障统计为工单并对工单进行汇总管理。将工单数据进行归类,将故障分类进行查看、统计、分析等等。2.设备状态:工单可通过用户上报设备状态数据进行查看,维修工程师在维修设备前,可以进行设备信息查询。3.设备状态:设备运行......
  • 在线客服系统PHP源码免费开源 (搭建教程+全新UI)
    安装环境宝塔面板  php>8.0 mysql5.7安装搭建1.建站点上传程序2.建数据库 导入数据3.网站目录/public伪静态设置4.修改数据库配置信息5.修改config.js里的配置信息6.启动wokerman命令更详细的搭建教程请下载源码根目录下 安装教程.docx产品亮点:自动回复......
  • 24年最新最详细Python安装教程,附安装包!感觉收藏!
      Python由荷兰国家数学与计算机科学研究中心的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。 Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,......
  • 【ROS教程】ROS常用API讲解
    @目录1.节点初始化2.话题通信2.1创建发布者对象2.2消息发布2.3创建订阅者对象3.服务通信3.1创建服务对象3.2创建客户对象3.3客户发送请求3.4客户对象等待服务4.回旋函数4.1spin4.2spinOnce5.时间5.1时刻5.1.1获取当前时刻5.1.2设置时刻5.2时间间隔5.2.1设置时间间......
  • Adobe InCopy(IC)文字处理软件win/mac软件下载安装 系统要求和使用教程
    目录一、AdobeIC软件介绍1.1软件概述1.2软件特点1.3适用范围二、系统要求2.1Windows系统要求2.2macOS系统要求三、安装步骤3.1Windows系统安装步骤3.2macOS系统安装步骤四、使用教程4.1基本操作4.2高级功能4.3注意事项一、AdobeIC软件介绍1.1软......
  • c++教程之三大结构
    C++顺序结构教程在编程的世界里,顺序结构是构建所有程序的基础。无论是简单的脚本还是复杂的应用程序,它们都是由一系列按照特定顺序执行的指令组成的。C++,作为一种高效、灵活的编程语言,同样遵循这一原则。本教程将深入介绍C++中的顺序结构,包括变量与数据类型、运算符与表达式、......
  • 在Windows上搭建自己的Git服务器的图文教程
    一、简介以前,在别家的公司,一般早就把源代码管理工具搭建好了,很少有机会自己搭建一套。最近,公司也许要把现在不少的源码进行管理,于是我打算自己搭建源代码管理服务器。说起源代码管理,当然有很多中解决方案,我个人偏向搭建一个Git服务器。毕竟这个自己用的比较多,也熟悉。而且,现在......