Coco Merge App.
The pyodi coco app can be used to merge COCO annotation files.
Example usage:
pyodi coco merge coco_1.json coco_2.json output.json
This app merges COCO annotation files by replacing original image and annotations ids with new ones
and adding all existent categories.
API REFERENCE
  
  
coco_merge(input_extend, input_add, output_file, indent=None)
  
  
      Merge COCO annotation files.
  Parameters:
  
    
      
        | Name | Type | Description | Default | 
    
    
        
          | input_extend | str | Path to input file to be extended. | required | 
        
          | input_add | str | Path to input file to be added. | required | 
        
          | output_file |  | Path to output file with merged annotations. | required | 
        
          | indent | Optional[int] | Argument passed to json.dump. See https://docs.python.org/3/library/json.html#json.dump. | None | 
    
  
      
        Source code in pyodi/apps/coco/coco_merge.py
        | 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 | @logger.catch(reraise=True)
def coco_merge(
    input_extend: str, input_add: str, output_file: str, indent: Optional[int] = None,
) -> str:
    """Merge COCO annotation files.
    Args:
        input_extend: Path to input file to be extended.
        input_add: Path to input file to be added.
        output_file : Path to output file with merged annotations.
        indent: Argument passed to `json.dump`. See https://docs.python.org/3/library/json.html#json.dump.
    """
    with open(input_extend, "r") as f:
        data_extend = json.load(f)
    with open(input_add, "r") as f:
        data_add = json.load(f)
    output: Dict[str, Any] = {
        k: data_extend[k] for k in data_extend if k not in ("images", "annotations")
    }
    output["images"], output["annotations"] = [], []
    for i, data in enumerate([data_extend, data_add]):
        logger.info(
            "Input {}: {} images, {} annotations".format(
                i + 1, len(data["images"]), len(data["annotations"])
            )
        )
        cat_id_map = {}
        for new_cat in data["categories"]:
            new_id = None
            for output_cat in output["categories"]:
                if new_cat["name"] == output_cat["name"]:
                    new_id = output_cat["id"]
                    break
            if new_id is not None:
                cat_id_map[new_cat["id"]] = new_id
            else:
                new_cat_id = max(c["id"] for c in output["categories"]) + 1
                cat_id_map[new_cat["id"]] = new_cat_id
                new_cat["id"] = new_cat_id
                output["categories"].append(new_cat)
        img_id_map = {}
        for image in data["images"]:
            n_imgs = len(output["images"])
            img_id_map[image["id"]] = n_imgs
            image["id"] = n_imgs
            output["images"].append(image)
        for annotation in data["annotations"]:
            n_anns = len(output["annotations"])
            annotation["id"] = n_anns
            annotation["image_id"] = img_id_map[annotation["image_id"]]
            annotation["category_id"] = cat_id_map[annotation["category_id"]]
            output["annotations"].append(annotation)
    logger.info(
        "Result: {} images, {} annotations".format(
            len(output["images"]), len(output["annotations"])
        )
    )
    with open(output_file, "w") as f:
        json.dump(output, f, indent=indent)
    return output_file
 |