|
- import argparse
- import json
-
-
- class annotation_converter(object):
- def __init__(self, dataset_info):
- self.cur_inst_id = 1
- self.images = []
- self.annotations = []
- self.categories = []
- self.info = {
- "description": dataset_info["ruleset_code"],
- "url": "http://www.cambricon.com",
- "version": "1.0",
- "year": 2021,
- "contributor": "Cambricon",
- "date_created": "2021-01-27 09:11:52.357475",
- }
- self.licenses = [
- {
- "url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
- "id": 1,
- "name": "Attribution-NonCommercial-ShareAlike License",
- },
- {
- "url": "http://creativecommons.org/licenses/by-nc/2.0/",
- "id": 2,
- "name": "Attribution-NonCommercial License",
- },
- {
- "url": "http://creativecommons.org/licenses/by-nc-nd/2.0/",
- "id": 3,
- "name": "Attribution-NonCommercial-NoDerivs License",
- },
- {
- "url": "http://creativecommons.org/licenses/by/2.0/",
- "id": 4,
- "name": "Attribution License",
- },
- {
- "url": "http://creativecommons.org/licenses/by-sa/2.0/",
- "id": 5,
- "name": "Attribution-ShareAlike License",
- },
- {
- "url": "http://creativecommons.org/licenses/by-nd/2.0/",
- "id": 6,
- "name": "Attribution-NoDerivs License",
- },
- {
- "url": "http://flickr.com/commons/usage/",
- "id": 7,
- "name": "No known copyright restrictions",
- },
- {
- "url": "http://www.usa.gov/copyright.shtml",
- "id": 8,
- "name": "United States Government Work",
- },
- ]
-
- def with_image(self, img):
- self.images.append(
- {
- "height": img["image_height"],
- "width": img["image_width"],
- "id": img["image_id"],
- "file_name": img["image_name"],
- "date_captured": "2013-11-21 04:53:43",
- "license": 4,
- }
- )
-
- def with_instance(self, inst):
- self.annotations.append(
- {
- "bbox": inst["box"][0],
- "area": self.__area(inst["box"][0]),
- "image_id": inst["image_id"],
- "id": self.cur_inst_id,
- "iscrowd": 0,
- "category_id": inst["rule_id"] + 1,
- }
- )
- self.cur_inst_id = self.cur_inst_id + 1
-
- def with_rule(self, rule):
- self.categories.append({"id": rule["rule_id"] + 1, "name": rule["rule_code"]})
-
- def __area(self, bbox):
- return bbox[2] * bbox[3]
-
- def convert(self):
- return {
- "info": self.info,
- "licenses": self.licenses,
- "images": self.images,
- "type": "instances",
- "annotations": self.annotations,
- "categories": self.categories,
- }
-
-
- def main():
-
- parser = argparse.ArgumentParser()
- parser.add_argument("--input", type=str, help="Input file", required=True)
- parser.add_argument("--output", type=str, help="Output file", required=True)
- args = parser.parse_args()
-
- with open(args.input) as f:
- input = json.load(f)
-
- converter = annotation_converter(input["dataset_info"])
- for image in input["image_info"]:
- converter.with_image(image)
-
- for instance in input["instance_info"]:
- converter.with_instance(instance)
-
- for rule in input["rule_dict"]:
- converter.with_rule(rule)
-
- res = converter.convert()
-
- with open(args.output, "w") as f:
- json.dump(res, f)
-
-
- if __name__ == "__main__":
- main()
|