XML便签显示在图片上

import os, cv2
import xml.etree.ElementTree as ET
from matplotlib import pyplot as plt

xmls_path = './NEU-val/label/'
imgs_path = './NEU-val/images/'

for images in os.listdir(imgs_path):
    oriname = images.rstrip('.jpg')
    img_path = os.path.join(imgs_path, oriname+'.jpg')
    img = cv2.imread(img_path)

    xml_path = os.path.join(xmls_path, oriname+'.xml')
    tree = ET.parse(xml_path)
    root = tree.getroot()
    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 = (0, 0, 255)
        cv2.rectangle(img, (Xmin, Ymin), (Xmax, Ymax), color, 2)
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, object_name, (Xmin, Ymin-7), font, 0.5, color, 2)
        cv2.imshow('Image', img)
    cv2.waitKey(0)



你可能感兴趣的:(深度学习入门案例)