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 divisionimport osfrom PIL import Imageimport xml.dom.minidomimport 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].dataobjectlist = annotation.getElementsByTagName(’object’)i = 1for 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 - x1h = 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文件畫框的圖片并保存
from __future__ import divisionimport osfrom PIL import Imageimport xml.dom.minidomimport numpy as npImgPath = r’D:tmpvideo_wang_mod0100022_8253_0021_3output/’AnnoPath = r’D:tmpvideo_wang_mod0100022_8253_0021_3Annotations/’ProcessedPath = r’D:tmpvideo_wang_mod0100022_8253_0021_3cut/’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è)參考,也希望大家多多支持好吧啦網(wǎng)。
