jkushwaha commited on
Commit
4f3f330
·
verified ·
1 Parent(s): e64c8bb

Create coco_json conversion_example.py

Browse files
Files changed (1) hide show
  1. coco_json conversion_example.py +174 -0
coco_json conversion_example.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from glob import glob
2
+ import pandas as pd
3
+ import json
4
+ import os
5
+
6
+ def jsons_to_dataframe(json_dir):
7
+ # Initialize lists to store data
8
+ filename_list = []
9
+ image_id_list = []
10
+ width_list = []
11
+ height_list = []
12
+ category_name_list = []
13
+ bbox_list = []
14
+
15
+ # Iterate over each JSON file in the directory
16
+ for filename in os.listdir(json_dir):
17
+ if filename.endswith('.json'):
18
+ image_filename = filename.split('/')[-1].replace('.json', '.png')
19
+ image_id = int(filename.split('/')[-1].split('.')[0])
20
+ json_file = os.path.join(json_dir, filename)
21
+
22
+ # Load JSON data from file
23
+ with open(json_file, 'r') as f:
24
+ data = json.load(f)
25
+
26
+ # Extract relevant data from JSON
27
+ filename_value = image_filename#data['filename']
28
+ width_value = int(data['size']['width'])
29
+ height_value = int(data['size']['height'])
30
+
31
+ # Process each object in the JSON data
32
+ for obj in data['object']:
33
+ category_name = obj['name']
34
+ xmin = obj['bndbox']['xmin']
35
+ ymin = obj['bndbox']['ymin']
36
+ xmax = obj['bndbox']['xmax']
37
+ ymax = obj['bndbox']['ymax']
38
+
39
+ # Calculate width and height of the bbox
40
+ bbox_width = xmax - xmin
41
+ bbox_height = ymax - ymin
42
+
43
+ # Create bbox dictionary
44
+ bbox_dict = {
45
+ "xmin": xmin,
46
+ "ymin": ymin,
47
+ "width": bbox_width,
48
+ "height": bbox_height
49
+ }
50
+
51
+ # Append data to lists
52
+ filename_list.append(filename_value)
53
+ image_id_list.append(image_id)
54
+ width_list.append(width_value)
55
+ height_list.append(height_value)
56
+ category_name_list.append(category_name)
57
+ bbox_list.append(bbox_dict)
58
+
59
+ # Create DataFrame
60
+ df = pd.DataFrame({
61
+ 'filename': filename_list,
62
+ 'image_id': image_id_list,
63
+ 'width': width_list,
64
+ 'height': height_list,
65
+ 'category_name': category_name_list,
66
+ 'bbox': bbox_list
67
+ })
68
+
69
+ return df
70
+
71
+ # Example usage:
72
+ json_directory = '/content/final/train/annots/' # Replace with the directory containing your JSON files
73
+ df = jsons_to_dataframe(json_directory)
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+ import pandas as pd
84
+ import json
85
+
86
+ # Example DataFrame
87
+ # Assuming df is your pandas DataFrame containing the annotations
88
+ # For demonstration, let's assume df is structured as per the provided example
89
+
90
+ # Define categories in the same order as provided
91
+ categories = [
92
+ {'id': 1, 'name': 'Active_IC'},
93
+ {'id': 2, 'name': 'capacitor'},
94
+ {'id': 3, 'name': 'connectors'},
95
+ {'id': 4, 'name': 'crystal'},
96
+ {'id': 5, 'name': 'diode'},
97
+ {'id': 6, 'name': 'fuse'},
98
+ {'id': 7, 'name': 'gnd'},
99
+ {'id': 8, 'name': 'headers'},
100
+ {'id': 9, 'name': 'inductor'},
101
+ {'id': 10, 'name': 'led'},
102
+ {'id': 11, 'name': 'nmos'},
103
+ {'id': 12, 'name': 'npn'},
104
+ {'id': 13, 'name': 'pmos'},
105
+ {'id': 14, 'name': 'pnp'},
106
+ {'id': 15, 'name': 'pwr'},
107
+ {'id': 16, 'name': 'pwr_connector'},
108
+ {'id': 17, 'name': 'resistor'},
109
+ {'id': 18, 'name': 'switch'}
110
+ ]
111
+
112
+ def dataframe_to_coco_format(df):
113
+ # Initialize COCO format dictionary
114
+ coco_format = {
115
+ "info": {
116
+ "description": "COCO format dataset",
117
+ "version": "1.0",
118
+ "year": 2024,
119
+ "contributor": "Anonymous",
120
+ "date_created": "2024/06/30"
121
+ },
122
+ "licenses": [],
123
+ "categories": categories,
124
+ "images": [],
125
+ "annotations": []
126
+ }
127
+
128
+ # Track image IDs to ensure uniqueness
129
+ image_id_map = {}
130
+
131
+ # Iterate over DataFrame rows
132
+ for idx, row in df.iterrows():
133
+ image_id = row['image_id']
134
+ filename = row['filename']
135
+ width = row['width']
136
+ height = row['height']
137
+ category_name = row['category_name']
138
+ bbox = row['bbox']
139
+
140
+ # Add image information if not already added
141
+ if image_id not in image_id_map:
142
+ image_id_map[image_id] = len(coco_format['images']) + 1 # COCO image ID starts from 1
143
+ coco_format['images'].append({
144
+ 'id': image_id_map[image_id],
145
+ 'file_name': filename,
146
+ 'width': width,
147
+ 'height': height
148
+ })
149
+
150
+ # Find category ID
151
+ category_id = [cat['id'] for cat in categories if cat['name'] == category_name][0]
152
+
153
+ # Add annotation information
154
+ coco_format['annotations'].append({
155
+ 'id': len(coco_format['annotations']) + 1, # COCO annotation ID starts from 1
156
+ 'image_id': image_id_map[image_id],
157
+ 'category_id': category_id,
158
+ 'bbox': [bbox['xmin'], bbox['ymin'], bbox['width'], bbox['height']],
159
+ 'area': bbox['width'] * bbox['height'],
160
+ 'iscrowd': 0 # Assuming no crowds in the dataset
161
+ })
162
+
163
+ return coco_format
164
+
165
+ # Example usage:
166
+ # Assuming `df` is your pandas DataFrame obtained from `jsons_to_dataframe` function
167
+
168
+ # Convert DataFrame to COCO format
169
+ coco_data = dataframe_to_coco_format(df)
170
+
171
+ # Save COCO format JSON to a file
172
+ output_json_file = 'coco_format.json'
173
+ with open(output_json_file, 'w') as f:
174
+ json.dump(coco_data, f)