目录
自定义自己的Tusimple格式数据集
一、使用工具
二、使用步骤
1.安装labelme
2.使用labelme标记数据
3.批量使用labelme将json文件转换成可视化的文件
4.将img.png和label.png转换成标准形式
labelme
参考大佬的文章http://yearing1017.cn/2020/05/30/labelme%E7%9A%84%E5%AE%89%E8%A3%85%E5%8F%8A%E4%BD%BF%E7%94%A8/
首先如果是视频数据可以先进行图片的采样,我这里提供一个脚本:
import os
import cv2
videos_src_path = 'videos'#视频文件夹路径
videos_save_path = 'images'#要输出的图片路径
videos = os.listdir(videos_src_path)
videos.sort(key=lambda x:int(x[5:-4]))
i = 1
for each_video in videos:
if not os.path.exists(videos_save_path ):
os.mkdir(videos_save_path )
each_video_save_full_path = videos_save_path+'/'+str(i)+'_'
each_video_full_path = os.path.join(videos_src_path,each_video)
cap = cv2.VideoCapture(each_video_full_path)
frame_count = 1
frame_clip = 200 #采样的间隔
success = True
while(success):
success,frame = cap.read()
if success==True:
if frame_count % frame_clip == 1:
if frame.shape[1]!=1280:
frame = cv2.resize(frame, (1280,720))
cv2.imwrite(each_video_save_full_path + "frame%d.jpg" % frame_count,frame)
frame_count = frame_count + 1
#success = False
i = i + 1
cap.release()
使用折线标记,画线,并为每条线赋不同的标签值,注意标记对应的颜色,后面会用到。
示例如下
这里使用python脚本实现
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
if __name__ == '__main__':
for scan_dir in ['testimagelabel']:#这里写自己的图片的路径
json_data = scan_dir
for name in os.listdir(json_data):
file_path = os.path.join(json_data, name)
os.system(str("labelme_json_to_dataset " + file_path))
print("success json to dataset: ", file_path)
效果如下图所示
内部的细节为:
其中我们主要用到的是img.png和label.png.
import cv2
from skimage import measure, color
from skimage.measure import regionprops
import numpy as np
import os
import copy
def replace_color(img, src_clr, dst_clr):
''' 通过矩阵操作颜色替换程序
@param img: 图像矩阵
@param src_clr: 需要替换的颜色(r,g,b)
@param dst_clr: 目标颜色 (r,g,b)
@return 替换后的图像矩阵
'''
img_arr = np.asarray(img, dtype=np.double)
r_img = img_arr[:,:,0].copy()
g_img = img_arr[:,:,1].copy()
b_img = img_arr[:,:,2].copy()
img = r_img * 256 * 256 + g_img * 256 + b_img
src_color = src_clr[0] * 256 * 256 + src_clr[1] * 256 + src_clr[2] #编码
r_img[img == src_color] = dst_clr[0]
g_img[img == src_color] = dst_clr[1]
b_img[img == src_color] = dst_clr[2]
dst_img = np.array([r_img, g_img, b_img], dtype=np.uint8)
dst_img = dst_img.transpose(1,2,0)
return dst_img
def skimageFilter(gray):#根据标签颜色,标准化标签如:
binary_warped = copy.copy(gray)
#binary_warped[binary_warped > 0.1] = 255
binary_warped = replace_color(binary_warped,(128,0,0),(1,1,1))#红色是1
binary_warped = replace_color(binary_warped,(0,128,0),(2,2,2))#绿色是2
binary_warped = replace_color(binary_warped,(0,128,128),(3,3,3))#黄色是3
binary_warped = replace_color(binary_warped,(0,0,128),(4,4,4))#蓝色是4
return binary_warped
def moveImageTodir(path,targetPath,name):
if os.path.isdir(path):
image_name = "gt_image/"+str(name)+".png"#原图
binary_name = "gt_binary_image/"+str(name)+".png"#标签图
train_rows = image_name + " " + binary_name + " " + "1 1 1 1" + "\n"#数据标注内容,可自定义
origin_img = cv2.imread(path+"/img.png")
origin_img = cv2.resize(origin_img, (1280,720))
cv2.imwrite(targetPath+"/"+image_name, origin_img)
img = cv2.imread(path+'/label.png')
img = cv2.resize(img, (1280,720))
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
binary_warped = skimageFilter(img)
binary_warped=cv2.cvtColor(binary_warped,cv2.COLOR_BGR2GRAY)
cv2.imwrite(targetPath+"/"+binary_name, binary_warped)
print("success create data name is : ", train_rows)
return train_rows
return None
if __name__ == "__main__":
#ep = "D:/File/Winscp/20.png"
#img = cv2.imread(ep)
#getimagelabel(img)
count = 1
with open("E:/datasets/testdataset/train.txt", 'w+') as file:
dir_name = "testimagelabel"#os.path.join("./images", images_dir + "/annotations")#上一个生成的文件目录,即保存转换好的标签的目录
for annotations_dir in os.listdir(dir_name):
json_dir = os.path.join(dir_name, annotations_dir)
if os.path.isdir(json_dir):
train_rows = moveImageTodir(json_dir, "E:/datasets/testdataset", str(count).zfill(4))
file.write(train_rows)
count += 1
ok 现在可以开始训练了