4 回答

TA贡献1809条经验 获得超8个赞
使用命令FOR /?阅读substitution of FOR variable references帮助输出的最后一页。要获取文件的基本名称,%%~na可以使用。在没有的情况下运行它,ECHO OFF以便您可以看到每个命令。
setlocal
set "yourDir=C:\Users\Acer\Desktop\datasets\combined masks\"
set "yourExt=*.json"
pushd %yourDir%
for %%a in (*%yourExt%) do (labelme_json_to_dataset %%a -o %%~na)
popd
endlocal

TA贡献2037条经验 获得超6个赞
import labelme
import os, sys
path="path/to/directory"
dirs = os.listdir(path)
i=0
for item in dirs:
if item.endswith(".json"):
if os.path.isfile(path+item):
my_dest ="fin" + str(i)
os.system("mkdir "+my_dest)
os.system("labelme_json_to_dataset "+item+" -o "+my_dest)
i=i+1

TA贡献2039条经验 获得超8个赞
for /l %n in (2,1,50) do (labelme_json_to_dataset images%n.json -o images%n)
每个file.json
空白文件夹(之前为保存数据集而准备的)都在同一个文件夹中(使用此代码的当前目录)。

TA贡献1796条经验 获得超4个赞
这个解决方案并不完美,只是生成了遮罩和覆盖 png 文件。它也只是保留文件名。
import argparse
import base64
import json
import os
import os.path as osp
import imgviz
import PIL.Image
from labelme.logger import logger
from labelme import utils
def main():
parser = argparse.ArgumentParser()
parser.add_argument("json_file")
parser.add_argument("-o", "--out", default=None)
args = parser.parse_args()
json_file = args.json_file
if args.out is None:
out_dir = osp.basename(json_file).replace(".", "_")
out_dir = osp.join(osp.dirname(json_file), out_dir)
else:
out_dir = args.out
if not osp.exists(out_dir):
os.mkdir(out_dir)
data = json.load(open(json_file))
imageData = data.get("imageData")
if not imageData:
imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
print(imagePath)
with open(imagePath, "rb") as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode("utf-8")
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {"_background_": 0}
for shape in sorted(data["shapes"], key=lambda x: x["label"]):
label_name = shape["label"]
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
lbl, _ = utils.labelme_shapes_to_label(img.shape, data["shapes"])
label_names = [None] * (max(label_name_to_value.values()) + 1)
for name, value in label_name_to_value.items():
label_names[value] = name
lbl_viz = imgviz.label2rgb(
lbl, imgviz.asgray(img), label_names=label_names, loc="rb"
)
filename = str(json_file).split('.')[1]
utils.lblsave(osp.join(out_dir, f'.{filename}.png'), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, f'.{filename}_viz.png'))
logger.info("Saved to: {}".format(out_dir))
if __name__ == "__main__":
main()
添加回答
举报