import argparse
from os.path import isfile, isdir, join
import random, yaml
import sys, cv2 ,os
from shutil import copyfile
from distutils.dir_util import copy_tree
from xml.dom import minidom
import xml.etree.ElementTree as ET
from lxml.etree import Element, SubElement, tostring
from xml.dom.minidom import parseString
def txtToXml(txtPath, picPath, xmlPath):
for item in os.listdir(txtPath):
try:
print("item:",item)
picFlie = os.path.join(picPath, item.replace('txt','jpg'))
picName = item.replace('txt','jpg')
if not os.path.exists(picFlie):
picFlie = os.path.join(picPath, item.replace('txt','png'))
picName = item.replace('txt','png')
node_root = Element('annotation')
node_folder = SubElement(node_root, 'folder')
node_folder.text = 'IMAGE'
node_filename = SubElement(node_root, 'filename')
node_filename.text = picName
node_size = SubElement(node_root, 'size')
node_width = SubElement(node_size, 'width')
print("picPath:",picFlie)
img = cv2.imread(picFlie)
height, width, _ = img.shape
node_width.text = '%s' % width
node_height = SubElement(node_size, 'height')
node_height.text = '%s' % height
node_depth = SubElement(node_size, 'depth')
node_depth.text = '%s' % 3
#read txt
path = os.path.join(txtPath, item)
# print("path:",path)
f2 = open(path,"r")
lines = f2.readlines()
bbox = [0,0,0,0]
for line3 in lines:
# for i in inds:
# bbox = dets[i, :4]
# score = dets[i, -1]
#rm [0,0,0,0]
line3 = line3.rstrip("\n")
line = line3.split(' ')
# print("line:",line)
# print("line[3]:",line[3])
bbox[0] = float(line[1])*width - float(line[3])*width/2
bbox[1] = float(line[2])*height - float(line[4])*height/2
bbox[2] = float(line[1])*width + float(line[3])*width/2
bbox[3] = float(line[2])*height + float(line[4])*height/2
if bbox[0]==0 and bbox[2]==0 and bbox[1]==0 and bbox[3]==0 : continue
node_object = SubElement(node_root, 'object')
node_name = SubElement(node_object, 'name')
# if int(line[0]) == 1:
# node_name.text = 'hand'
# else:
# node_name.text = 'goods'
node_name.text = line[0]
node_difficult = SubElement(node_object, 'difficult')
node_difficult.text = '0'
node_bndbox = SubElement(node_object, 'bndbox')
node_xmin = SubElement(node_bndbox, 'xmin')
node_xmin.text = '%s' % bbox[0]
node_ymin = SubElement(node_bndbox, 'ymin')
node_ymin.text = '%s' % bbox[1]
node_xmax = SubElement(node_bndbox, 'xmax')
node_xmax.text = '%s' % bbox[2]
node_ymax = SubElement(node_bndbox, 'ymax')
node_ymax.text = '%s' % bbox[3]
xml = tostring(node_root, pretty_print=True)
save_ml = os.path.join(xmlPath, item.replace('txt', 'xml'))
with open(save_ml, 'wb') as f:
f.write(xml)
except:
pass
def create_dir_not_exist(path):
if not os.path.exists(path):
os.mkdir(path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--picPath', type=str, default = 'dataResult')
parser.add_argument('--txtPath', type=str, default='dataResult/xml')
opt = parser.parse_args()
picPath = opt.picPath
txtPath = opt.txtPath
xmlPath = os.path.join(picPath, "xml")
create_dir_not_exist(xmlPath)
txtToXml(txtPath, picPath, xmlPath)
# allPicPath = "/home/hs/data/0_paimian/batch"
# allXmlPath = "/home/hs/yolov5/yolov5/runs/detect"
# for picFile in os.listdir(allPicPath):
# try:
# txtPath = allXmlPath + '/' + picFile + '/labels'
# xmlPath = allPicPath + '/' + picFile + '/xmls'
# create_dir_not_exist(xmlPath)
# picPath = allPicPath + '/' + picFile
# # print("txtPath:", txtPath)
# # print("xmlPath:", xmlPath)
# # print("picPath:", picPath)
# txtToXml(txtPath, picPath, xmlPath)
# except Exception as e:
# print(e)
# continue
# pass
yoloTxtToXml.py
最新推荐文章于 2024-06-28 11:03:59 发布