File size: 5,422 Bytes
1512fcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from glob import glob
import pandas as pd
import json
import os
import shutil

def jsons_to_dataframe(json_dir):
    # Initialize lists to store data
    filename_list = []
    image_id_list = []
    width_list = []
    height_list = []
    category_name_list = []
    bbox_list = []

    # Iterate over each JSON file in the directory
    for filename in os.listdir(json_dir):
        image_id = int(filename.split('/')[-1].split('.')[0])
        if (filename.endswith('.json') and image_id>=1700):
            image_dir = json_dir.replace('annots', 'images')
            image_filename = filename.split('/')[-1].replace('.json', '.png')
            shutil.copy2(f"{image_dir}/{image_filename}", "/content/drive/MyDrive/final/circuit/val/")
            json_file = os.path.join(json_dir, filename)

            # Load JSON data from file
            with open(json_file, 'r') as f:
                data = json.load(f)

            # Extract relevant data from JSON
            filename_value = image_filename#data['filename']
            width_value = int(data['size']['width'])
            height_value = int(data['size']['height'])

            # Process each object in the JSON data
            for obj in data['object']:
                category_name = obj['name']
                xmin = int(float(obj['bndbox']['xmin']))
                ymin = int(float(obj['bndbox']['ymin']))
                xmax = int(float(obj['bndbox']['xmax']))
                ymax = int(float(obj['bndbox']['ymax']))

                # Calculate width and height of the bbox
                bbox_width = xmax - xmin
                bbox_height = ymax - ymin

                # Create bbox dictionary
                bbox_dict = {
                    "xmin": xmin,
                    "ymin": ymin,
                    "width": bbox_width,
                    "height": bbox_height
                }

                # Append data to lists
                filename_list.append(filename_value)
                image_id_list.append(image_id)
                width_list.append(width_value)
                height_list.append(height_value)
                category_name_list.append(category_name)
                bbox_list.append(bbox_dict)

    # Create DataFrame
    df = pd.DataFrame({
        'filename': filename_list,
        'image_id': image_id_list,
        'width': width_list,
        'height': height_list,
        'category_name': category_name_list,
        'bbox': bbox_list
    })

    return df


categories = [
    {'id': 1, 'name': 'Active_IC'},
    {'id': 2, 'name': 'capacitor'},
    {'id': 3, 'name': 'connector'},
    {'id': 4, 'name': 'crystal'},
    {'id': 5, 'name': 'diode'},
    {'id': 6, 'name': 'gnd'},
    {'id': 7, 'name': 'inductor'},
    {'id': 8, 'name': 'led'},
    {'id': 9, 'name': 'misc'},
    {'id': 10, 'name': 'nmos'},
    {'id': 11, 'name': 'npn'},
    {'id': 12, 'name': 'pmos'},
    {'id': 13, 'name': 'pnp'},
    {'id': 14, 'name': 'pwr'},
    {'id': 15, 'name': 'pwr_connector'},
    {'id': 16, 'name': 'resistor'},
    {'id': 17, 'name': 'switch'}
]

def dataframe_to_coco_format(df):
    # Initialize COCO format dictionary
    coco_format = {
        "info": {
            "description": "COCO format dataset",
            "version": "1.0",
            "year": 2024,
            "contributor": "Anonymous",
            "date_created": "2024/06/30"
        },
        "licenses": [],
        "categories": categories,
        "images": [],
        "annotations": []
    }

    # Track image IDs to ensure uniqueness
    image_id_map = {}

    # Iterate over DataFrame rows
    for idx, row in df.iterrows():
        image_id = row['image_id']
        filename = row['filename']
        width = row['width']
        height = row['height']
        category_name = row['category_name']
        bbox = row['bbox']

        # Add image information if not already added
        if image_id not in image_id_map:
            image_id_map[image_id] = len(coco_format['images']) + 1  # COCO image ID starts from 1
            coco_format['images'].append({
                'id': image_id_map[image_id],
                'file_name': filename,
                'width': width,
                'height': height
            })

        # Find category ID
        category_id = [cat['id'] for cat in categories if cat['name'] == category_name][0]

        # Add annotation information
        coco_format['annotations'].append({
            'id': len(coco_format['annotations']) + 1,  # COCO annotation ID starts from 1
            'image_id': image_id_map[image_id],
            'category_id': category_id,
            'bbox': [bbox['xmin'], bbox['ymin'], bbox['width'], bbox['height']],
            'area': bbox['width'] * bbox['height'],
            'iscrowd': 0  # Assuming no crowds in the dataset
        })

    return coco_format



# Example usage:
# json_directory = '/content/drive/MyDrive/final/full_bboxcnn_data/annots'  # Replace with the directory containing your JSON files
# df = jsons_to_dataframe(json_directory)
# # Example usage:
# # Assuming `df` is your pandas DataFrame obtained from `jsons_to_dataframe` function

# # Convert DataFrame to COCO format
# coco_data = dataframe_to_coco_format(df)

# # Save COCO format JSON to a file
# output_json_file = '/content/drive/MyDrive/final/circuit/val/val_coco_format.json'
# with open(output_json_file, 'w') as f:
#     json.dump(coco_data, f)