from glob import glob import pandas as pd import json import os 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): if filename.endswith('.json'): image_filename = filename.split('/')[-1].replace('.json', '.png') image_id = int(filename.split('/')[-1].split('.')[0]) 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 = obj['bndbox']['xmin'] ymin = obj['bndbox']['ymin'] xmax = obj['bndbox']['xmax'] ymax = 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 # Example usage: json_directory = '/content/final/train/annots/' # Replace with the directory containing your JSON files df = jsons_to_dataframe(json_directory) import pandas as pd import json # Example DataFrame # Assuming df is your pandas DataFrame containing the annotations # For demonstration, let's assume df is structured as per the provided example # Define categories in the same order as provided categories = [ {'id': 1, 'name': 'Active_IC'}, {'id': 2, 'name': 'capacitor'}, {'id': 3, 'name': 'connectors'}, {'id': 4, 'name': 'crystal'}, {'id': 5, 'name': 'diode'}, {'id': 6, 'name': 'fuse'}, {'id': 7, 'name': 'gnd'}, {'id': 8, 'name': 'headers'}, {'id': 9, 'name': 'inductor'}, {'id': 10, 'name': 'led'}, {'id': 11, 'name': 'nmos'}, {'id': 12, 'name': 'npn'}, {'id': 13, 'name': 'pmos'}, {'id': 14, 'name': 'pnp'}, {'id': 15, 'name': 'pwr'}, {'id': 16, 'name': 'pwr_connector'}, {'id': 17, 'name': 'resistor'}, {'id': 18, '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: # 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 = 'coco_format.json' with open(output_json_file, 'w') as f: json.dump(coco_data, f)