opencv cv2矩形openVINO模型输出故障排除

6ioyuze2  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(171)

我正在试验这个openVINO动物园模型的人物检测。我所坚持的是我是否正确地处理了模型架构。这是模型输入和模型输出的信息。(所有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)是不正确的,我认为这超出了整个图像的坐标。

xoefb8l8

xoefb8l81#

我猜您需要从图像边缘至少留出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() Package 器。

相关问题