gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
convert code yolo label txt files to rt-detr anno json
- User
convert code yolo label txt files to rt-detr anno json
- Assistant
convert code yolo label txt files to rt-detr anno json
To convert YOLO label TXT files to RT-DETR JSON annotations, you need to write a script that reads the YOLO TXT files and converts the data into the appropriate JSON format expected by RT-DETR. Here's a step-by-step guide and example script to help you with this conversion:
Each line in a YOLO label TXT file typically follows this format:
<class_id> <x_center> <y_center> <width> <height>
Where:
<class_id> is the integer class label.<x_center>, <y_center>, <width>, and <height> are the normalized values relative to the image dimensions.RT-DETR expects annotation data in COCO-style JSON format. A typical COCO annotation file contains:
images: Information about each image.annotations: Bounding box and category data for each object.categories: All category labels used.Assuming the YOLO annotations are normalized and you have access to the image dimensions, here's a Python script to perform the conversion:
import os
import json
from PIL import Image
def convert_yolo_to_rtdetr(yolo_dir, images_dir, output_json):
# Initialize the JSON structure
coco_json = {
"images": [],
"annotations": [],
"categories": []
}
# Populate the categories (modify as per your needs)
categories = [{"id": i, "name": f"class_{i}"} for i in range(10)] # Example: 10 classes
coco_json["categories"] = categories
annotation_id = 1
for yolo_file in os.listdir(yolo_dir):
if not yolo_file.endswith(".txt"):
continue
# Corresponding image file
image_file = os.path.join(images_dir, yolo_file.replace(".txt", ".jpg")) # Modify if image extension is different
if not os.path.exists(image_file):
print(f"Image file {image_file} not found, skipping...")
continue
# Get image dimensions
with Image.open(image_file) as img:
width, height = img.size
# Add image info to JSON
image_info = {
"id": len(coco_json["images"]) + 1,
"file_name": os.path.basename(image_file),
"width": width,
"height": height
}
coco_json["images"].append(image_info)
# Read YOLO annotations
yolo_path = os.path.join(yolo_dir, yolo_file)
with open(yolo_path, 'r') as f:
for line in f:
# Parse the YOLO line
class_id, x_center, y_center, box_width, box_height = map(float, line.split())
# Convert to absolute coordinates
x_center *= width
y_center *= height
box_width *= width
box_height *= height
# Convert YOLO (center, width, height) to COCO (x, y, width, height)
x = x_center - box_width / 2
y = y_center - box_height / 2
# Add annotation info
annotation_info = {
"id": annotation_id,
"image_id": image_info["id"],
"category_id": int(class_id),
"bbox": [x, y, box_width, box_height],
"area": box_width * box_height,
"iscrowd": 0
}
coco_json["annotations"].append(annotation_info)
annotation_id += 1
# Save to JSON file
with open(output_json, 'w') as json_file:
json.dump(coco_json, json_file, indent=4)
# Example usage
yolo_annotations_dir = "path/to/yolo/annotations"
images_directory = "path/to/images"
output_json_file = "output/annotations.json"
convert_yolo_to_rtdetr(yolo_annotations_dir, images_directory, output_json_file)
images_dir contains all the images corresponding to the YOLO label files for accurate dimension extraction.Not yet included in a published release.