File size: 1,373 Bytes
ce190ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from pathlib import Path

if __name__ == "__main__":
    # labelbox json export path
    path = "/Users/victor/Downloads/export-2021-02-27T17_15_30.291Z.json"
    # where to write the downloaded images
    out = Path("/Users/victor/Downloads/labelbox_test_flood-v2")
    # create out dir
    out.mkdir(exist_ok=True, parents=True)

    # load export data
    with open(path, "r") as f:
        data = json.load(f)

    for i, d in enumerate(data):
        # find all polygons
        objects = d["Label"]["objects"]
        # retrieve original image name
        name = d["External ID"]
        stem = Path(name).stem
        # output dir for current image
        m_out = out / stem[:30]
        m_out.mkdir(exist_ok=True, parents=True)

        # save 1 png per polygon
        for o, obj in enumerate(objects):
            print(f"{i}/{len(data)} : {o}/{len(objects)}")

            # create verbose label -> "cannotflood", "mustflood"
            label = obj["value"].replace("_", "")
            # unique polygon mask filename
            m_path = m_out / f"{stem}_{label}_{o}.png"
            # download address for curl
            uri = obj["instanceURI"]
            # command to download the image
            command = f'curl {uri} > "{str(m_path)}"'
            # execute command
            os.system(command)
            print("#" * 20)