代码
import os
import json
import cv2
images_folder = 'images'
labels_folder = 'labels'
output_folder = 'labels_txt'
os.makedirs(output_folder, exist_ok=True)
image_files = [f for f in os.listdir(images_folder) if f.endswith('.jpg')]
for image_file in image_files:
file_prefix = os.path.splitext(image_file)[0]
image_path = os.path.join(images_folder, image_file)
json_path = os.path.join(labels_folder, f"{file_prefix}.json")
image = cv2.imread(image_path)
image_height, image_width, _ = image.shape
with open(json_path, 'r') as file:
data = json.load(file)
yolo_data = []
for shape in data['shapes']:
label = shape['label']
if label == "vehicle":
points = shape['points']
normalized_points = []
for point in points:
normalized_x = point[0] / image_width
normalized_y = point[1] / image_height
normalized_points.extend([normalized_x, normalized_y])
yolo_data.append(f"0 {' '.join(map(str, normalized_points))}")
yolo_file_path = os.path.join(output_folder, f"{file_prefix}.txt")
with open(yolo_file_path, 'w') as file:
file.write("\n".join(yolo_data))
print("所有YOLO格式标签文件已生成。")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人