labelme2voc.py无法读取window下生成的json文件

labelme2voc.py无法读取window下生成的json文件

    • 解决方法:

在windows系统下,利用labelme标注图像生成的json文件,图像的路径是反斜杠的,例如:“imagePath”: “…\imgs\28.9_3.jpg”;如果在linunx系统下,运行 python labelme2voc.py --input_dir jsons --output_dir data_dataset_voc --labels labels.txt时,会提示错误:
Failed opening image file: jsons_win/…\stage_first\54_561_f.jpg
Traceback (most recent call last):
File “/home/zhangdeshan/.local/lib/python3.7/site-packages/labelme/label_file.py”, line 117, in load
base64.b64encode(imageData).decode(“utf-8”),
File “/data/envs/conda/lib/python3.7/base64.py”, line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not ‘NoneType’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “labelme2voc.py”, line 106, in
main(args)
File “labelme2voc.py”, line 52, in main
label_file = labelme.LabelFile(filename=filename)
File “/home/zhangdeshan/.local/lib/python3.7/site-packages/labelme/label_file.py”, line 44, in init
self.load(filename)
File “/home/zhangdeshan/.local/lib/python3.7/site-packages/labelme/label_file.py”, line 135, in load
raise LabelFileError(e)
labelme.label_file.LabelFileError: a bytes-like object is required, not ‘NoneType’

解决方法:

在labelme2voc.py文件中,添加一段代码,主要作用是将’\‘替换为’//‘,以便linux能正确的识别图片路径

如下:

for filename in glob.glob(osp.join(args.input_dir, “*.json”)):
print(“Generating dataset from:”, filename)
with open(filename, “r”) as f: # 以只读方式打开文件
data = f.read() # 读取文件,读取为一个字符串
str_replace = data.replace(‘\’,‘/’)#将字符串中的某个字符进行替换
with open(filename, “w”) as f:#重新打开文件,选择写入模式
f.write(str_replace) # 将修改后的字符串重新写入文件
label_file = labelme.LabelFile(filename=filename)
print(label_file)
base = osp.splitext(osp.basename(filename))[0]
print(base)
out_img_file = osp.join(args.output_dir, “JPEGImages”, base + “.jpg”)
out_lbl_file = osp.join(
args.output_dir, “SegmentationClassnpy”, base + “.npy”
)
out_png_file = osp.join(
args.output_dir, “SegmentationClass”, base + “.png”
)

你可能感兴趣的:(python,开发语言)