gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
how to convert yolo txt labels to json coco type label python
- User
how to convert yolo txt labels to json coco type label python
- Assistant
how to convert yolo txt labels to json coco type label python
To convert YOLO format labels to COCO format in Python, you need to read the YOLO text files, interpret the label information, and then structure this data in a JSON format that complies with the COCO dataset specification. Here’s a step-by-step guide on how to do this conversion:
In YOLO format, each label file corresponds to one image and contains rows with the following structure:
<object-class> <x_center> <y_center> <width> <height>
object-class: integer, the class of the object.x_center, y_center, width, height: float, normalized to the image size.In COCO format, the JSON file contains the following main items:
images: a list of image metadata.annotations: a list of annotation metadata.licenses, info, categories: optional metadata information.Here's a sample Python script to perform the conversion:
import os
import json
def yolo_to_coco(yolo_annotations_path, images_info, categories_info):
annotations = []
image_id_counter = 0
annotation_id_counter = 0
for image_info in images_info:
image_id = image_id_counter
filename = image_info['file_name']
width = image_info['width']
height = image_info['height']
image_annotations_path = os.path.join(yolo_annotations_path, os.path.splitext(filename)[0] + '.txt')
try:
with open(image_annotations_path, 'r') as file:
for line in file:
parts = line.strip().split()
class_id = int(parts[0])
x_center_rel = float(parts[1])
y_center_rel = float(parts[2])
width_rel = float(parts[3])
height_rel = float(parts[4])
# Convert YOLO format from relative to absolute values
x_center = x_center_rel * width
y_center = y_center_rel * height
bbox_width = width_rel * width
bbox_height = height_rel * height
# Convert to top-left corner format
x_min = x_center - bbox_width / 2
y_min = y_center - bbox_height / 2
# Create COCO annotation format
annotation = {
"id": annotation_id_counter,
"image_id": image_id,
"category_id": class_id,
"bbox": [x_min, y_min, bbox_width, bbox_height],
"area": bbox_width * bbox_height,
"iscrowd": 0
}
annotations.append(annotation)
annotation_id_counter += 1
except FileNotFoundError:
print(f"Annotation file for image {filename} not found, skipping.")
image_id_counter += 1
# Construct COCO JSON structure
coco_json = {
"images": images_info,
"annotations": annotations,
"categories": categories_info
}
return coco_json
# Example usage
images_info = [
{
"id": 0,
"file_name": "image1.jpg",
"width": 640,
"height": 480
},
# Add more image metadata as required
]
categories_info = [
{
"id": 0,
"name": "category1"
},
# Add more categories as required
]
yolo_annotations_path = 'path/to/yolo/annotations'
coco_output = yolo_to_coco(yolo_annotations_path, images_info, categories_info)
with open('coco_annotations.json', 'w') as json_file:
json.dump(coco_output, json_file, indent=4)
images_info and categories_info are filled with accurate information regarding your images and classes..txt files with the same basename as your images.Test the output with COCO tools or visualization tools to ensure correctness.