-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
to coco #2095
Comments
dear junjun |
在detect()函数中output_file = 'results.txt' # 设定单一输出文件路径 在循环外打开文件,确保是追加模式with open(output_file, 'a') as f: import os 输入文件夹路径(包含多个YOLO格式的结果文件)input_folder = 'path/to/yolo/results' 输出合并的单一结果文件路径output_file = 'combined_results.txt' def combine_yolo_results(input_folder, output_file):
运行合并函数combine_yolo_results(input_folder, output_file) |
junjun 听到请回答 |
1111 |
import os
import json
import glob
import torch
文件夹路径和输出JSON路径
txt_folder = 'runs\detect\exp5\labels' # 读取YOLO格式结果的文件夹路径
output_json = 'runs\detect\exp5\labelscoco_format.json' # 输出COCO格式JSON文件路径
def yolo_to_coco_bbox(bbox, img_width=1, img_height=1):
"""
将 YOLO 坐标转换为 COCO 坐标。
YOLO 格式: (center_x, center_y, width, height) -> COCO 格式: (x_min, y_min, width, height)
"""
x_c, y_c, w, h = bbox
x_min = (x_c - w / 2) * img_width
y_min = (y_c - h / 2) * img_height
width = w * img_width
height = h * img_height
return [x_min, y_min, width, height]
def convert_yolo_to_coco(txt_folder):
"""
读取 YOLO 格式的 txt 文件并转换为 COCO 格式。
"""
coco_results = []
txt_files = glob.glob(os.path.join(txt_folder, "*.txt"))
转换YOLO结果并保存为COCO格式的JSON
coco_results = convert_yolo_to_coco(txt_folder)
with open(output_json, "w") as f:
json.dump(coco_results, f, indent=4)
print(f"COCO format results saved to {output_json}")
The text was updated successfully, but these errors were encountered: