OpenCV实现预处理 vs OpenVINO预处理API函数实现预处理
OpenVINO2022.1
注意:实测Windows上的OpenVINO2022.1向前兼容性不好,例如,不⽀持IECore
前向兼容不好
YOLOv5 推理范例程序
下载源代码和范例模型
from pyexpat import model
import cv2
import numpy as np
import time
import yaml
#from openvino.inference_engine import IECore # the version of openvino <= 2021.4.2
from openvino.runtime import Core # the version of openvino >= 2022.1
# 载⼊COCO Label
with open('./coco.yaml','r', encoding='utf-8') as f:
result = yaml.ad(),Loader=yaml.FullLoader)
class_list = result['names']
# YOLOv5s输⼊尺⼨
INPUT_WIDTH = 640
INPUT_HEIGHT = 640
# ⽬标检测函数,返回检测结果
def detect(image, net):
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (INPUT_WIDTH, INPUT_HEIGHT), swapRB=True, crop=False) preds = net([blob])[next(iter(net.outputs))] # API version>=2022.1
#result = net.infer({"images": blob}) # API version<=2021.4.2
#preds = result["output"] # API version<=2021.4.2
return preds
# YOLOv5的后处理函数,解析模型的输出
rectangle函数opencvdef wrap_detection(input_image, output_data):
class_ids = []
confidences = []
boxes = []
#print(output_data.shape)
rows = output_data.shape[0]
image_width, image_height, _ = input_image.shape
x_factor = image_width / INPUT_WIDTH
y_factor = image_height / INPUT_HEIGHT
for r in range(rows):
row = output_data[r]
confidence = row[4]
if confidence >= 0.4:
classes_scores = row[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 = row[0].item(), row[1].item(), row[2].item(), row[3].item()
left = int((x - 0.5 * w) * x_factor)
left = int((x - 0.5 * w) * x_factor)
top = int((y - 0.5 * h) * y_factor)
width = int(w * x_factor)
height = int(h * y_factor)
box = np.array([left, top, width, height])
boxes.append(box)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
result_class_ids = []
result_confidences = []
result_boxes = []
for i in indexes:
result_confidences.append(confidences[i])
result_class_ids.append(class_ids[i])
result_boxes.append(boxes[i])
return result_class_ids, result_confidences, result_boxes
# 按照YOLOv5要求,先将图像长:宽 = 1:1,多余部分填充⿊边
def format_yolov5(frame):
row, col, _ = frame.shape
_max = max(col, row)
result = np.zeros((_max, _max, 3), np.uint8)
result[0:row, 0:col] = frame
return result
# 载⼊yolov5s onnx模型
model_path = "./"
# Read yolov5s onnx model with OpenVINO API
# ie = IECore() #Initialize IECore version<=2021.4.2
ie = Core() #Initialize Core version>=2022.1
'''#List all the available devices
devices = ie.available_devices
for device in devices:
device_name = ie.get_property(device_name=device, name="FULL_DEVICE_NAME")
print(f"{device}: {device_name}")
'''
# net = ie.load_network(network=model_path, device_name="AUTO") # API version<=2021.4.2 model_onnx = ie.read_model(model=model_path) # read model, API version>=2022.1
# print(model_onnx.inputs) #Check the input nodes of the model
# print(model_onnx.outputs) #Check the output nodes of the model
net = iepile_model(model=model_onnx, device_name="AUTO")
# 开启Webcam,并设置为1280x720
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# 调⾊板
colors = [(255, 255, 0), (0, 255, 0), (0, 255, 255), (255, 0, 0)]
# 开启检测循环
while True:
start = time.time()
_, frame = ad()
if frame is None:
print("End of stream")
break
# 将图像按最⼤边1:1放缩
inputImage = format_yolov5(frame)
# 执⾏推理计算
outs = detect(inputImage, net)
# 拆解推理结果
class_ids, confidences, boxes = wrap_detection(inputImage, outs[0])
# 显⽰检测框bbox
for (classid, confidence, box) in zip(class_ids, confidences, boxes):
color = colors[int(classid) % len(colors)]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论