说明
在目标检测数据集处理中,我们经常会遇到标签之间不同格式的转化,以下介绍JSON格式的标签转YOLO格式。
格式
json格式标签是通过labelme软件进行标注,实现转为txt格式,即保存归一化后的中心点坐标和归一化后检测框长和宽。
代码
import os
import json
# labelme标注的json标签文件目录和保存生成的txt标签的文件夹
dir_json = r'F:\文件B_数集文件\B_葡萄数据\07-230612葡萄\00-标注文件/'
dir_txt = r'F:\文件B_数集文件\B_葡萄数据\07-230612葡萄\00-标注文件/txt/' # txt存储目录
os.mkdir(dir_txt)
def json2txt(path_json, path_txt): # 可修改生成格式
with open(path_json, 'r',encoding='utf-8') as path_json:
jsonx = json.load(path_json)
with open(path_txt, 'w+') as ftxt:
shapes = jsonx['shapes']
#获取图片长和宽
width=jsonx['imageWidth']
height=jsonx['imageHeight']
for shape in shapes:
#获取矩形框两个角点坐标
x1 = shape['points'][0][0]
y1 = shape['points'][0][1]
x2 = shape['points'][1][0]
y2 = shape['points'][1][1]
if shape['label']=='grape': # 对应类别转为数字代号
cat=0
else:
cat=1
#对结果进行归一化
dw = 1. / width
dh = 1. / height
x=dw *(x1+x2)/2
y=dh *(y1+y2)/2
w=dw *abs(x2-x1)
h = dh * abs(y2 - y1)
yolo = f"{cat} {x} {y} {w} {h} \n"
ftxt.writelines(yolo)
list_json = os.listdir(dir_json)
for cnt, json_name in enumerate(list_json):
if os.path.splitext(json_name)[-1] == ".json":
path_json = dir_json + json_name
path_txt = dir_txt + json_name.replace('.json', '.txt')
json2txt(path_json, path_txt)
txt标签可视化
import cv2
import numpy as np
# 定义可视化函数
def visualize(image_path, label_path, class_names):
# 读取图片
image = cv2.imread(image_path)
# 获取图片的大小
height, width, _ = image.shape
# 读取标签文件
with open(label_path, "r") as f:
lines = f.readlines()
# 遍历每个标签
for line in lines:
# 解析标签
class_id, x, y, w, h = map(float, line.split())
class_name = class_names[int(class_id)]
# 计算 bounding box 的坐标
left = int((x - w / 2) * width)
top = int((y - h / 2) * height)
right = int((x + w / 2) * width)
bottom = int((y + h / 2) * height)
# 绘制 bounding box
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)
# 绘制类别名称
text_size, _ = cv2.getTextSize(class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
cv2.putText(image, class_name, (left, top - text_size[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# 显示图片
cv2.imshow("visualization", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 调用函数,可视化 YOLO 标签(请替换为你的图片路径、标签路径和类别名称列表)
visualize(r"F:\文件B_数集文件\B_葡萄数据\07-230612葡萄\01-标注文件六月第三周\0620_2_1.png", r"F:\文件B_数集文件\B_葡萄数据\07-230612葡萄\0620_2_1.txt", ["类别1", "类别2", "类别3"])