对cv2矩形的openVINO模型输出进行故障排除



我正在试验这个用于人员检测的openVINO Zoo模型。我所关注的是我是否正确地处理了模型体系结构。这是模型输入和模型输出的信息。(所有3个链接都指向同一页面,但位置不同(

import cv2
import numpy as np
import matplotlib.pyplot as plt
from openvino.runtime import Core
MODEL = "person-detection-asl-0001"
PRECISION = "FP16"
MODEL_PATH = "./person-detection-asl-0001/"
CHAR = "/"
FILE_TYPE = ".xml"
FULL_MODEL_STR = MODEL_PATH + CHAR + PRECISION + CHAR + MODEL + FILE_TYPE
print("testing model")
print(FULL_MODEL_STR)
ie_core = Core()

def model_init(model_path):
model = ie_core.read_model(model=model_path)
compiled_model = ie_core.compile_model(model=model, device_name="CPU")
input_keys = compiled_model.input(0)
output_keys = compiled_model.output(0)
return input_keys, output_keys, compiled_model

input_key, output_keys, compiled_model = model_init(FULL_MODEL_STR)
print("COMPILED MODEL: ", compiled_model)

# Get input size - Recognition.
height, width = list(input_key.shape)[2:]
print("MODEL DIMENSIONS: ", (height, width))
image = cv2.imread("./ben_sarah.JPG")
# cv2.imshow("image",image)

image_mod = cv2.resize(image, (width, height))
image_mod = image_mod.transpose((2, 0, 1))
image_mod = image_mod.reshape(1, 3, height, width)

# Run inference.
boxes = compiled_model([image_mod])[compiled_model.output('boxes')]
print(f"{MODEL} BOXES.SHAPE: {boxes.shape}")
def postprocess(result, image):
aligns = image.shape
detections = result.reshape(-1, 5)
for i, detection in enumerate(detections):
xmin, ymin, xmax, ymax, confidence = detection
if confidence > 0.2:
xmin = int(max((xmin * image.shape[1]), 10))
ymin = int(max((ymin * image.shape[0]), 10))
xmax = int(min((xmax * image.shape[1]), image.shape[1] - 10))
ymax = int(min((ymax * image.shape[0]), image.shape[0] - 10))
conf = round(confidence, 2)
print(f"conf: {conf:.2f}")
print((xmin, ymin),(xmax, ymax))
# For bounding box
cv2.rectangle(image, (xmin, ymin),
(xmax, ymax), (255, 255, 255), 5)
# For the text background
# Finds space required
(w, h), _ = cv2.getTextSize(
f"{conf:.2f}", cv2.FONT_HERSHEY_SIMPLEX, 1.7, 1)
# Prints the text.
cv2.rectangle(image, (xmin, ymin + h + 5),
(xmin + w + 5, ymin), (255, 255, 255), -1)
cv2.putText(image, f"{conf:.2f}", (xmin, ymin + h),
cv2.FONT_HERSHEY_SIMPLEX, 1.7, (0, 0, 0), 3)
return image
final = postprocess(boxes, image)
cv2.imwrite(f"./outputs/{PRECISION}-{MODEL}.png", final)
cv2.imshow("final", final)

代码运行。。。但是模型输出用于在检测到的人周围创建框,坐标是不正确的。

例如,在postprocess函数中,我认为我在做错误的事情:

print(f"conf: {conf:.2f}")
print((xmin, ymin),(xmax, ymax))

退货:

conf: 0.50
(29012, 127753) (1270, 950)

如果要表示的数字(29012, 127753)(xmin,ymin(不正确,我认为这在整个图像的坐标之外。原来的image.shape(960, 1280, 3)

我想你想要从图像边缘开始至少10个像素的边距来绘制边界框,所以你需要:

xmin = int(max(xmin, 10))
ymin = int(max(ymin, 10))
xmax = int(min(xmax, image.shape[1] - 10))
ymax = int(min(ymax, image.shape[0] - 10))

如果xmin、ymin、xmax、ymax已经是整数,则不需要int((包装器。

最新更新