常用目标检测的格式转换脚本文件txt,json等
文章目录
- 常用目标检测的格式转换脚本文件txt,json等
- 前言
- 一、json格式转yolo的txt格式
- 二、yolov8的关键点labelme打的标签json格式转可训练的txt格式
- 三、yolo的目标检测txt格式转coco数据集标签的json格式
- 四、根据yolo的目标检测训练的最好权重推理图片
- 五、根据yolo标签的txt文件提取某一个特征类别的标签, 并在原图上进行绘制
- 六、根据yolo标签的txt文件提取某一个特征类别的标签, 并在原图上进行绘制
前言
⭐️ ⭐️ ⭐️ 还在完善中
⭐️ ⭐️ ⭐️
本节主要介绍在目标检测领域内,常用的格式转换脚本
一、json格式转yolo的txt格式
json格式的目标检测数据集标签格式转yolo目标检测的标签txt的格式
代码如下(示例): 主要修改 classes, json_folder_path, output_dir
"""
目标检测的 json --> 转为 yolo的txt
"""
import json
import osdef convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[2]) / 2.0y = (box[1] + box[3]) / 2.0w = box[2] - box[0]h = box[3] - box[1]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def decode_json(json_path, output_dir, classes):with open(json_path, 'r', encoding='utf-8') as f:data = json.load(f)base_name = os.path.splitext(os.path.basename(data['imagePath']))[0]txt_path = os.path.join(output_dir, base_name + '.txt')with open(txt_path, 'w', encoding='utf-8') as txt_file:for shape in data['shapes']:if shape['shape_type'] == 'rectangle':label = shape['label']if label not in classes:continuecls_id = classes.index(label)points = shape['points']x1, y1 = points[0]x2, y2 = points[1] # Assuming the points are diagonalbb = convert((data['imageWidth'], data['imageHeight']), [x1, y1, x2, y2])txt_file.write(f"{cls_id} {' '.join(map(str, bb))}\n")if __name__ == "__main__":# 指定YOLO类别classes = ['loose', 'un-loose'] # 根据实际类别名称进行修改# JSON格式的标签文件路径json_folder_path = './json' # 替换为实际的JSON文件夹路径