6、编写 OpenVINO 应用程序(Python 版)

设置环境变量

添加一个名为 PYTHONPATH 的环境变量,并为该变量添加两个路径:
C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\python\python3 和 C:\Program Files (x86)\IntelSWTools\openvino_2019.3.334\python\python3.6
其中第一个路径是包含 OpenCV Python 模块的路径,第二个路径是包含 OpenVINO Python 模块的路径。设置好了该环境变量,我们才能正常导入相关的库。

例1:编写程序(使用 OpenCV 库)

通过 OpenCV dnn 模块来调用 OpenVINO,实现 AI 推理计算。

#导入opencv-openvino模块
import cv2 as cv
import time
#配置推断计算设备,IR文件路径,图片路径
DEVICE = cv.dnn.DNN_TARGET_CPU
model_xml = 'D:/tf_train/workspaces/cats_dogs/IR_model/cats_dogs_detector.xml'
model_bin = 'D:/tf_train/workspaces/cats_dogs/IR_model/cats_dogs_detector.bin'
image_file = 'D:/tf_train/workspaces/cats_dogs/images/test/3.jpg'

#读取IR模型文件
net = cv.dnn.readNet(model_xml, model_bin)

#指定AI推断执行硬件
net.setPreferableTarget(DEVICE)

#读取图片
img = cv.imread(image_file)

#将图片传入模型的输入张量
blob = cv.dnn.blobFromImage(img,ddepth=cv.CV_8U)
net.setInput(blob)

#执行推断计算
start = time.time()
out = net.forward()
end = time.time()
print("Infer Time:{}ms".format((end-start)*1000))

#处理推断计算结果
for detection in out.reshape(-1, 7):
    confidence = float(detection[2])
    xmin = int(detection[3] * img.shape[1])
    ymin = int(detection
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值