我有一个由Labelme工具生成的JSON文件格式的图像蒙版数据集,在Github tutorial (https://github.com/wkentaro/labelme/tree/master/examples/tutorial)上,它显示了如何将JSON文件更改为图像文件,我们使用以下命令行代码
labelme_json_to_dataset apc2016_obj3.json -o apc2016_obj3_json但是,这种方法一次只能处理一个文件,所以我一直在尝试使用一组代码来处理所有文件,我尝试了以下代码
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 %%a
popd
endlocal代码现在可以通过读取文件名并添加文件扩展名.json来工作。但是,将文件保存到具有相同名称的目录中时,会出现以下错误
Traceback (most recent call last):
File "c:\users\acer\.conda\envs\labelme\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\acer\.conda\envs\labelme\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\ACER\.conda\envs\labelme\Scripts\labelme_json_to_dataset.exe\__main__.py", line 7, in <module>
File "c:\users\acer\.conda\envs\labelme\lib\site-packages\labelme\cli\json_to_dataset.py", line 65, in main
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
File "c:\users\acer\.conda\envs\labelme\lib\site-packages\PIL\Image.py", line 2131, in save
fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: '000828.json\\img.png'我不熟悉cmd,需要帮助将输出保存到文件名不带.json扩展名的目录中
下面是一个文件示例,它展示了一个成功的运行应该是如何看起来像enter image description here的
(labelme) C:\Users\ACER\Desktop\datasets\combined masks>labelme_json_to_dataset 000814.json -o 000814
[[1m[33mWARNING[0m] [36mjson_to_dataset[0m:[36mmain[0m:[36m15[0m - [1m[33mThis script is aimed to demonstrate how to convert the JSON file to a single image dataset.[0m
[[1m[33mWARNING[0m] [36mjson_to_dataset[0m:[36mmain[0m:[36m17[0m - [1m[33mIt won't handle multiple JSON files to generate a real-use dataset.[0m
[[1m[37mINFO [0m] [36mjson_to_dataset[0m:[36mmain[0m:[36m73[0m - [1m[37mSaved to: 000814[0m
(labelme) C:\Users\ACER\Desktop\datasets\combined masks>发布于 2020-05-04 09:22:39
使用命令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发布于 2020-06-16 22:37:56
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发布于 2020-09-28 02:58:04
for /l %n in (2,1,50) do
(labelme_json_to_dataset images%n.json -o images%n)每个file.json和每个空文件夹(之前为保存数据集准备好的)都在同一个文件夹中(使用此代码的当前目录)。
https://stackoverflow.com/questions/61537887
复制相似问题