Yolo-将coco数据集中的json文件转为txt且解决类别不连续问题
生活随笔
收集整理的這篇文章主要介紹了
Yolo-将coco数据集中的json文件转为txt且解决类别不连续问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
解決問題
1.將coco數據集中,annotations的json文件,讀取,進行轉為ID保持一致的txt文件。
2.解決COCO數據集中,類別不連續的問題
方法
from __future__ import print_function import os, sys, zipfile import jsonclass_num = 0 def 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]h = box[3]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)json_file = '/Data/coco/annotations/instances_val2017.json' # # Object Instance 類型的標注data = json.load(open(json_file, 'r')) ana_txt_save_path = "/Data/coco/test2017" # 保存的路徑 if not os.path.exists(ana_txt_save_path):os.makedirs(ana_txt_save_path)index = 0 cat_id_map = {} for img in data['images']:filename = img["file_name"]img_width = img["width"]img_height = img["height"]img_id = img["id"]ana_txt_name = filename.split(".")[0] + ".txt" # 對應的txt名字,與jpg一致index +=1print(str(index) +' '+str(ana_txt_name))f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')# 保存的文件for ann in data['annotations']:if ann['image_id'] == img_id:if ann['category_id'] not in cat_id_map:cat_id_map[ann['category_id']] = class_numclass_num += 1box = convert((img_width, img_height), ann["bbox"])f_txt.write("%s %s %s %s %s\n" % (cat_id_map[ann['category_id']], box[0], box[1], box[2], box[3]))f_txt.close() fileObject = open('/Data/coco/val_cat_id_map.txt', 'w') # 類別進行重新映射 for cat_id in cat_id_map: fileObject.write(str(cat_id))fileObject.write('\n') fileObject.close()總結
以上是生活随笔為你收集整理的Yolo-将coco数据集中的json文件转为txt且解决类别不连续问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell 基础介绍 [1]
- 下一篇: error while loading