root_folder = r"./img/json" #将json 文件夹里面的东西转移到img/json文件夹下面
Paste_PNG_folder = r"./img/orimg" #解析json 文件夹,获取元素图像
Paste_label_folder = r"./img/label" #解析json 文件夹,获取白色标签
Paste_label_viz_folder = r"./img/label_viz" #解析json 文件夹,验证数据标注是否正确
Make_mask_folder=r"./img/cv2_mask" #解析json 文件夹,获取红色标签
# -*- coding: utf-8 -*-
# 1.单文件json格式标签转为png格式
'''
直接cmd终端运行:
labelme_json_to_dataset 文件名.json
可得到一个文件夹,有4个文件
img.png,源文件图像
label.png,标签图像
label_names.txt,标签中的各个类别的名称
label_viz.png,源文件与标签融合文件
注:JPG是有损压缩,而PNG是无损压缩,png图像会大于jpg图像。
'''
import os
import shutil
import cv2
# 2.批量json格式标签转为png格式
# os模块多次调用CMD命令,os.popen() 或 os.system()
# 多次调用labelme_json_to_dataset,为每个json生成一个目录文件(包含4个文件)
def json2png(json_folder, root_folder):
# 获取文件夹内的文件名
FileNameList = os.listdir(json_folder)
print(FileNameList)
# 激活labelme环境
os.system("activate labelme")
for i in range(len(FileNameList)):
# 判断当前文件是否为json文件
x = os.path.splitext(FileNameList[i])
if (x[1] == ".json"):
json_file = os.path.join(json_folder, FileNameList[i])
out_file = os.path.join(root_folder, x[0] + '_json')
# 将该json文件转为png
# 用法:labelme_json_to_dataset [-h] [-o OUT] json_file
os.system("labelme_json_to_dataset " + ' -o ' + out_file + ' ' + json_file)
# 3.将文件批量转化成标准格式(即,所有原图像一个文件夹,所有label一个文件夹,所有可视化图像一个文件夹)
def file_split(folder):
root_folder, Paste_PNG_folder, Paste_label_folder, Paste_label_viz_folder,Make_mask_folder = folder[0], folder[1], folder[2], folder[
3],folder[4]
# 获取文件夹内的文件名
PathNameList = os.listdir(root_folder)
print(PathNameList)
NewFileName = 1
for i in range(len(PathNameList)):
# 判断当前文件是否为json文件
x_folder = os.path.splitext(PathNameList[i])[0]
# print(x_folder)
img_file = os.path.join(root_folder, x_folder)
# print(img_file)
FileNameList = os.listdir(img_file)
# print(FileNameList)
# 复制img.png文件
PNG_file = os.path.join(img_file, "img.png")
new_PNG_file = Paste_PNG_folder + "\\" + str(NewFileName) + ".png"
shutil.copyfile(PNG_file, new_PNG_file)
# 复制label.png文件
label_file = os.path.join(img_file, "label.png")
new_label_file = Paste_label_folder + "\\" + str(NewFileName) + ".png"
shutil.copyfile(label_file, new_label_file)
# 复制label_viz.png文件
label_viz_file = os.path.join(img_file, "label_viz.png")
new_label_viz_file = Paste_label_viz_folder + "\\" + str(NewFileName) + ".png"
shutil.copyfile(label_viz_file, new_label_viz_file)
# 制作mask 图像
label_png = cv2.imread(new_label_file, 0)
label_png[label_png > 0] = 255
png_save_path=os.path.join(Make_mask_folder,str(NewFileName))+".png"
cv2.imwrite(png_save_path, label_png)
# 文件序列名+1
NewFileName = NewFileName + 1
if __name__ == '__main__':
json_folder = r"./json"
# 保存中间生成文件的文件夹, 每个子目录下有4个文件:img.png,label.png,label_names.txt,label_viz.png
root_folder = r"./img/json"
# 保存最终生成文件的文件夹,原图像、label、label_viz
Paste_PNG_folder = r"./img/orimg"
Paste_label_folder = r"./img/label"
Paste_label_viz_folder = r"./img/label_viz"
Make_mask_folder=r"./img/cv2_mask"
folder = [root_folder, Paste_PNG_folder, Paste_label_folder, Paste_label_viz_folder,Make_mask_folder]
for folder_x in folder:
if os.path.exists(folder_x):
shutil.rmtree(folder_x)
os.makedirs(folder_x)
else:
os.makedirs(folder_x)
# 用json生成png
json2png(json_folder, root_folder)
# 文件分开
file_split(folder)