labelbox_export_json_app.py 2.91 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
# 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)
26 27 28 29 30

        six.print_("There are:"
                   + str(len(decoded_data))
                   + " samples to download.")

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        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')
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

                if not os.path.exists(file_name):
                    six.print_("downloading to:" + file_name)
                    urllib.request.urlretrieve(instance_uri, file_name)

                if not os.path.exists(file_name):
                    six.print_("ERROR: Failed to download:" + file_name)
                else:
                    if output_format == 'grey':

                        file_name = os.path.join(dir_name,
                                                 filename_no_extension
                                                 + '_' + value + '.png')

                        if not os.path.exists(file_name):
                            image = cv2.imread(file_name)
                            grey = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
                            cv2.imwrite(file_name, grey)
                            six.print_("writing to:" + file_name)

                    elif output_format == 'rgb':

                        file_name = os.path.join(dir_name,
                                                 filename_no_extension
                                                 + '_' + value + '.png')

                        if not os.path.exists(file_name):
                            image = cv2.imread(file_name)
                            rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR)
                            cv2.imwrite(file_name, rgb)
                            six.print_("writing to:" + file_name)

        six.print_("Done")