YOLO v4 图片批量测试

  • Post author:
  • Post category:其他


如果新拿到一批数据,如何利用现有的yolov4 训练好的模型进行批量测试。



1.生成测试图片文档

在如下程序中,将

imgs_path

更换为自己的文件路径,下边程序将生成一个测试图片路径的txt文档。

import os
imgs_path = 'images'
txt_path = 'batch_test.txt'
img_list = [os.path.join(imgs_path, i) for i in os.listdir(imgs_path)]
save_txt = '\n'.join(img_list)
file_write_obj = open(txt_path, 'a')
file_write_obj.write(save_txt)
file_write_obj.close()



2.检测图像

下边就是利用yolo图像测试进行检测图像,其中将自己生成好的

batch_test.txt

一行行作为输入,送入网络中进行检测,同时将生成的log保存在

text_result.txt

中。

./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights -ext_output -dont_show -out result.json < batch_test.txt >> text_result.txt



3.利用json画图片

import json
import cv2


def load_json(path):
    with open(path, 'r') as f:
        json_dict = json.load(f)
    return json_dict


results_json = '../result.json'

json_dict = load_json(results_json)
for i in json_dict:
    print(i)
    image_path = i['filename']
    img = cv2.imread(image_path)
    [h, w, c] = img.shape
    objects_info = i['objects']
    for j in objects_info:
        class_id = j['class_id']
        if class_id == 0:
            color = (255, 0, 0)
        else:
            color = (255, 0, 255)
        box_info = j['relative_coordinates']
        center_x = box_info['center_x']
        center_y = box_info['center_y']
        width = box_info['width']
        height = box_info['height']
        confidence = j['confidence']
        x_min = int((center_x - width / 2) * w)
        y_min = int((center_y - height / 2) * h)
        x_max = int((center_x + width / 2) * w)
        y_max = int((center_y + height / 2) * h)
        img = cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color)
    cv2.imshow('img', img)
    cv2.waitKey(500)



版权声明:本文为qq_35451572原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。