VisDrone格式訓練自己FPN數據集

數據格式定義
在這裏插入圖片描述

Xml轉txt

import os
import os.path
import xml.etree.ElementTree as ET
import glob

class_names = ['opium']
xmlpath = 'xml文件路徑'
txtpath = '保存txt文件路徑'


def xml_to_txt(xmlpath, txtpath):
    os.chdir(xmlpath)
    annotations = os.listdir('.')
    annotations = glob.glob(str(annotations) + '*.xml')

    # file_save = 'train' + '.txt'
    # file_txt = os.path.join(txtpath, file_save)
    # f_w = open(file_txt, 'w')

    for i, file in enumerate(annotations):
        in_file = open(file)
        file = file[:-4]
        file_txt = txtpath + file + '.txt'
        f_w = open(file_txt, 'w')
        print(file_txt)

        tree = ET.parse(in_file)
        root = tree.getroot()

        for obj in root.iter('object'):
            current = list()
            name = obj.find('name').text

            class_num = class_names.index(name)

            xmlbox = obj.find('bndbox')

            x1 = int(xmlbox.find('xmin').text)
            x2 = int(xmlbox.find('xmax').text)
            y1 = int(xmlbox.find('ymin').text)
            y2 = int(xmlbox.find('ymax').text)

            f_w.write(str(x1) + ',' + str(y1) + ',' + str(x2-x1) + ',' + str(y2-y1) + ',' + '1,1,0,0' + '\n')

            # f_w.write(x1 + ',' + y1 + ',' + x2 - x1 + ',' + y2 - y1 + ',1,1,-1,-1' + '\n')
            f_w.close


xml_to_txt(xmlpath, txtpath)

對於生成的txt文件,以4的倍數txt文件必須有一個object_category項爲4
在這裏插入圖片描述
否則無法生成.tfrecord文件

參考:
https://blog.csdn.net/qq_36401512/article/details/85261576
https://blog.csdn.net/Angela_qin/article/details/80944604

源碼:https://github.com/yangxue0827/FPN_Tensorflow

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章