import os
import json
# 定义类别索引映射
class_indices = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '\\': 10}
# 读取并解析train.txt文件
def parse_annotations(annotations_file_path):
with open(annotations_file_path, 'r',encoding='utf-8') as file:
lines = file.readlines()
return lines
# 将PaddleOCR标注转换为YOLOv5格式
def convert_paddle_to_yolov5(image_name, paddle_annotations):
yolo_annotations = []
for obj in paddle_annotations:
cls_index = class_indices[obj['transcription']]
points = obj['points']
x1, y1, x2, y2 = points[0][0], points[0][1], points[2][0], points[2][1]
# 计算边界框的中心和宽高,并转换为归一化坐标
# 假设图像尺寸为原始尺寸,这里用640x480作为示例
img_width, img_height = 640, 480
bbox_width = x2 - x1
bbox_height = y2 - y1
x_center = x1 + bbox_width / 2
y_center = y1 + bbox_height / 2
# 归一化
x_center /= img_width
y_center /= img_height
bbox_width /= img_width
bbox_height /= img_height
# 添加到YOLOv5格式的注释列表
yolo_annotations.append(f"{cls_index} {x_center} {y_center} {bbox_width} {bbox_height}")
return yolo_annotations
# 处理train.txt中的每张图像
def process_train_file(annotations_file_path, output_dir):
annotations_lines = parse_annotations(annotations_file_path)
for line in annotations_lines:
print(line)
if line.strip(): # 确保不处理空行
image_path, json_str = line.strip().split("\t")
image_name = os.path.basename(image_path).split('.')[0].split('/')[-1]
paddle_annotations = json.loads(json_str)
yolo_annotations = convert_paddle_to_yolov5(image_name, paddle_annotations)
# 写入YOLOv5格式的标注文件
output_file_path = os.path.join(output_dir, f"{image_name}.txt")
with open(output_file_path, 'w',encoding='utf8') as output_file:
for annotation in yolo_annotations:
output_file.write(annotation + '\n')
# 指定train.txt文件路径和输出目录
annotations_file_path = r'E:\01train_and_result\shuiwenju\train\6月\train_data_0613\det\train.txt'
output_dir = 'output_annotations'
# 确保输出目录存在
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 开始处理
process_train_file(annotations_file_path, output_dir)
print("转换完成。")
09-06
7782
09-05