本篇文章给大家分享的是有关如何在Python中读取xml数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1、云计算,典型应用OpenStack。2、WEB前端开发,众多大型网站均为Python开发。3.人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python。4、系统运维工程项目,自动化运维的标配就是python+Django/flask。5、金融理财分析,量化交易,金融分析。6、大数据分析。
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数据,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。