将xml格式的数据集标签可视化,在原图的相应位置绘制矩形框和目标类别
原创代码,只需使用PIL库和python中自带的xml.dom.minidom包(本人技术水平有限,使用的方法比较简单粗暴)修改文件路径即可使用。
import os
from xml.dom import minidom # python自带的xml.dom.minidom包
from PIL import ImageDraw, ImageFont, Image # PIL绘制图形、文本
# 定义函数draw_rectangle画矩形框**********************************
def draw_rectangle(name):
doc = minidom.parse("D:/test/" + name) # 打开xml文件,注意文件名格式
imageName = name.replace(".xml", ".jpg") # 替换文件名后缀
image = Image.open("D:/test/" + imageName) # 打开相应的jpg文件
objects = doc.getElementsByTagName("object") # 定义objects
for object in objects: # 遍历标签中所有的物体
name = object.getElementsByTagName("name")[0]
xmin = object.getElementsByTagName("xmin")[0]
ymin = object.getElementsByTagName("ymin")[0]
xmax = object.getElementsByTagName("xmax")[0]
ymax = object.getElementsByTagName("ymax")[0]
# type(xmin) = xml.dom.minidom.Element
x_min = int("".join([str(x1) for x1 in xmin.firstChild.data]))
y_min = int("".join([str(y1) for y1 in ymin.firstChild.data]))
x_max = int("".join([str(x2) for x2 in xmax.firstChild.data]))
y_max = int("".join([str(y2) for y2 in ymax.firstChild.data]))
# type(xmin.firstChild.data) = str type(x_min) = int
# str转int: int("".join([str(x) for x in str]))
draw = ImageDraw.Draw(image) # 加载画图命令
if name.firstChild.data == "truck":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[0], width=3)
# (坐标,颜色,线宽)
draw.text((x_min, y_min - 30), "truck", fill=color[0], font=ttfont)
# (坐标,文本,颜色,字体+大小)
if name.firstChild.data == "car":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[1], width=3)
draw.text((x_min, y_min - 30), "car", fill=color[1], font=ttfont)
if name.firstChild.data == "dangertruck":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[2], width=3)
draw.text((x_min, y_min - 30), "dangertruck", fill=color[2], font=ttfont)
if name.firstChild.data == "special truck":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[3], width=3)
draw.text((x_min, y_min - 30), "special truck", fill=color[3], font=ttfont)
if name.firstChild.data == "bus":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[4], width=3)
draw.text((x_min, y_min - 30), "bus", fill=color[4], font=ttfont)
if name.firstChild.data == "person":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[5], width=3)
draw.text((x_min, y_min - 30), "person", fill=color[5], font=ttfont)
if name.firstChild.data == "motorcycle":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[6], width=3)
draw.text((x_min, y_min - 30), "motorcycle", fill=color[6], font=ttfont)
if name.firstChild.data == "bicycle":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[7], width=3)
draw.text((x_min, y_min - 30), "bicycle", fill=color[7], font=ttfont)
if name.firstChild.data == "animal":
draw.rectangle([x_min, y_min, x_max, y_max], outline=color[8], width=3)
draw.text((x_min, y_min - 30), "animal", fill=color[8], font=ttfont)
image.save("D:/Image/" + imageName) # 保存图像,C盘根目录无权限
# 定义函数draw_rectangle画矩形框**********************************
file_dir = "D:/test" # path
file_name = os.listdir(file_dir) # 遍历文件获取全部文件名
color = [(102, 128, 102), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255),
(0, 128, 0), (0, 0, 0)] # 控制矩形框颜色
ttfont = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", 30) # 控制文本字体(电脑自带字体库)与大小
for name in file_name: # 循环语句(注意:)
if name.find(".xml") >= 0: # 仅读取xml文件
draw_rectangle(name)