VIL100数据集(IMG:image(尺寸((1080 * 1920),(720 * 1280),(480 * 960),(478 * 960),(448 * 960),(378 * 672),(474 * 960)),GT:json)
目的:处理成统一尺寸,便于model train.
(基于车道线的消失点裁剪后resize成800 * 320,GT格式包括mask,点格式的line_txt)
import os
import glob
import shutil
import cv2
import json
import numpy as np
path="/VIL100"
file_path="/VIL100/data/train.txt"
img_path="/merge/VIL100/image"
mask_path="/merge/VIL100/mask"
text_path = '/merge/VIL100/line_txt/'
n_0 =0
new_w = 800
new_h = 320
with open(file_path,'r') as f:
for line in f:
image_name_old = line.split()[0].split('/')[-1]
ori_image = cv2.imread(path+line.split()[0])
mask_name_old = line.split()[1].split('/')[-1]
# read json
json_name = path + line.split()[0].replace('JPEGImages', 'Json')+'.json'
mask_json = json.load((open(json_name, encoding='utf-8')))
anno = mask_json['annotations']['lane']
points_list = []
for anno_line in anno:
points_list.append(np.array(anno_line['points']))
if len(anno) == 0:
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@kong")
print(points_list)
continue
mask = cv2.imread(path+line.split()[1],0)
Num= np.amax(mask[:,:],axis=0)
dice=np.argmax(mask[:,:],axis=0)
used_dice=[]
for i in range(0,len(dice)):
if dice[i]!=0:
used_dice.append(dice[i])
if len(used_dice)==0:
used_dice.append(0)
crop_y=np.amin(used_dice)
# print(crop_y)
img = np.zeros((mask.shape[0]-crop_y, mask.shape[1], 3), np.uint8)
img[:mask.shape[0]-crop_y, :, :] = ori_image[crop_y:, ...]
mas = np.zeros((mask.shape[0] - crop_y, mask.shape[1], 3), np.uint8)
mas[:mask.shape[0] - crop_y, :, 0] = mask[crop_y:, :]
mas[:mask.shape[0] - crop_y, :, 1] = mask[crop_y:, :]
mas[:mask.shape[0] - crop_y, :, 2] = mask[crop_y:, :]
img_new = cv2.resize(img, (new_w, new_h), interpolation=1)
mas_new = cv2.resize(mas, (new_w, new_h), interpolation=1)
draw_img = img_new.copy()
mas_new =np.where(mas_new>0 ,255,0)
len_num = len(str(n_0))
split_0 = 8 - len_num
Public_name = "0000000000000000000000000000000000000000000000000000000000000000"
split_name = Public_name[0:split_0]
#Visualization crop and resize for the points of json
# for idx, pts in enumerate(points_list):
# for i in range(1, len(pts)):
# x1 = int(pts[i - 1][0]/ori_image.shape[1] * new_w)
# y1 = int((pts[i - 1][1] - crop_y)/(ori_image.shape[0]-crop_y) * new_h)
# x2 = int(pts[i][0]/ori_image.shape[1] * new_w)
# y2 = int((pts[i][1] - crop_y)/(ori_image.shape[0]-crop_y) * new_h)
# cv2.line(draw_img, (x1,y1),
# (x2,y2), (0,125,45), thickness=4)
# plt.figure(dpi=720)
# plt.imshow(draw_img, cmap='BrBG_r')
# save files
image_name = 'VIL100' + "_" + split_name + str(n_0)+".png"
mask_name = 'VIL100' + "_" + split_name + str(n_0)+".png"
txt_name = 'VIL100' + "_" + split_name + str(n_0)+".lines.txt"
cv2.imwrite(os.path.join(img_path, image_name), img_new)
cv2.imwrite(os.path.join(mask_path, mask_name), mas_new)
save_txt_path = os.path.join(text_path, txt_name)
with open(save_txt_path, 'w') as fp:
lines = ''
for pts in(points_list):
line = ''
for i in range(len(pts)):
x = pts[i][0]/ori_image.shape[1] * new_w
y = (pts[i][1] - crop_y)/(ori_image.shape[0]-crop_y) * new_h
line += f"{x:.2f} {y:.2f} "
line+='\n'
lines+=line
#print(lines)
fp.writelines(lines)
n_0 = n_0 + 1