在训练yolo的过程中,难免涉及标注的数据格式转化,经过了几次修改和迭代,最终把转化代码跟大家一起分享。
先把xml转txt部分的代码分享一下,py_convert_xml2txt.py:
# -*- coding:utf-8 -*-
import os
import shutil
import cv2
import numpy as np
import xml.etree.ElementTree as ET
wordname_coco = ['person','bicycle','car','motorbike','aeroplane','bus','train','truck','boat','traffic light','fire hydrant',
'stop sign','parking meter','bench','bird','cat','dog','horse','sheep','cow','elephant','bear', 'zebra','giraffe',
'backpack','umbrella','handbag','tie','suitcase','frisbee','skis','snowboard','sports ball','kite','baseball bat',
'baseball glove','skateboard','surfboard','tennis racket','bottle','wine glass','cup','fork','knife','spoon','bowl',
'banana','apple','sandwich','orange','broccoli','carrot','hot dog','pizza','donut','cake','chair','sofa','potted plant',
'bed','dining table','toilet','tv','laptop','mouse','remote','keyboard','cell phone','microwave','oven','toaster','sink',
'refrigerator','book','clock','vase','scissors','teddy bear','hair drier','toothbrush']
wordname_voc = ['aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable','dog','horse',
'motorbike','person','pottedplant','sheep','sofa','sheep','train','tvmonitor']
wordname_zkrc = ['person','car','cat','dog']
#person car truck cat dog
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_xml2txt_voc():
#text_dir = './data/JPEGImages/'
xml_dir = "./data/LR_person_car_dataset/train/"
imagedir = "./data/LR_person_car_dataset/train/"
out_txt_dir = './InfraredData/txt/'
out_temp_txt_dir = './InfraredData/temp_Txt/'
out_images_dir = './InfraredData/Images/'
title = "20240823_4_"
count = 0
list = os.listdir(xml_dir) # 列出文件夹下所有的目录与文件
for i in range(0, len(list)):
name = os.path.splitext(list[i])
if name[1] == '.xml':
print(list[i])
#changeTitle = title + str(i + 1) + "_"
xml_path = xml_dir + list[i]
jpg_path = imagedir + name[0] + '.jpg