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快速落地部署教程的更多相关文章

  1. Twitter开源的Heron快速安装部署教程

    什么是Heron? Twitter使用Storm实时分析海量数据已经有好几年了,并在2011年将其开源.该项目稍后开始在Apache基金会孵化,并在2015年秋天成为顶级项目.Storm以季度为发布周 ...

  2. Hexo快速部署教程

    一直有建立博客的需要,使用过Wordpress动态博客,一直访问速度比较慢,刚开始以为是空间域名的解析的问题,尝试使用Hexo静态博客,部署后感觉速度正常很多,特意发文快速部署教程 准备 本文是在wi ...

  3. 【gitlab】gitlab快速部署教程

    gitlab快速部署教程 部署环境 Ubuntu 16.04(亲测可用) 开始部署 安装依赖 sudo apt-get install curl openssh-server ca-certifica ...

  4. 使用ASP.NET MVC、Rabbit WeixinSDK和Azure快速开发部署微信后台

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:公众号后台系统和数据都基本准备妥当了,可以来分享下我是如何开发本微信公众号的后台系统了 ...

  5. 写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程

    学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...

  6. Kubernetes 1.3.1 快速单机部署

    Kubernetes发展到今天, 在官网上已经有非常多的成熟部署方案, 但是由于墙的原因, 最简单的MiniKube都无法进行, 参考了以下两篇文章后, 终于安装成功. k8s-1.13版本测试环境搭 ...

  7. Eclipse中安装JRebel热部署教程

    Eclipse中安装JRebel热部署教程 前言        Eclipse安装JRebel插件可快速实现热部署,节省了大量重启时间,提高开发效率. 本文只介绍Eclipse安装JRebel插件版本 ...

  8. Flask+uwsgi+Nginx+Ubuntu部署教程

    学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...

  9. GitLab + Jenkins + Harbor 工具链快速落地指南

    目录 一.今天想干啥? 二.今天干点啥? 三.今天怎么干? 3.1.常规打法 3.2.不走寻常路 四.开干吧! 4.1.工具链部署 4.2.网络配置 4.3.验证工具链部署结果 4.3.1.GitLa ...

  10. CRL快速开发框架系列教程十三(嵌套查询)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

随机推荐

  1. 总结:软件开发的3个方向 与 嵌入式Linux学习路线(驱动方向)

    --- title: 嵌入式Linux学习路线图(驱动方向) date: 2020-05-09 07:17:58 categories: tags: - embeded - summary - arm ...

  2. TI AM64x工业核心板硬件说明书(双核ARM Cortex-A53 + 单/四核Cortex-R5F + 单核Cortex-M4F,主频1GHz)

    1          硬件资源 创龙科技SOM-TL64x是一款基于TI Sitara系列AM64x双核ARM Cortex-A53 + 单/四核Cortex-R5F + 单核Cortex-M4F设计 ...

  3. ubuntu16.04 安装 eclips c/c++

    前言 最近需要在ubuntu16上使用eclips编译c,尝试了apt安装和官网最新包安装甚至应用商店安装,效果都不太理想,现在把我的安装方法记录一下. 正文 !!!前提,已经自己配置好了java8的 ...

  4. 2023年台州市初赛Misc

    2023年台州市初赛Misc 这是神马 冰蝎流量,找到key <?php @error_reporting(0); session_start(); $key="144a6b22963 ...

  5. Centos7离线安装gcc4.8

    有时候CentOS工作在无互联网的环境下,需要在离线环境下安装一些组件,这次实现的是模拟在离线环境下安装gcc4.8. 第一步: 先去http://mirrors.aliyun.com/centos/ ...

  6. mybatis log4j打印sql语句

    依赖 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</a ...

  7. 如何在有数BI中实现千人千面的数据推送?

    问题背景 前几天有个项目管理的同学来咨询我一个问题,该项目有一个项目进度信息表,表中有项目名称,项目阶段,项目状态,项目任务等字段,在实际工作中想要实现如下场景: 当项目名称为A时,且项目阶段是需求阶 ...

  8. 机器学习策略篇:详解处理数据不匹配问题(Addressing data mismatch)

    处理数据不匹配问题 如果您的训练集来自和开发测试集不同的分布,如果错误分析显示有一个数据不匹配的问题该怎么办?这个问题没有完全系统的解决方案,但可以看看一些可以尝试的事情.如果发现有严重的数据不匹配问 ...

  9. SQL_left join 和from 两个表的区别

    一个是普通的联接,结果中的记录在两个表中都有.一个是左外联接,结果中的记录在A表中存在,B表中不一定有.相当于a表为主体表,b为辅助表. 例子: mysql> select * from a;+ ...

  10. [oeasy]python0104_指示灯_显示_LED_辉光管_霓虹灯

    编码进化 回忆上次内容 x86.arm.riscv等基础架构 都是二进制的 包括各种数据.指令   但是我们接触到的东西 都是屏幕显示出来的字符   计算机 显示出来的 一个个具体的字型   ​   ...