数据使用Pascal voc 2007 里边的000012图像,将这张图片和对应的xml文件复制到代码同级目录下
import xml.etree.ElementTree as ET import matplotlib.pyplot as plt import matplotlib.image as mpimg # mpimg 用于读取图片 xml_path='000012.xml' tree = ET.parse(xml_path) root=tree.getroot() for object in root.findall('object'): a = [] a.append(object.find('name').text) a.append(int(object.find('bndbox').find('xmin').text)) a.append(int(object.find('bndbox').find('ymin').text)) a.append(int(object.find('bndbox').find('xmax').text)) a.append(int(object.find('bndbox').find('ymax').text)) pic = mpimg.imread('000012.jpg') fig, ax = plt.subplots() ax.add_patch(plt.Rectangle((a[1],a[2]),a[3]-a[1],a[4]-a[2],fill=False,edgecolor='r', linewidth=2.5)) plt.imshow(pic) plt.show()
原图:
GT图: