基于openvino的图像分类
"""
Created on Tue Nov 16 01:38:44 2021
@MysteriousKnight: 23608
@Email: xingchenziyi@163.com
"""
from openvino.inference_engine import IECore
import numpy as np
import time
import cv2 as cv
ie = IECore()
devices = ie.available_devices
print(devices[:])
with open("imagenet_classes.txt") as f:
labels = [line.strip() for line in f.readlines()]
model_xml = "resnet18.xml"
model_bin = "resnet18.bin"
net = ie.read_network(model=model_xml, weights=model_bin)
input_blob = next(iter(net.inputs))
output_blob = next(iter(net.outputs))
n,c,h,w = net.inputs[input_blob].shape
src = cv.imread("C:/Users/23608/Desktop/xxx.jpg")
img = cv.resize(src, (w,h))
image = np.float32(img) / 255.0
image = image.transpose(2,0,1)
exec_net = ie.load_network(network=net, device_name="CPU")
res = exec_net.infer(inputs={input_blob:np.array([image])})
res = res[output_blob]
label_index = np.argmax(res,1)[0]
cv.putText(src, labels[label_index], (0,50), cv.FONT_HERSHEY_SIMPLEX, 0.75, (255, 0, 0), 2)
cv.imshow("winname1", src)
cv.waitKey(0)
效果