数据集格式转换:提供多种脚本,将不同格式的数据集转换为YOLO格式,方便在YOLO框架中使用。
coco2yolo.py:将COCO格式的数据集转换为YOLO格式。
xml2yolo.py:将XML格式的标注文件(如Pascal VOC格式)转换为YOLO格式。
hefei-json2yolo.py、soda2yolo.py、tt100k2yolo.py:分别将特定数据集的标注格式转换为YOLO格式。
bdd2yolo、yolo2BDD:实现BDD数据集与YOLO格式之间的相互转换。
KITTI2YOLO:将KITTI数据集转换为YOLO格式。
数据集分析与可视化:提供工具对目标检测数据集进行分析,并以可视化形式展示结果。
Analysis_dataset:统计目标检测数据集中目标框的位置,并通过热力图展示;统计目标框的长宽比和高度,使用直方图展示。
count_labels.py:统计数据集中各类标签的数量,帮助了解数据分布。
vis_yolo_files.py:可视化YOLO格式的标注文件,便于检查标注的准确性。
数据集操作:提供对数据集进行操作的脚本,方便数据预处理。
copy_images_from_folders.py:从多个文件夹中递归提取特定格式的图片,便于整理数据集。
split_data.py:将数据集按照一定比例划分为训练集和验证集。
多媒体处理:提供将图像序列转换为视频的工具。
这些工具旨在简化深度学习项目中的数据处理流程,提高工作效率。
以下是对代码中功能的逐步注释和讲解。由于项目中的代码文件较多,这里对关键的脚本进行注释和说明。如果需要具体代码逐行注释,请明确告知需要讲解的文件。
coco2yolo.py
示例注释:import os
import json
from tqdm import tqdm # 用于显示进度条
# 设置 COCO 数据集路径
coco_path = "path/to/coco.json" # 指定 COCO 格式数据集文件路径
output_path = "output/yolo/" # YOLO 格式的输出路径
# 加载 COCO 格式数据集
with open(coco_path, 'r') as f:
coco_data = json.load(f)
# 遍历 COCO 数据集的标注信息
for annotation in tqdm(coco_data['annotations']):
# 从 COCO 中获取目标框的位置信息
x, y, width, height = annotation['bbox']
# 将 COCO 的坐标格式转换为 YOLO 格式
# YOLO 格式为 (目标中心 x, y, 宽度, 高度),且所有数值归一化为 [0, 1]
x_center = x + width / 2
y_center = y + height / 2
x_center_norm = x_center / image_width
y_center_norm = y_center / image_height
width_norm = width / image_width
height_norm = height / image_height
# 保存为 YOLO 格式
with open(output_path + f"{image_id}.txt", 'w') as output_file:
output_file.write(f"{class_id} {x_center_norm} {y_center_norm} {width_norm} {height_norm}\n")
os
和 json
模块操作文件,tqdm
用于显示转换进度。.txt
文件,方便目标检测训练。xml2yolo.py
示例注释:import xml.etree.ElementTree as ET # XML 文件解析
import os
# 定义 PASCAL VOC XML 文件路径
xml_path = "path/to/xml"
output_path = "output/yolo/"
# 遍历 XML 文件目录
for xml_file in os.listdir(xml_path):
if not xml_file.endswith(".xml"):
continue
# 解析 XML 文件
tree = ET.parse(os.path.join(xml_path, xml_file))
root = tree.getroot()
# 获取图像宽高
image_width = int(root.find('size/width').text)
image_height = int(root.find('size/height').text)
# 遍历所有目标
for obj in root.findall('object'):
class_name = obj.find('name').text
bbox = obj.find('bndbox')
# 获取目标框坐标
x_min = int(bbox.find('xmin').text)
y_min = int(bbox.find('ymin').text)
x_max = int(bbox.find('xmax').text)
y_max = int(bbox.find('ymax').text)
# 转换为 YOLO 格式
x_center = (x_min + x_max) / 2.0 / image_width
y_center = (y_min + y_max) / 2.0 / image_height
width = (x_max - x_min) / image_width
height = (y_max - y_min) / image_height
# 保存为 YOLO 格式
with open(output_path + xml_file.replace('.xml', '.txt'), 'w') as output_file:
output_file.write(f"{class_name} {x_center} {y_center} {width} {height}\n")
xml.etree.ElementTree
用于解析 XML 格式文件。vis_yolo_files.py
示例注释:import cv2 # 用于图像处理
import os
# 输入 YOLO 文件夹路径和对应图片路径
label_path = "path/to/yolo_labels/"
image_path = "path/to/images/"
# 遍历 YOLO 格式文件
for label_file in os.listdir(label_path):
if not label_file.endswith(".txt"):
continue
# 加载对应图片
image_file = label_file.replace('.txt', '.jpg')
image = cv2.imread(os.path.join(image_path, image_file))
# 可视化目标框
with open(os.path.join(label_path, label_file), 'r') as f:
for line in f.readlines():
# YOLO 格式解析
class_id, x_center, y_center, width, height = map(float, line.strip().split())
x_center *= image_width
y_center *= image_height
width *= image_width
height *= image_height
# 转换为矩形框坐标
x_min = int(x_center - width / 2)
y_min = int(y_center - height / 2)
x_max = int(x_center + width / 2)
y_max = int(y_center + height / 2)
# 绘制矩形框
cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
# 显示图像
cv2.imshow('YOLO Visualization', image)
cv2.waitKey(0)
.txt
文件,提取目标框信息。frames2video.py
示例注释:import cv2
import os
# 定义输入帧路径和输出视频路径
frames_path = "path/to/frames/"
output_video_path = "output/video.mp4"
frame_rate = 30 # 设置帧率
# 获取所有帧文件
frames = sorted(os.listdir(frames_path))
# 获取第一帧的大小
first_frame = cv2.imread(os.path.join(frames_path, frames[0]))
height, width, _ = first_frame.shape
# 初始化视频写入器
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(output_video_path, fourcc, frame_rate, (width, height))
# 写入每一帧
for frame_file in frames:
frame = cv2.imread(os.path.join(frames_path, frame_file))
video_writer.write(frame)
video_writer.release()
print("视频生成完成!")
以上为关键脚本的注释说明,具体需求可以进一步详细分析每个文件的逻辑和实现!