Yolov3 tensorflow2.0 : xml_to_txt.py

Yolov3 tensorflow2.0 源代碼:https://github.com/YunYang1994/TensorFlow2.0-Examples/tree/master/4-Object_Detection/YOLOV3

Train your own dataset :
數據集: 圖片 + XML文件
在這裏插入圖片描述
訓練前要把 xml 轉換爲 txt 文件,txt文件如以下:

xxx/xxx.jpg 18.19,6.32,424.13,421.83,20 323.86,2.65,640.0,421.94,20 
xxx/xxx.jpg 48,240,195,371,11 8,12,352,498,14
# image_path x_min, y_min, x_max, y_max, class_id  x_min, y_min ,..., class_id 
# make sure that x_max < width and y_max < height

所以我們需要 xml_to_txt 的腳本:

"""
需要修改的地方 :
1、你自己的類別 CLASSES
2、數據集路勁 data_path
3、第12行,生成文件的保存路徑及名稱
"""
import xml.etree.ElementTree as ET
import os

CLASSES = ['apple', 'pear', 'tomato', 'eggplant']

def convert_xml_annotation(data_path, classes):
    xml_dir = []
    for xml in os.listdir(data_path):
        if xml.endswith('.xml'):
            xml_dir.append(xml)
    print("Total xml files : ", len(xml_dir))
    with open("D:/TensorFlow2.0-Examples/4-Object_Detection/YOLOV3/data/dataset/fruits_train.txt", 'w') as f:
        for i in range(len(xml_dir)):
            tree = ET.parse(data_path + xml_dir[i])
            root = tree.getroot()

            # image path
            filename = root.find('filename').text
            image_path = data_path + filename
            annotation = image_path

            # coordinates of label : xmin  ymin  xmax  ymax
            for obj in root.iter('object'):
                difficult = obj.find('difficult').text
                cls = obj.find('name').text
                if cls not in classes or int(difficult) == 1:
                    continue
                cls_id = classes.index(cls)
                bbox = obj.find('bndbox')
                xmin = bbox.find('xmin').text.strip()
                xmax = bbox.find('xmax').text.strip()
                ymin = bbox.find('ymin').text.strip()
                ymax = bbox.find('ymax').text.strip()
                annotation += ' ' + ','.join([xmin, ymin, xmax, ymax,str(cls_id)])
            print(annotation)
            f.write(annotation + "\n")


convert_xml_annotation("D:/SoftWare/easydl2labelImg-master/Data/Fruits/", CLASSES)

處理之後,圖片不需要再進行Resize,就可以直接用來訓練。因爲在項目的實現中,作者已經寫好了相應的img_preprocess函數。

發佈了22 篇原創文章 · 獲贊 5 · 訪問量 1964
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章