文章目录
- 一、Python批量修改图片文件名和xml文件信息
- 二、python获取图像名称保存成一个txt文件
- 三、xml格式的label转换为yolo-darknet版的label
一、Python批量修改图片文件名和xml文件信息
from PIL import Image
import os.path
import glob
import xml.etree.ElementTree as ET
import xml.dom.minidom
i = 0
xmldir = r"D:\outputs"
imgsdir = r"D:\images"
for xmlfile in os.listdir(xmldir):
xmlname = os.path.splitext(xmlfile)[0]
for pngfile in os.listdir(imgsdir):
pngname = os.path.splitext(pngfile)[0]
if pngname == xmlname:
olddir = os.path.join(os.path.abspath(imgsdir), pngname + ".jpg")
newdir = os.path.join(os.path.abspath(imgsdir), str(i) + ".jpg")
os.rename(olddir, newdir)
print(xmlfile, '----->', str(i) + '.png')
dom = xml.dom.minidom.parse(os.path.join(xmldir, xmlfile))
root = dom.documentElement
root.getElementsByTagName('filename')[0].firstChild.data = str(i) + '.png'
old_xmldir = os.path.join(xmldir, xmlfile)
new_xmldir = os.path.join(xmldir, str(i) + '.xml')
with open(old_xmldir, 'w') as fh:
dom.writexml(fh)
os.rename(old_xmldir, new_xmldir)
i += 1
print('total number is ', i)
效果:
二、python获取图像名称保存成一个txt文件
import sys
sys.path.append('D:\\Program files\\Anaconda\\libs')
import os
import random
data_base_dir = "D:\images"
file_list = []
write_file_name = 'image.txt'
write_file = open(write_file_name, "w")
for file in os.listdir(data_base_dir):
if file.endswith(".jpg"):
write_name = file
file_list.append(write_name)
sorted(file_list)
number_of_lines = len(file_list)
for current_line in range(number_of_lines):
write_file.write(file_list[current_line][:-4] + '\n')
效果:
三、xml格式的label转换为yolo-darknet版的label
import os
import xml.etree.ElementTree as ET
classes = ["play cards"]
def convert(size, box):
print(size, box)
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
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_annotation(image_id):
print(image_id)
in_file = open(r'./data/Annotations/%s.xml' % (image_id), 'rb')
out_file = open('./data/labels/%s.txt' % (image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
image_ids_train = open('./ImageXML.txt').read().strip().split()
for image_id in image_ids_train:
print(image_id)
convert_annotation(image_id)