Python讀取VOC中的xml目標框

代碼:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# get annotation object bndbox location
import os
import cv2
try:
    import xml.etree.cElementTree as ET  #解析xml的c語言版的模塊
except ImportError:
    import xml.etree.ElementTree as ET
											         
##get object annotation bndbox loc start 
def GetAnnotBoxLoc(AnotPath):#AnotPath VOC標註文件路徑
    tree = ET.ElementTree(file=AnotPath)  #打開文件,解析成一棵樹型結構
    root = tree.getroot()#獲取樹型結構的根
    ObjectSet=root.findall('object')#找到文件中所有含有object關鍵字的地方,這些地方含有標註目標
    ObjBndBoxSet={} #以目標類別爲關鍵字,目標框爲值組成的字典結構
    for Object in ObjectSet:
        ObjName=Object.find('name').text
        BndBox=Object.find('bndbox')
        x1 = int(BndBox.find('xmin').text)#-1 #-1是因爲程序是按0作爲起始位置的
        y1 = int(BndBox.find('ymin').text)#-1
        x2 = int(BndBox.find('xmax').text)#-1
        y2 = int(BndBox.find('ymax').text)#-1
        BndBoxLoc=[x1,y1,x2,y2]
        if ObjName in ObjBndBoxSet:
        	ObjBndBoxSet[ObjName].append(BndBoxLoc)#如果字典結構中含有這個類別了,那麼這個目標框要追加到其值的末尾
        else:
        	ObjBndBoxSet[ObjName]=[BndBoxLoc]#如果字典結構中沒有這個類別,那麼這個目標框就直接賦值給其值吧
    return ObjBndBoxSet
##get object annotation bndbox loc end

def display(objBox,pic):
    img = cv2.imread(pic)
    
    for key in objBox.keys():
        for i in range(len(objBox[key])):
            cv2.rectangle(img, (objBox[key][i][0],objBox[key][i][1]), (objBox[key][i][2], objBox[key][i][3]), (0, 0, 255), 2)        
            cv2.putText(img, key, (objBox[key][i][0],objBox[key][i][1]), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0), 1)
    cv2.imshow('img',img)
    cv2.imwrite('display.jpg',img)
    cv2.waitKey(0)


if __name__== '__main__':
    pic = r"./VOCdevkit/VOC2007/JPEGImages/000282.jpg"
    ObjBndBoxSet=GetAnnotBoxLoc(r"./VOCdevkit/VOC2007/Annotations/000282.xml")
    print(ObjBndBoxSet)
    display(ObjBndBoxSet,pic)

輸出結果:

{'chair': [[335, 263, 484, 373]], 'person': [[327, 104, 476, 300], [232, 57, 357, 374], [3, 32, 199, 374], [58, 139, 296, 374]]}

圖示:

在這裏插入圖片描述

參考: https://blog.csdn.net/lingyunxianhe/article/details/81808539

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章