时间:2021-05-22
下载的数据是pascal voc2012的数据,已经有annotation了,不过是xml格式的,训练的模型是在Google模型的基础上加了两层网络,因此要在原始图像中裁剪出用于训练的部分图像。
另外,在原来给的标注框的基础上,做了点框的移动。最后同类目标存储在同一文件夹中。
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].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 divisionimport osfrom PIL import Imageimport xml.dom.minidomimport numpy as npImgPath = 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数据,cv2裁剪图片实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
使用cv2读取图片时,输出图片形状大小时出现报错“'NoneType'objecthasnoattributeshape”,后来排查发现读取图片的返回值imag
PIL和cv2是python中两个常用的图像处理库,PIL一般是anaconda自带的,cv2是opencv的python版本。base64在网络传输图片的时候
cv2方式:#-*-coding:UTF-8-*-importcv2"""cv2模块--图片的读入和显示"""image_path="D:/PycharmPro
我是在做行人检测中需要将一段视频变为图片数据集,然后想将视频每秒钟的图片提取出来。语言:python所需要的库:cv2,numpy(自行安装)opencv中提供
通过查看书籍,自己总结了一下,怎样用python代码实现调用笔记本摄像头的功能。这主要是通过opencv中cv2模块来实现这个功能。其中是调用cv2中的函数,比