pythonimage-processingcmd

To convert a batch Labelme json files to a set of images and labels using anaconda prompt


I have a dataset of image masks that were generated by the Labelme tool as JSON files and on the Github tutorial (https://github.com/wkentaro/labelme/tree/master/examples/tutorial) it shows to change the JSON file into an image file we use the following command line code

labelme_json_to_dataset apc2016_obj3.json -o apc2016_obj3_json

however this only works for one file at a time, so I have been trying to find a way to process all the files using one set of code, I've tried the following code

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

the code works now by reading the name of the file and adding the file extension .json however, saving the file to a directory with the same name including the .json extension is giving me the following error

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'

I'm not familiar with cmd and require help saving the output to a directory with the file name without the .json extenstion

below is a single file example that shows how a successful run is supposed to look like 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>

Solution

  • Use the command FOR /? to read about substitution of FOR variable references on the last page of the help output. To get just the file basename, %%~na can be used. Run this without ECHO OFF so that you can see each command.

    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