(Yolov4测试)将Yolov4模型输出的检测框映射到原图

(Yolov4测试)将Yolov4模型输出的检测框映射到原图

逻辑:

  1. 读取原图。根据配置文件、.data文件和权重文件导入模型。
  2. 将图片、训练好的网络模型、标签等一起送入detection函数进行检测输出检测图片和detections列表,列表包含类别、置信度、和框的坐标(x,y,w,h)等。
  3. 计算原图与检测图片的尺寸转换关系,这个转换关系同样适用于坐标转换。将检测图片上的框的坐标根据转换关系映射到原图上。
  4. 得到转换之后检测框的坐标之后,在原图上画框。
  5. 进行保存。
import os
import cv2
import darknet

config_file = './train/data/yolov4.cfg'
data_file = "./train/data/train.data"
weights = "./backup/yolov4_last.weights"

def image_detection(image_path, network, class_names, class_colors, thresh):
    # Darknet doesn't accept numpy images.
    # Create one with image we reuse for each detect
    width = darknet.network_width(network)
    height = darknet.network_height(network)
    darknet_image = darknet.make_image(width, height, 3)

    image = cv2.imread(image_path)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(image_rgb, (width, height),
                               interpolation=cv2.INTER_LINEAR)

    darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
    detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
    darknet.free_image(darknet_image)
    image = darknet.draw_boxes(detections, image_resized, class_colors)
    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections


network, class_names, class_colors = darknet.load_network(config_file,
                                                          data_file,
                                                          weights,
                                                          batch_size=1)

def bbox2points(bbox):
    x, y, w, h = bbox
    xmin = int(round(x - (w / 2)))
    xmax = int(round(x + (w / 2)))
    ymin = int(round(y - (h / 2)))
    ymax = int(round(y + (h / 2)))
    return xmin, ymin, xmax, ymax

colors = {'truck': (0, 0, 255), 'car': (0, 255, 0), 'people': (255, 0, 0)}
save_path = "save"
images_path = "test_demo1"
img_names = os.listdir(images_path)
for img_name in img_names:
    img = os.path.join(images_path, img_name)
    image, detections = image_detection(img, network, class_names, class_colors, thresh=0.25)

    im = cv2.imread(img)
    w = im.shape[1]
    h = im.shape[0]

    for label, confidence, bbox in detections:
        xmin, ymin, xmax, ymax = bbox2points(bbox)
        xmin = int(float((xmin * w) / 608))
        xmax = int(float((xmax * w) / 608))
        ymin = int(float((ymin * h) / 608))
        ymax = int(float((ymax * h) / 608))

        cv2.rectangle(im, (xmin, ymax), (xmax, ymin), colors[label], 1)
        cv2.putText(im, "{} [{:.2f}]".format(label, float(confidence)),
                    (xmin, ymax - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    colors[label], 2)

    cv2.imwrite(os.path.join(save_path, img_name + '__.jpg'), im)
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值