Python 讀取xml數(shù)據(jù),cv2裁剪圖片實(shí)例
下載的數(shù)據(jù)是pascal voc2012的數(shù)據(jù),已經(jīng)有annotation了,不過(guò)是xml格式的,訓(xùn)練的模型是在Google模型的基礎(chǔ)上加了兩層網(wǎng)絡(luò),因此要在原始圖像中裁剪出用于訓(xùn)練的部分圖像。
另外,在原來(lái)給的標(biāo)注框的基礎(chǔ)上,做了點(diǎn)框的移動(dòng)。最后同類目標(biāo)存儲(chǔ)在同一文件夾中。
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
補(bǔ)充知識(shí):python-----截取xml文件畫(huà)框的圖片并保存
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ù),cv2裁剪圖片實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python數(shù)據(jù)提取-lxml模塊
- Python爬蟲(chóng)基于lxml解決數(shù)據(jù)編碼亂碼問(wèn)題
- Python 解析簡(jiǎn)單的XML數(shù)據(jù)
- Python xml、字典、json、類四種數(shù)據(jù)類型如何實(shí)現(xiàn)互相轉(zhuǎn)換
- Python大數(shù)據(jù)之使用lxml庫(kù)解析html網(wǎng)頁(yè)文件示例
- Python 3.x基于Xml數(shù)據(jù)的Http請(qǐng)求方法
- Python基于dom操作xml數(shù)據(jù)的方法示例
- 詳細(xì)解讀Python中解析XML數(shù)據(jù)的方法
- python與xml數(shù)據(jù)的交互詳解
相關(guān)文章
Python學(xué)習(xí)開(kāi)發(fā)之圖形用戶界面詳解
當(dāng)前流行的計(jì)算機(jī)桌面應(yīng)用程序大多數(shù)為圖形化用戶界面(Graphic User Interface,GUI),python也提供了多個(gè)圖形開(kāi)發(fā)界面的庫(kù),這篇文章主要給大家介紹了關(guān)于Python學(xué)習(xí)開(kāi)發(fā)之圖形用戶界面的相關(guān)資料,需要的朋友可以參考下2021-08-08python學(xué)習(xí)手冊(cè)中的python多態(tài)示例代碼
多態(tài)是面向?qū)ο笳Z(yǔ)言的一個(gè)基本特性,多態(tài)意味著變量并不知道引用的對(duì)象是什么,根據(jù)引用對(duì)象的不同表現(xiàn)不同的行為方式,下面使用一個(gè)示例學(xué)習(xí)他的使用方法2014-01-01Python?multiprocessing.value實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)共享的示例
本文介紹了Python中的multiprocessing.value,通過(guò)示例代碼展示了如何使用這個(gè)類實(shí)現(xiàn)多進(jìn)程數(shù)據(jù)共享,以及使用場(chǎng)景和注意事項(xiàng)等內(nèi)容,感興趣的朋友跟隨小編一起看看吧2023-07-07Django框架會(huì)話技術(shù)實(shí)例分析【Cookie與Session】
這篇文章主要介紹了Django框架會(huì)話技術(shù),結(jié)合實(shí)例形式分析了Django框架Cookie與Session相關(guān)使用技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05