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