0、MP4文件截图(saveframe.py)
import cv2
import os
cap = cv2.VideoCapture("re.mp4") #xxx.mp4为文件名
a = os.getcwd()+'\\re' #获取路径
os.mkdir(a)
frames_total = cap.get(7)
print('frames_total:',frames_total)
frames_need = 250 #生成的图片数,需小于总数否则报错
print('frames_need:',frames_need)
n = 1
timeF = frames_total//frames_need
print('frames_gap:',timeF)
i = 0
while cap.isOpened():
ret, frame = cap.read()
if n%timeF == 0:
i += 1
print(i)
cv2.imwrite('./re/re{}.jpg'.format(i),frame) #生成后的放置路径
if i == frames_need:
break
n = n+1
cv2.waitKey(1)
cap.release()
1、Anaconda下载创建labelme环境并下载labelme库
conda create -name labelme python=3.6
conda activate labelme
pip install labelme
2、 使用与运行
进入labelme环境后直接输入labelme进入标注程序
file中设置自动保存
快捷键 ctrl+z撤销 a 上一张 d下一张
车道线使用create linestrip
3、Json格式文件批量转label图片
在anaconda\envs\labelme\Lib\site-packages\labelme\cli中找到json_to_dataset.py制作备份后修改源文件
import argparse
import base64
import json
import os
import os.path as osp
import imgviz
import PIL.Image
from labelme.logger import logger
from labelme import utils
def main():
logger.warning(
"This script is aimed to demonstrate how to convert the "
"JSON file to a single image dataset."
)
logger.warning(
"It won't handle multiple JSON files to generate a "
"real-use dataset."
)
parser = argparse.ArgumentParser()
parser.add_argument("json_file")
parser.add_argument("-o", "--out", default=None)
args = parser.parse_args()
json_file = args.json_file
filename = json_file[:-5]
if args.out is None:
out_dir = osp.basename(json_file).replace(".", "_")
print('_______',out_dir)
filename = out_dir[:-5]
out_dir = osp.join(osp.dirname(json_file), out_dir)
else:
out_dir = args.out
print('*****************',filename)
# if not osp.exists(out_dir):
# os.mkdir(out_dir)
data = json.load(open(json_file))
imageData = data.get("imageData")
if not imageData:
imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
with open(imagePath, "rb") as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode("utf-8")
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {"_background_": 0}
for shape in sorted(data["shapes"], key=lambda x: x["label"]):
label_name = shape["label"]
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
lbl, _ = utils.shapes_to_label(
img.shape, data["shapes"], label_name_to_value
)
label_names = [None] * (max(label_name_to_value.values()) + 1)
for name, value in label_name_to_value.items():
label_names[value] = name
lbl_viz = imgviz.label2rgb(
lbl, imgviz.asgray(img), label_names=label_names, loc="rb"
)
print('22222',out_dir,type(out_dir))
# out_dir = out_dir[:12]
#PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
utils.lblsave(osp.join( out_dir+".png"), lbl)
#PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))
# with open(osp.join(out_dir, "label_names.txt"), "w") as f:
# for lbl_name in label_names:
# f.write(lbl_name + "\n")
logger.info("Saved to: {}".format(out_dir))
if __name__ == "__main__":
main()
然后新建批量转化函数create.py(与标注好的JSON 文件放置于同一目录下):
import os
#json_folder = r" C:\Users\Jason\Desktop\dataset\re_DJI_20201002103929_0033_S"
json_folder = os.getcwd()
# 获取文件夹内的文件名
FileNameList = os.listdir(json_folder)
# 激活labelme环境
os.system("conda activate labelme")
for i in range(len(FileNameList)):
# 判断当前文件是否为json文件
if(os.path.splitext(FileNameList[i])[1] == ".json"):
json_file = json_folder + "\\" + FileNameList[i]
# 将该json文件转为png
os.system("labelme_json_to_dataset " + json_file)
在labelme环境下CD至该目录下运行,完成3文件(如下图)
4、 Culane格式数据集制作
1) 新建 lanesuidao文件夹,其中新建all文件夹和list文件夹
2) jpg 和 json分开放置在新的文件夹中(为了分别使用程序形成TXT文件)
3) trainlist.py生成trainlist.txt,其中改三处地方,文件路径、txt名字和file.write里的名字(要和第一步的 all相同 /all/)
#!/usr/bin/python
import os
def ListFilesToTxt(dir,file,wildcard,recursion):
exts = wildcard.split(" ")
files = os.listdir(dir)
for name in files:
fullname=os.path.join(dir,name)
if(os.path.isdir(fullname) & recursion):
ListFilesToTxt(fullname,file,wildcard,recursion)
else:
for ext in exts:
if(name.endswith(ext)):
file.write("/all/"+name + "\n") #存放合集(原图和json等)文件夹的名字
break
def Test():
dir="C:\Users\Jason\Desktop\dataset\gaokong\yuantu" #存放原图文件夹(只存放原图)路径
outfile="trainlist.txt" #写入的txt文件名
wildcard = ".jpg" #要读取的文件类型;
file = open(outfile,"w")
if not file:
print ("cannot open the file %s for writing" % outfile)
ListFilesToTxt(dir,file,wildcard, 1)
file.close()
Test()
4) json2txt.py生成txt文件 新建list_linshi文件夹(每个jpg应一个txt),json2txt.py修改两处路径
import os
import json
import numpy as np
dir_json = 'C:\Users\Jason\Desktop\dataset\gaokong\json' #json存储的文件目录
dir_txt = 'C:\Users\Jason\Desktop\dataset\gaokong\list_linshi' #txt存储目录
if not os.path.exists(dir_txt):
os.makedirs(dir_txt)
list_json = os.listdir(dir_json)
def json2txt(path_json,path_txt): #可修改生成格式
with open(path_json,'r') as path_json:
jsonx=json.load(path_json)
with open(path_txt,'w+') as ftxt:
for shape in jsonx['shapes']:
label = str(shape['label'])+' '
xy=np.array(shape['points'])
strxy = ''
for m,n in xy:
m=int(m)
n=int(n)
# print('m:',m)
# print('n:',n)
strxy+=str(m)+' '+str(n)+' '
label = strxy
ftxt.writelines(label+"\n")
for cnt,json_name in enumerate(list_json):
print('cnt=%d,name=%s'%(cnt,json_name))
path_json = dir_json + json_name
print(path_json)
path_txt = dir_txt + json_name.replace('.json','.lines.txt')
print(path_txt)
json2txt(path_json,path_txt)
5) 将隧道的原图 和 上述所有的txt文件拷贝到 lane/yuantu/
culane 数据集由jpg和相应的txt文件组成
6) 将trainlist.txt复制到 lanesuidao/list/
7) 进入seg_label_generate 文件夹,修改labelGen.sh中的路径为四个文件所在目录的路径,
生成laneseg_label
原本来源:github
稍作修改分开函数:
#!/bin/bash
# modify CULane to yours
CULane=/home/tt/mfz/lane/lane/data/gaokong #修改
OutputPath=${CULane}/laneseg_label
if [ ! -d $OutputPath ]; then
mkdir $OutputPath
fi
./seg_label_generate \
-l ${CULane}/list/train.txt \
-m imgLabel \
-d $CULane \
-w 16 \
-o $OutputPath \
# explanation:
# -l: image list file to process
# -m: set mode to "imgLabel" or "trainList"
# -d: dataset path
# -w: the width of lane labels generated
# -o: path to save the generated labels
# -s: visualize annotation, remove this option to generate labels
8) 进入某一个anaconda虚拟环境,(包含Opencv)
sh labelGen.sh,可能会提示opencv相关的错误,
解决:sudo apt-get install libopencv-dev,再sh 部分问题可见博客
9) 将laneseg_label里的label图都删掉,将在自己的PNG图替换到这儿。
注意名字需对应不可带_josn,如带后缀需批量改名
10) 将labelListGen.sh里的路径修改,sh labellistGen.sh生成train_gt.txt
修改后代码:
#!/bin/bash
# modify CULane to yours
CULane=/home/tt/mfz/lane/lane/data/gaokong #修改
OutputPath=${CULane}/laneseg_label
if [ ! -d $OutputPath ]; then
mkdir $OutputPath
fi
./seg_label_generate \
-l ${CULane}/list/train.txt \
-m trainList \
-d $CULane \
-w 16 \
-o $OutputPath \
# explanation:
# -l: image list file to process
# -m: set mode to "imgLabel" or "trainList"
# -d: dataset path
# -w: the width of lane labels generated
# -o: path to save the generated labels
# -s: visualize annotation, remove this option to generate labels
三个文件夹构成数据集
yuantu (原图jpg和相应的json2txt.py生成的txt文件组成)
laneseg_label(嵌套文件夹下包含替换后的PNG)
list(包含俩个txt文件,trainlist.py生成的train.txt 和 labelListGen.sh生成的train_gt.txt)
11) 修改configs/culane.py中的相关参数 和 路径可以开始训练 注意 数据集文件夹 log 代码文件夹不 要放在一起**