当我使用 labelme 界面中的图像标签作为输出时,我得到 json 文件。但在标记后我需要采用 png、bmp、jpeg 等图像格式。谁能建议我任何代码?
导入json 从 PIL 导入图像
with open('your,json') as f:
data = json.load(f)
# Load the file path from the json
imgpath = data['yourkey']
# Place the image path into the open method
img = Image.open(imgpath)
最佳答案
基于tutorial原版repository ,您可以使用labelme_json_to_dataset <<JSON_PATH>> -o <<OUTPUT_FOLDER_PATH>>
.
在 python
上运行它/jupyter
,您可以使用:
import os
def labelme_json_to_dataset(json_path):
os.system("labelme_json_to_dataset "+json_path+" -o "+json_path.replace(".","_"))
如果您需要对多个图像执行此操作,只需循环该函数即可。
基于issue ,可以使用 labelme2voc.py 或 labelme2coco.py 重新实现 labelme_json_to_dataset 行为。
您还可以使用其他实现,例如 labelme2Datasets
您还可以使用 labelme
实现您自己对 labelme_json_to_dataset 的修改图书馆。基本上,您使用 label_file = labelme.LabelFile(filename=filename)
接下来是 img = labelme.utils.img_data_to_arr(label_file.imageData)
。流程的示例如下:
import labelme
import os
import glob
def labelme2images(input_dir, output_dir, force=False, save_img=False, new_size=False):
"""
new_size_width, new_size_height = new_size
"""
if save_img:
_makedirs(path=osp.join(output_dir, "images"), force=force)
if new_size:
new_size_width, new_size_height = new_size
print("Generating dataset")
filenames = glob.glob(osp.join(input_dir, "*.json"))
for filename in filenames:
# base name
base = osp.splitext(osp.basename(filename))[0]
label_file = labelme.LabelFile(filename=filename)
img = labelme.utils.img_data_to_arr(label_file.imageData)
h, w = img.shape[0], img.shape[1]
if save_img:
if new_size:
img_pil = Image.fromarray(img).resize((new_size_height, new_size_width))
else:
img_pil = Image.fromarray(img)
img_pil.save(osp.join(output_dir, "images", base + ".jpg"))
关于json - 如何将 json 文件从 labelme 界面转换为 png 或图像格式文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66887103/