您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何在Python中讀取xml數(shù)據(jù),小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
1、云計算,典型應用OpenStack。2、WEB前端開發(fā),眾多大型網(wǎng)站均為Python開發(fā)。3.人工智能應用,基于大數(shù)據(jù)分析和深度學習而發(fā)展出來的人工智能本質(zhì)上已經(jīng)無法離開python。4、系統(tǒng)運維工程項目,自動化運維的標配就是python+Django/flask。5、金融理財分析,量化交易,金融分析。6、大數(shù)據(jù)分析。
from __future__ import division import os from PIL import Image import xml.dom.minidom import numpy as np ImgPath = 'C:/Users/Desktop/XML_try/img/' AnnoPath = 'C:/Users/Desktop/XML_try/xml/' ProcessedPath = 'C:/Users/Desktop/CropedVOC/' imagelist = os.listdir(ImgPath) for image in imagelist: image_pre, ext = os.path.splitext(image) imgfile = ImgPath + image xmlfile = AnnoPath + image_pre + '.xml' DomTree = xml.dom.minidom.parse(xmlfile) annotation = DomTree.documentElement filenamelist = annotation.getElementsByTagName('filename') #[<DOM Element: filename at 0x381f788>] 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 savepath = ProcessedPath + objectname if not os.path.exists(savepath): os.makedirs(savepath) bndbox = objects.getElementsByTagName('bndbox') cropboxes = [] for box in bndbox: 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) w = x2 - x1 h = y2 - y1 obj = np.array([x1,y1,x2,y2]) shift = np.array([[0.8,0.8,1.2,1.2],[0.9,0.9,1.1,1.1],[1,1,1,1],[0.7,0.7,1,1],[1,1,1.2,1.2],\ [0.7,1,1,1.2],[1,0.7,1.2,1],[(x1+w*1/3)/x1,(y1+h*1/3)/y1,(x2+w*1/3)/x2,(y2+h*1/3)/y2],\ [(x1-w*1/3)/x1,(y1-h*1/3)/y1,(x2-w*1/3)/x2,(y2-h*1/3)/y2]]) XYmatrix = np.tile(obj,(9,1)) cropboxes = XYmatrix * shift img = Image.open(imgfile) for cropbox in cropboxes: cropedimg = img.crop(cropbox) cropedimg.save(savepath + '/' + image_pre + '_' + str(i) + '.jpg') i += 1
補充知識:python-----截取xml文件畫框的圖片并保存
from __future__ import division import os from PIL import Image import xml.dom.minidom import numpy as np ImgPath = r'D:\tmp\video_wang_mod\01\00022_8253_0021_3\output/' AnnoPath = r'D:\tmp\video_wang_mod\01\00022_8253_0021_3\Annotations/' ProcessedPath = r'D:\tmp\video_wang_mod\01\00022_8253_0021_3\cut/' imagelist = os.listdir(ImgPath) for image in imagelist: image_pre, ext = os.path.splitext(image) imgfile = ImgPath + image print(imgfile) if not os.path.exists(AnnoPath + image_pre + '.xml' ): continue xmlfile = AnnoPath + image_pre + '.xml' 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 savepath = ProcessedPath + objectname if not os.path.exists(savepath): os.makedirs(savepath) bndbox = objects.getElementsByTagName('bndbox') cropboxes = [] for box in bndbox: 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) w = x2 - x1 h = y2 - y1 obj = np.array([x1,y1,x2,y2]) shift = np.array([[1,1,1,1]]) XYmatrix = np.tile(obj,(1,1)) cropboxes = XYmatrix * shift img = Image.open(imgfile) for cropbox in cropboxes: cropedimg = img.crop(cropbox) cropedimg.save(savepath + '/' + image_pre + '_' + str(i) + '.jpg') i += 1
以上就是如何在Python中讀取xml數(shù)據(jù),小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。