用xml.etree.ElementTree库来解析xml文件
1.解析:ET.parse()
2.获取根节点:getroot()
3.找节点用find() 和findall()
4.
.text 是内容 #本代码用到.text
.tag 是题目
.attrib 是属性
5.用cv2绘图
提取出来坐标,然后rectangle(图片,左上角坐标,右下角坐标,框颜色,框线粗细)
把目标名字输出:putTEXT(图片,目标名字,起始坐标,字体,字体大小,字体颜色,粗细)
其他:两个find()定位到
没用过,但看起来差不多。效果都一样,熟悉一个就行。
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os,cv2
xml_file='C:/Users/nansbas/Desktop/01_000002_01244-01086_0939-0989.xml'
tree=ET.parse(xml_file)
root=tree.getroot()
imgfile='C:/Users/nansbas/Desktop/01_000002_01244-01086_0939-0989.jpg'
im = cv2.imread(imgfile)
for object in root.findall('object'):
object_name=object.find('name').text
Xmin=int(object.find('bndbox').find('xmin').text)
Ymin=int(object.find('bndbox').find('ymin').text)
Xmax=int(object.find('bndbox').find('xmax').text)
Ymax=int(object.find('bndbox').find('ymax').text)
color = (4, 250, 7)
cv2.rectangle(im,(Xmin,Ymin),(Xmax,Ymax),color,2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(im, object_name, (Xmin,Ymin - 7), font, 0.5, (6, 230, 230), 2)
cv2.imshow('01',im)
cv2.imwrite('C:/Users/nansbas/Desktop/02.jpg', im)
结果如下:
上面是按单个标签进行处理,进一步的,按xml文件夹进行处理。下面是另一个xml解析库的实现。
xml.dom.minidom 同样是树状结构解析的。
# -*- coding: utf-8 -*-
from __future__ import division
import os
import xml.dom.minidom
import cv2
def read_xml(ImgPath, AnnoPath, Savepath):
imagelist = os.listdir(AnnoPath)
for image in imagelist:
image_pre, ext = os.path.splitext(image)
imgfile = ImgPath + '/' + image_pre + '.jpg'
xmlfile = AnnoPath + '/' + image_pre + '.xml'
im = cv2.imread(imgfile)
DomTree = xml.dom.minidom.parse(xmlfile)
annotation = DomTree.documentElement
filenamelist = annotation.getElementsByTagName('filename')
filename = filenamelist[0].childNodes[0].data
objectlist = annotation.getElementsByTagName('object')
i = 1
for objects in objectlist:
namelist = objects.getElementsByTagName('name')
objectname = namelist[0].childNodes[0].data
bndbox = objects.getElementsByTagName('bndbox')
for box in bndbox:
try:
x1_list = box.getElementsByTagName('xmin')
x1 = int(x1_list[0].childNodes[0].data)
y1_list = box.getElementsByTagName('ymin')
y1 = int(y1_list[0].childNodes[0].data)
x2_list = box.getElementsByTagName('xmax')
x2 = int(x2_list[0].childNodes[0].data)
y2_list = box.getElementsByTagName('ymax')
y2 = int(y2_list[0].childNodes[0].data)
minX = x1
minY = y1
maxX = x2
maxY = y2
if(i % 9 == 0):
color = (128,0,0)
elif (i % 9 == 1):
color = (153, 51, 0)
elif (i % 9 == 2):
color = (255, 204, 0)
elif (i % 9 == 3):
color = (0, 51, 0)
elif (i % 9 == 4):
color = (51, 204, 204)
elif (i % 9 == 5):
color = (128, 0, 128)
elif (i % 9 == 6):
color = (0, 255, 255)
elif (i % 9 == 7):
color = (60, 179, 113)
elif (i % 9 == 8):
color = (255, 127, 80)
elif (i % 9 == 9):
color = (0, 255, 0)
cv2.rectangle(im,(Xmin,Ymin),(Xmax,Ymax),color,2)
# path = Savepath + '/' + image_pre + '_New' + '.jpg'
path = Savepath + '/' + image_pre + '.jpg'
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(im, objectname, (minX,minY - 7), font, 0.5, (0, 0, 255), 1)
cv2.imwrite(path, im)
i += 1
except Exception, e:
print e
read_xml('C:/Users/nansbas/Desktop/01','C:/Users/nansbas/Desktop/01','C:/Users/nansbas/Desktop/01')
结果如下