提供一个脚本,专门合并百度的语义分割标注工具EISeg生成的文件夹。 EISeg默认标注完之后会在你的文件夹下面生成一个label文件夹,里面会存放png格式的标注图像和一个annotations.json文件。该脚本的作用就是将dirpath路径下所有的标注图像的文件夹合并成一个,文件会被从重命名为序号为1开始的数字名字。
import os
import codecs
import threadpool
import json
import shutil
dirpath = r'xxx'
dst_path_dir = r'xxx'
annotation_files = [os.path.join(y, file) for y, z, x in os.walk(dirpath)
for file in x if os.path.split(file)[1] == 'annotations.json']
index_files = []
index = 0
for annotation_file in annotation_files:
dir = os.path.split(annotation_file)[0].replace('label', '')
with codecs.open(annotation_file) as f:
data = json.load(f)
for info in data['images']:
index_files.append([index, os.path.join(dir, info['file_name'])])
index += 1
dst_label_dir = os.path.join(dst_path_dir, 'labels')
dst_img_dir = os.path.join(dst_path_dir, 'images')
if not os.path.exists(dst_label_dir):
os.makedirs(dst_label_dir)
if not os.path.exists(dst_img_dir):
os.makedirs(dst_img_dir)
def ThreadFun_c1(file):
index = file[0]
file = file[1]
print(file)
src_label_dir = os.path.join(os.path.split(file)[0], 'label')
label_name = os.path.splitext(os.path.split(file)[1])[0] + '.png'
img_name = os.path.split(file)[1]
dst_label_name = '{}.png'.format(index)
dst_img_name = '{}{}'.format(index, os.path.splitext(img_name)[1])
shutil.copy(os.path.join(src_label_dir, label_name), os.path.join(dst_label_dir, dst_label_name))
shutil.copy(file, os.path.join(dst_img_dir, dst_img_name))
pool = threadpool.ThreadPool(16)
requests = threadpool.makeRequests(ThreadFun_c1, index_files)
[pool.putRequest(req) for req in requests]
pool.wait()
|