批量可视化目标检测标注框——YOLO格式数据集


import cv2
import os

img_dir = 'D:/images/'
label_dir = 'D:/labels/'
save_dir = 'D:/save/' # 事先新建一个文件夹,用来存放标注好的图片

lable_file = os.listdir(label_dir)
img_file = os.listdir(img_dir)

for file in lable_file:

    file_dir = os.path.join(label_dir, file)

    with open(file_dir, 'r') as f:
        print(os.path.join(img_dir, file.split('.')[0]+'.jpg'))
        image_src = cv2.imread(os.path.join(img_dir, file.split('.')[0]+'.jpg'))
        image_row = image_src.shape[0]
        image_col = image_src.shape[1]

        for line in f.readlines():
            x_ = float(line.split(' ')[1])
            y_ = float(line.split(' ')[2])
            w_ = float(line.split(' ')[3])
            h_ = float(line.split(' ')[4])
              
            w = image_col
            h = image_row

            x1 = w * x_ - 0.5 * w * w_
            x2 = w * x_ + 0.5 * w * w_
            y1 = h * y_ - 0.5 * h * h_
            y2 = h * y_ + 0.5 * h * h_

            draw = cv2.rectangle(image_src,(int(x1),int(y1)),(int(x2),int(y2)),[0,255,0],2)
        
        cv2.imwrite(os.path.join(save_dir, file.split('.')[0]+'.jpg'), draw)

你可能感兴趣的:(机器学习/深度学习,计算机视觉,图像处理,人工智能,目标检测)