iasjkk commited on
Commit
77b1729
·
verified ·
1 Parent(s): c0adea4

Upload json_tococo.py

Browse files
Files changed (1) hide show
  1. json_tococo.py +103 -0
json_tococo.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+
4
+ # Function to convert a single JSON file to COCO format
5
+ def convert_to_coco(json_data):
6
+ # Initialize COCO data structure
7
+ coco_data = {
8
+ "info": {},
9
+ "licenses": [],
10
+ "images": [],
11
+ "annotations": [],
12
+ "categories": []
13
+ }
14
+
15
+ # Populate 'info' field (optional)
16
+ coco_data["info"] = {
17
+ "description": "Converted from custom format to COCO format",
18
+ "url": "http://cocodataset.org",
19
+ "version": "1.0",
20
+ "year": 2024,
21
+ "contributor": "Anonymous",
22
+ "date_created": "2024-06-29"
23
+ }
24
+
25
+ # Populate 'images' field
26
+ image_data = {
27
+ "id": 1,
28
+ "file_name": json_data["filename"],
29
+ "width": int(json_data["size"]["width"]),
30
+ "height": int(json_data["size"]["height"]),
31
+ "license": None,
32
+ "flickr_url": None,
33
+ "coco_url": None,
34
+ "date_captured": None
35
+ }
36
+ coco_data["images"].append(image_data)
37
+
38
+ # Populate 'annotations' field
39
+ annotation_id = 1
40
+ for obj in json_data["object"]:
41
+ annotation = {
42
+ "id": annotation_id,
43
+ "image_id": image_data["id"],
44
+ "category_id": None, # This needs to be mapped to COCO categories
45
+ "segmentation": [],
46
+ "area": 0,
47
+ "bbox": [
48
+ float(obj["bndbox"]["xmin"]),
49
+ float(obj["bndbox"]["ymin"]),
50
+ float(obj["bndbox"]["xmax"]) - float(obj["bndbox"]["xmin"]),
51
+ float(obj["bndbox"]["ymax"]) - float(obj["bndbox"]["ymin"])
52
+ ],
53
+ "iscrowd": 0,
54
+ "attributes": obj.get("attributes", [])
55
+ }
56
+ coco_data["annotations"].append(annotation)
57
+ annotation_id += 1
58
+
59
+ return coco_data
60
+
61
+ # Directory containing JSON files
62
+ json_dir = "/path/to/your/json/files/"
63
+
64
+ # Initialize a list to hold all COCO data dictionaries
65
+ all_coco_data = []
66
+
67
+ # Iterate over each JSON file in the directory
68
+ for filename in os.listdir(json_dir):
69
+ if filename.endswith(".json"):
70
+ json_path = os.path.join(json_dir, filename)
71
+ with open(json_path, 'r') as f:
72
+ json_data = json.load(f)
73
+
74
+ # Convert JSON data to COCO format
75
+ coco_data = convert_to_coco(json_data)
76
+
77
+ # Append to the list of all COCO data
78
+ all_coco_data.append(coco_data)
79
+
80
+ # Optionally, merge all COCO data into one dictionary
81
+ merged_coco_data = {
82
+ "info": all_coco_data[0]["info"], # Assuming info is the same across all files
83
+ "licenses": all_coco_data[0]["licenses"], # Assuming licenses are the same across all files
84
+ "images": [],
85
+ "annotations": [],
86
+ "categories": all_coco_data[0]["categories"] # Assuming categories are the same across all files
87
+ }
88
+
89
+ # Merge images and annotations
90
+ for coco_data in all_coco_data:
91
+ merged_coco_data["images"].extend(coco_data["images"])
92
+ merged_coco_data["annotations"].extend(coco_data["annotations"])
93
+
94
+ # Convert to JSON format
95
+ merged_json = json.dumps(merged_coco_data, indent=4)
96
+ print(merged_json)
97
+
98
+ # Optionally, save merged JSON to a file
99
+ output_path = os.path.join(json_dir, "merged_coco_data.json")
100
+ with open(output_path, 'w') as f:
101
+ json.dump(merged_coco_data, f, indent=4)
102
+
103
+ print(f"Merged COCO data saved to {output_path}")