最近让人标了些图片,想看一下他标的是不是符合我的要求,但txt文件毕竟还是不好看,所以写了一个画框的函数, 不过我的目标检测只有一个类,不能区分类别

def yolo_txt2cv_points(file_path,save_path):#前者是需要的文件,文件包含两个文件夹images和labels 后者是需要保存在哪
images_path = os.path.join(file_path,'images')
labels_path = os.path.join(file_path,'labels')
print(len(os.listdir(images_path)))
for file in os.listdir(images_path):
image_path = os.path.join(images_path,file)
image = cv2.imread(image_path)
file_name = file.split('.')[0]
label_file = file_name + '.txt'
label_path = os.path.join(labels_path, label_file)
width = image.shape[1]
height = image.shape[0]
with open(label_path, 'r') as f:
rects = f.readlines()
for rect in rects:
a = float(rect[2:10])
b = float(rect[11:19])
c = float(rect[20:28])
d = float(rect[29:37])
box_width = c*width
box_height = d*height
first_point = (int(a*width - box_width/2), int(b*height - box_height/2))
last_point = (int(a*width + box_width/2), int(b*height + box_height/2))
# print(first_point,last_point)
cv2.rectangle(image, first_point, last_point, (0, 255, 0), 2)
save = os.path.join(save_path,file)
# print(save)
cv2.imwrite(save,image)
# print(a,b,c,d)
函数效果如下:

