参考:
(167条消息) Python如何优雅地可视化目标检测框_目标检测可视化_赵卓不凡的博客-CSDN博客(167条消息) Python 深度学习目标检测结果可视化_如何把深度学习预测值和真实值结果可视化_小李AI飞刀^_^的博客-CSDN博客(167条消息) Opencv基础画图函数——line、circle、rectangle、Rect、ellipse、polylines、putText函数的用法_polylines函数_拾牙慧者的博客-CSDN博客
第一个链接是用rectangle来实现单个图片的可视化,第二个链接里是用polylines来绘制文件夹里图片的可视化。结合了第二个链接里的读取方式和第一个链接里的可视化方法,得到了现在的代码。第三个链接讲的是opencv基础的画图函数。
结合二者的代码如下:
import cv2
import numpy as np
import os
class_name = ['ignore regions ','pedestrian', 'person', 'car', 'van', 'bus', 'truck', 'motor',
'bicycle', 'awning-tricycle', 'tricycle','other']
# 读取标签文件,返回一系列边界框的坐标点及类别信息
# 文件中一行代表一个边界框,每行由"x y w h score class truncation occlusion"组成
def read_label(label_file):
with open(label_file,'r') as f:
box_data = [] # 存储坐标点信息
category_data = [] # 存储标类别信息
for line in f.readlines():
label = line.split(',')
x1 = float(label[0])
y1 = float(label[1])
x2 = float(label[0])+float(label[2])
y2 = float(label[1])+float(label[3])
box = np.array([x1,y1,x2,y2],np.int32)
category = label[5]
category_box = np.array([category],np.int32)
box_data.append(box)
category_data.append(category_box)
return box_data,category_data
label_dir = '/data/ICCV/OBJECT DETECTION/VisDrone2019-DET-train/annotations/' # 存储标签的文件夹
image_dir = '/data/ICCV/OBJECT DETECTION/VisDrone2019-DET-train/images/'# 存储影像的文件夹
out_dir = '/data/ICCV/OBJECT DETECTION/VisDrone2019-DET-train/visual/' # 保存输出文件的文件夹
label_files = os.listdir(label_dir)
for label_file in label_files: # 遍历标签文件
label_path = label_dir + label_file # 标签文件路径
image_path = image_dir + label_file.replace('.txt','.jpg') # 影像文件路径(影像与标签文件名对应,替换后缀即可)
img = cv2.imread(image_path)
box_data,category_data = read_label(label_path) # 读取标签文件中的坐标点信息
for i in range(len(box_data)):
x1 = box_data[i][0]
y1 = box_data[i][1]
bbox = np.array(box_data[i],np.int32)
bbox_color = [(102,0,0),(255,0,0),(255,255,0),(0,255,255),(0,255,0),(0,0,255),
(153,51,255),(255,0,255),(96,96,96),(255,0,127),(153,153,255),(255,153,153)]
m=int(category_data[i][0])
color = bbox_color[m]
cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, thickness=2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,class_name[m], (x1, y1 - 7), font, 0.5, (6, 230, 230), 1)
label_show_file = out_dir +label_file.replace('.txt','.jpg')
cv2.imwrite(label_show_file, img)
可视化结果如下图: