import json
import os
import glob
import os.path as osp
def labelme2yolov2Seg(jsonfilePath="", resultDirPath="", classList=["buds"]):
if (not os.path.exists(resultDirPath)):
os.mkdir(resultDirPath)
jsonfileList = glob.glob(osp.join(jsonfilePath, "*.json"))
print(jsonfileList)
for jsonfile in jsonfileList:
with open(jsonfile, "r") as f:
file_in = json.load(f)
shapes = file_in["shapes"]
with open(resultDirPath + "\\" + jsonfile.split("\\")[-1].replace(".json", ".txt"), "w") as file_handle:
for shape in shapes:
file_handle.writelines(str(classList.index(shape["label"])) + " ")
for point in shape["points"]:
x = point[0] / file_in["imageWidth"]
y = point[1] / file_in["imageHeight"]
file_handle.writelines(str(x) + " " + str(y) + " ")
file_handle.writelines("\n")
file_handle.close()
f.close()
if __name__ == "__main__":
# 转换json文件
jsonfilePath = r"D:\Project\8\main\my_seg\json"
# 生成txt文件
resultDirPath = r"D:\Project\8\main\my_seg\txt"
#自己的类别
labelme2yolov2Seg(jsonfilePath=jsonfilePath, resultDirPath=resultDirPath, classList=["apple",'banan'])
确保这几个函数,你的环境!!!