labelme batch processing json files, json_to_dataset method

labelme batch processing json files, json_to_dataset method

This is the data generated after processing the. JSON file. The usage method is labelme_json_to_dataset + space + file name. JSON, the premise is that labelme should be installed and activated accurately. But this will cause a problem. It's too troublesome to process multiple images. Here we provide a tool to convert all JSON files directly under the. JSON file directory,

Step 1: find file

Open the abelme installation directory:
1.labelme is directly installed in the main environment of anaconda, in the directory of C: \ programdata \ anaconda3 \ lib \ site packages \ labelme \ cli (depending on the installation directory of Anaconda on your computer). You can see Documents.
2.labelme is installed in the newly created virtual environment. You need to find the virtual environment file in the envs folder under the anananda installation directory, C: \ programdata \ anaconda3 \ envs \ LB \ lib \ site packages \ labelme \ cli.

Step 2 modify file

Replace JSON with the code below_ To_ File and save

import argparse
import json
import os
import os.path as osp
import warnings
import PIL.Image
import yaml
from labelme import utils
import base64
def main():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")
    parser = argparse.ArgumentParser()
    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)
        out_dir = args.out
    if not osp.exists(out_dir):
    count = os.listdir(json_file) 
    for i in range(0, len(count)):
        path = os.path.join(json_file, count[i])
        if os.path.isfile(path):
            data = json.load(open(path))
            if data['imageData']:
                imageData = data['imageData']
                imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
                with open(imagePath, 'rb') as f:
                    imageData =
                    imageData = base64.b64encode(imageData).decode('utf-8')
            img = utils.img_b64_to_arr(imageData)
            label_name_to_value = {'_background_': 0}
            for shape in data['shapes']:
                label_name = shape['label']
                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value
            # label_values must be dense
            label_values, label_names = [], []
            for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
            assert label_values == list(range(len(label_values)))
            lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
            captions = ['{}: {}'.format(lv, ln)
                for ln, lv in label_name_to_value.items()]
            lbl_viz = utils.draw_label(lbl, img, captions)
            out_dir = osp.basename(count[i]).replace('.', '_')
            out_dir = osp.join(osp.dirname(count[i]), out_dir)
            if not osp.exists(out_dir):
            PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
            #PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
            utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
            PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
            with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
                for lbl_name in label_names:
                    f.write(lbl_name + '\n')
            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=label_names)
            with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)
            print('Saved to: %s' % out_dir)
if __name__ == '__main__':

Step 3 find labelme_json_to_dataset.exe file

The method is the same as the first step, I will only show the directory under my virtual environment: C:\ProgramData\Anaconda3\envs\lb\Scripts

Step 4 switch paths

Open anaconda prompt to activate the virtual environment

Use the cd command to switch paths: cd C:\ProgramData\Anaconda3\envs\lb\Scripts

Step 5: pass in the. json file path and perform the conversion

Executed by: labelme_ json_ To_ dataset.exe +Path to JSON file

Automatically generate conversion files in scripts:


If an error occurs AttributeError: module ' labelme.utils ’No 'draw_label 'attribute, attributeerrormodule label

You need to change the labelme version. You need to reduce the labelme version to 3.16.2. Enter the labelme environment by typing pip install labelme==3.16.2 to download this version automatically. You can succeed.

Tags: JSON Anaconda Lambda Attribute

Posted on Wed, 17 Jun 2020 03:47:42 -0400 by jabba_29