labelbox_export_json_app.py 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
# coding=utf-8

""" Processing for labelbox_export_json appliction. """

import os
import json
import urllib.request
import six
import cv2


def run_labelbox_json_export(json_file, output_dir, output_format):
    """
    Parses the Labelbox json file, and exports data.

    :param json_file: file name of json file
    :param output_dir: where to write to
    :param output_format: output image format, [grey|rgb|rgba (default)]
    """
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    with open(json_file) as file:
        data = file.read()
        decoded_data = json.loads(data)
        for sample in decoded_data:
            dataset_name = sample['Dataset Name']
            external_id = sample['External ID']
            basename = os.path.basename(external_id)
            filename_no_extension = os.path.splitext(basename)[0]
            objects = sample['Label']['objects']

            dir_name = os.path.join(output_dir, dataset_name)
            if not os.path.exists(dir_name):
                os.makedirs(dir_name)

            for obj in objects:
                instance_uri = obj['instanceURI']
                value = obj['value']
                file_name = os.path.join(dir_name,
                                         filename_no_extension
                                         + '_' + value + '.bmp')
                six.print_("writing to:" + file_name)

                urllib.request.urlretrieve(instance_uri, file_name)

                if output_format == 'grey':
                    image = cv2.imread(file_name)
                    grey = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
                    file_name = os.path.join(dir_name,
                                             filename_no_extension
                                             + '_' + value + '.png')
                    cv2.imwrite(file_name, grey)
                    six.print_("writing to:" + file_name)

                elif output_format == 'rgb':
                    image = cv2.imread(file_name)
                    rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
                    file_name = os.path.join(dir_name,
                                             filename_no_extension
                                             + '_' + value + '.png')
                    cv2.imwrite(file_name, rgb)
                    six.print_("writing to:" + file_name)