BDD100K数据集格式转YOLO格式(目标检测)
发布时间
阅读量:
阅读量
一、创建虚拟环境
1.创建虚拟环境(建议使用python3.7以上版本,bdd100k官方要求的)
conda create -n bdd100k python=3.8
2.去他的官网https://github.com/bdd100k/bdd100k,把这个包下载下来
3.解压后,cd 到刚刚下载的这个文件的目录下,然后运行以下代码配置环境:
pip install -r requirements.txt
二、格式转换
1.使用bdd100k自带工具包将bdd100k格式转换为coco格式,例如(改为自己的路径即可):
python -m bdd100k.label.to_coco -m det -i "C:\......\det_train.json" -o "C:\......\det_train_coco.json" --nproc 4
2.运行以下代码,将coco格式转换为yolo格式:
# 在使用本格式转换工具前,先使用 bdd100k 自带工具包将 bdd100k 格式转换为 coco 格式,例如:
# python -m bdd100k.label.to_coco -m det -i "C:\......\det_train.json" -o "C:\......\det_train_coco.json" --nproc 4
# 然后修改下方路径,运行此程序,将 coco 格式转换为 yolo 格式
import json
import os
# 输入 COCO JSON 文件路径
coco_json_path = "C:/Users/BJDYJ/Desktop/BDD100K_dataset/labels/det_val_coco.json"
# 输出标签文件夹路径
output_dir = "C:/Users/BJDYJ/Desktop/BDD100K_dataset/labels/val_labels"
# 创建输出文件夹
os.makedirs(output_dir, exist_ok=True)
# 读取 COCO JSON 文件
with open(coco_json_path, 'r', encoding='utf-8') as f:
coco_data = json.load(f)
# 获取图像信息和标注信息
images = {image['id']: image for image in coco_data['images']}
annotations = coco_data['annotations']
categories = {category['id']: category['name'] for category in coco_data['categories']}
# 遍历所有标注信息
for annotation in annotations:
image_id = annotation['image_id']
category_id = annotation['category_id']
bbox = annotation['bbox'] # COCO 格式中的 bbox: [x_min, y_min, width, height]
# 获取图像的宽高
image_info = images[image_id]
img_width = image_info['width']
img_height = image_info['height']
# 计算 YOLO 格式的 bbox: [center_x, center_y, width, height]
x_min, y_min, width, height = bbox
center_x = (x_min + width / 2) / img_width
center_y = (y_min + height / 2) / img_height
norm_width = width / img_width
norm_height = height / img_height
# 生成 YOLO 格式的标签行
label_line = f"{category_id - 1} {center_x:.6f} {center_y:.6f} {norm_width:.6f} {norm_height:.6f}"
# 构建输出文件路径
txt_filename = f"{os.path.splitext(image_info['file_name'])[0]}.txt"
txt_filepath = os.path.join(output_dir, txt_filename)
# 写入标签文件
with open(txt_filepath, 'a') as txt_file:
txt_file.write(label_line + '\n')
print("标签文件已成功转换并保存到:", output_dir)
3.或者这个,小数点后位数多一些(我也不知道影响大不大)
import os
import json
# COCO格式的标签文件路径
coco_file_path = r'C:\Users\BJDYJ\Desktop\BDD100K_dataset\labels\det_train_coco.json'
# YOLO格式标签保存路径
output_dir = r'C:\Users\BJDYJ\Desktop\BDD100K_dataset\labels\labels'
# 如果输出目录不存在,则创建它
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 加载COCO格式的JSON文件
with open(coco_file_path, 'r') as f:
coco_data = json.load(f)
# 提取图像信息和注释信息
images = coco_data['images']
annotations = coco_data['annotations']
categories = coco_data['categories']
# 创建从类别ID映射到类别索引ID的字典
category_id_to_name = {cat['id']: cat['name'] for cat in categories}
category_id_to_index = {cat['id']: idx for idx, cat in enumerate(categories)}
# 创建一个字典,将图像ID映射到文件名
image_id_to_filename = {img['id']: img['file_name'] for img in images}
# 处理每一个注释,转换为YOLO格式并保存到对应的文件中
for annotation in annotations:
image_id = annotation['image_id']
category_id = annotation['category_id']
# 获取图像的文件名
image_filename = image_id_to_filename[image_id]
# 获取图片的宽度和高度
image_info = next(img for img in images if img['id'] == image_id)
img_width = image_info['width']
img_height = image_info['height']
# 获取边界框信息(COCO格式为[x_min, y_min, width, height])
bbox = annotation['bbox']
x_min, y_min, box_width, box_height = bbox
# 将COCO格式的bbox转换为YOLO格式
x_center = (x_min + box_width / 2) / img_width
y_center = (y_min + box_height / 2) / img_height
norm_width = box_width / img_width
norm_height = box_height / img_height
# 获取类别索引ID
class_id = category_id_to_index[category_id]
# YOLO格式:类ID x_center y_center width height
yolo_annotation = f"{class_id} {x_center} {y_center} {norm_width} {norm_height}\n"
# 创建输出文件的路径(.txt文件名应与图像文件名对应)
label_filename = os.path.splitext(image_filename)[0] + '.txt'
label_filepath = os.path.join(output_dir, label_filename)
# 将注释追加写入到对应的txt文件
with open(label_filepath, 'a') as label_file:
label_file.write(yolo_annotation)
print("COCO格式的标签已经成功转换为YOLO格式!")
全部评论 (0)
还没有任何评论哟~
