Visdrone数据集数据前后处理操作系列Visdrone预处理
本篇博客包含:
1. 切割数据集
2. 将数据做成Yolo格式
3. 图像⼆值化
4. 绘制评估曲线
⼀、把Visdrone数据集进⾏切割,⽣成600*600⼤⼩的图⽚和xml⽂件train_crop_visdrone.py
import os
import scipy.misc as misc
借书卡制作from xml.dom.minidom import Document
import numpy as np
import copy, cv2
def save_to_txt(save_path, objects_axis):
f =open(save_path,'w')
objects_list = list()
objects_ =[','.join(map(str, i))+'\n'for i in objects_list]
objects_[-1]= objects_[-1][:-1]
# import pdb
# pdb.t_trace()
f.writelines(objects_)
f.clo()
def save_to_xml(save_path, im_width, im_height, objects_axis, label_name, name, hbb=True):
im_depth =0
object_num =len(objects_axis)
doc = Document()
annotation = ateElement('annotation')
doc.appendChild(annotation)
folder = ateElement('folder')
folder_name = ateTextNode('Visdrone')
folder.appendChild(folder_name)
annotation.appendChild(folder)
filename = ateElement('filename')
filename_name = ateTextNode(name)
filename.appendChild(filename_name)
annotation.appendChild(filename)
source = ateElement('source')
annotation.appendChild(source)
databa = ateElement('databa')
databa.ateTextNode('The Visdrone Databa'))
source.appendChild(databa)
动作的词语annotation_s = ateElement('annotation')
annotation_s.ateTextNode('Visdrone'))
source.appendChild(annotation_s)
image = ateElement('image')
image.ateTextNode('flickr'))
source.appendChild(image)
flickrid = ateElement('flickrid')
flickrid.ateTextNode('322409915'))
source.appendChild(flickrid)
owner = ateElement('owner')
annotation.appendChild(owner)
flickrid_o = ateElement('flickrid')
flickrid_o.ateTextNode('knautia'))
owner.appendChild(flickrid_o)
name_o = ateElement('name')
name_o.ateTextNode('yang'))
owner.appendChild(name_o)
size = ateElement('size')
annotation.appendChild(size)
width = ateElement('width')
width.ateTextNode(str(im_width)))
height = ateElement('height')
height.ateTextNode(str(im_height)))
depth = ateElement('depth')
depth.ateTextNode(str(im_depth)))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
gmented = ateElement('gmented')
gmented.ateTextNode('0'))
annotation.appendChild(gmented)
for i in range(object_num):
objects = ateElement('object')
annotation.appendChild(objects)
object_name = ateElement('name')
object_name.ateTextNode(label_name[int(objects_axis[i][5])])) objects.appendChild(object_name)
po = ateElement('po')
po.ateTextNode('Unspecified'))
objects.appendChild(po)
truncated = ateElement('truncated')
truncated.ateTextNode('1'))
objects.appendChild(truncated)
difficult = ateElement('difficult')
difficult.ateTextNode('0'))
objects.appendChild(difficult)
bndbox = ateElement('bndbox')
objects.appendChild(bndbox)
if hbb:
x0 = ateElement('xmin')
x0.ateTextNode(str((objects_axis[i][0]))))
bndbox.appendChild(x0)
y0 = ateElement('ymin')
y0.ateTextNode(str((objects_axis[i][1]))))
bndbox.appendChild(y0)
x1 = ateElement('xmax')
x1.ateTextNode(str((objects_axis[i][2]))))
bndbox.appendChild(x1)
y1 = ateElement('ymax')
y1.ateTextNode(str((objects_axis[i][3]))))
bndbox.appendChild(y1)
el:
el:
x0 = ateElement('x0')
x0.ateTextNode(str((objects_axis[i][0]))))
bndbox.appendChild(x0)
y0 = ateElement('y0')
y0.ateTextNode(str((objects_axis[i][1]))))
bndbox.appendChild(y0)
x1 = ateElement('x1')
x1.ateTextNode(str((objects_axis[i][2]))))
bndbox.appendChild(x1)
y1 = ateElement('y1')
y1.ateTextNode(str((objects_axis[i][3]))))
山东考试招生院bndbox.appendChild(y1)
x2 = ateElement('x2')
x2.ateTextNode(str((objects_axis[i][4]))))
bndbox.appendChild(x2)
y2 = ateElement('y2')
y2.ateTextNode(str((objects_axis[i][5]))))
bndbox.appendChild(y2)
vc的作用与功效x3 = ateElement('x3')
x3.ateTextNode(str((objects_axis[i][6]))))
bndbox.appendChild(x3)
y3 = ateElement('y3')
y3.ateTextNode(str((objects_axis[i][7]))))
bndbox.appendChild(y3)
f =open(save_path,'w')
f.prettyxml(indent =''))
f.clo()
class_list =['ignored regions','pedestrian','people','bicycle','car','van','truck','tricycle','awning-tricycle','bus','motor','others']
def format_label(txt_list):
format_data =[]
for i in txt_list[0:]:
format_data.append(
[int(xy)for xy in i.split(',')[:8]]
# {'x0': int(i.split(' ')[0]),
# 'x1': int(i.split(' ')[2]),
# 'x2': int(i.split(' ')[4]),
# 'x3': int(i.split(' ')[6]),
# 'y1': int(i.split(' ')[1]),
# 'y2': int(i.split(' ')[3]),
# 'y3': int(i.split(' ')[5]),
# 'y4': int(i.split(' ')[7]),
# 'class': class_list.index(i.split(' ')[8]) if i.split(' ')[8] in class_list el 0,
# 'difficulty': int(i.split(' ')[9])}
)
# if i.split(',')[8] not in class_list :
# print ('warning found a new label :', i.split(',')[8])
# exit()
return np.array(format_data)
def clip_image(file_idx, image, boxes_all, width, height, stride_w, stride_h):
if len(boxes_all)>0:
shape = image.shape
for start_h in range(0, shape[0], stride_h):
for start_w in range(0, shape[1], stride_w):
boxes = copy.deepcopy(boxes_all)
淘宝好评模版box = np.zeros_like(boxes_all)
start_h_new = start_h
start_h_new = start_h
start_w_new = start_w
if start_h + height > shape[0]:
start_h_new = shape[0]- height
if start_w + width > shape[1]:
start_w_new = shape[1]- width
top_left_row =max(start_h_new,0)
top_left_col =max(start_w_new,0)
bottom_right_row =min(start_h + height, shape[0])
bottom_right_col =min(start_w + width, shape[1])
subImage = image[top_left_row:bottom_right_row, top_left_col: bottom_right_col]
box[:,0]= boxes[:,0]- top_left_col
box[:,2]= boxes[:,0]+ boxes[:,2]- top_left_col
box[:,4]= boxes[:,4]
box[:,0]=[max(i,0)for i in box[:,0]]# 限制框的⼤⼩
# box[:, 6] = boxes[:, 6] - top_left_col
box[:,1]= boxes[:,1]- top_left_row
box[:,3]= boxes[:,1]+ boxes[:,3]- top_left_row
box[:,5]= boxes[:,5]
box[:,1]=[max(i,0)for i in box[:,1]]
# box[:, 7] = boxes[:, 7] - top_left_row
# box[:, 8] = boxes[:, 8]
center_y =0.5*(box[:,1]+ box[:,3])
center_x =0.5*(box[:,0]+ box[:,2])
# print('center_y', center_y)
# print('center_x', center_x)
# print ('boxes', boxes)
# print ('boxes_all', boxes_all)
# print ('top_left_col', top_left_col, 'top_left_row', top_left_row)
cond1 = np.interct1d(np.where(center_y[:]>=0)[0], np.where(center_x[:]>=0)[0])
cond2 = np.interct1d(np.where(center_y[:]<=(bottom_right_row - top_left_row))[0],
np.where(center_x[:]<=(bottom_right_col - top_left_col))[0])
idx = np.interct1d(cond1, cond2)
# idx = np.where(center_y[:]>=0 and center_x[:]>=0 and center_y[:] <= (bottom_right_row - top_left_row) and center_x[:] <= (bottom_right_col - t op_left_col))[0]
# save_path, im_width, im_height, objects_axis, label_name
if len(idx)>0:
name="%s_%04d_%04d.jpg"%(file_idx, top_left_row, top_left_col)
print(name)
xml = os.path.join(save_dir,'annotations_600_xml',"%s_%04d_%l"%(file_idx, top_left_row, top_left_col))
save_to_xml(xml, subImage.shape[1], subImage.shape[0], box[idx,:], class_list,str(name))
# save_to_txt(xml, box[idx, :])
# print ('save xml : ', xml)
if subImage.shape[0]>5and subImage.shape[1]>5:
img = os.path.join(save_dir,'images_600',"%s_%04d_%04d.jpg"%(file_idx, top_left_row, top_left_col))
cv2.imwrite(img, subImage)
print('class_list',len(class_list))
raw_data ='D:/datats/VisDrone/VisDrone2019-DET-val/'
raw_images_dir = os.path.join(raw_data,'images')
解忧杂货铺电影raw_label_dir = os.path.join(raw_data,'annotations')
save_dir ='D:/datats/VisDrone/VisDrone2019-DET-val/'
images =[i for i in os.listdir(raw_images_dir)if'jpg'in i]
labels =[i for i in os.listdir(raw_label_dir)if'txt'in i]
print('find image',len(images))
print('find label',len(labels))
min_length =1e10
max_length =1
img_h, img_w, stride_h, stride_w =600,600,450,450
for idx, img in enumerate(images):
# img = 'P1524.png'
img_data = misc.imread(os.path.join(raw_images_dir, img))
print(idx,'read image', img)
# if len(img_data.shape) == 2:
# img_data = img_data[:, :, np.newaxis]
# print ('find gray image')
txt_data =open(os.path.join(raw_label_dir, place('jpg','txt')),'r').readlines() # print (idx, len(format_label(txt_data)), img_data.shape)
# if max(img_data.shape[:2]) > max_length:
# max_length = max(img_data.shape[:2])
# if min(img_data.shape[:2]) < min_length:
# min_length = min(img_data.shape[:2])
# if idx % 50 ==0:
# print (idx, len(format_label(txt_data)), img_data.shape)
# print (idx, 'min_length', min_length, 'max_length', max_length)
box = format_label(txt_data)
# box = dele(box)
clip_image(img.strip('.jpg'), img_data, box, img_h, img_w, stride_h, stride_w)
# rm val/images/* && rm val/labeltxt/*
⼆、提取图⽚名字
extract_name.py
# P02 批量读取⽂件名(不带后缀)
import os
file_path ="D:/datats/VisDrone/VisDrone2019-DET-val/annotations_600/"
path_list = os.listdir(file_path)# os.listdir(file)会历遍⽂件夹内的⽂件并返回⼀个列表print(path_list)
path_name =[]# 把⽂件列表写⼊中
def saveList(pathName):
for file_name in pathName:
with open("name_","a")as f:
f.write(file_name.split(".")[0]+"\n")
def dirList(path_list):
小麦简笔画for i in range(0,len(path_list)):
path = os.path.join(file_path, path_list[i])
if os.path.isdir(path):
可怜的汤姆saveList(os.listdir(path))
dirList(path_list)
saveList(path_list)
三、将xml标注⽂件转成yolo需要的标注格式
xml2yolo.py
# 缺陷坐标xml转txt