常用目標檢測的格式轉換腳本文件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文件夾路徑