Python 读取xml数据,cv2裁剪图片实例
作者:竹子熊猫 时间:2021-07-18 00:08:08
下载的数据是pascal voc2012的数据,已经有annotation了,不过是xml格式的,训练的模型是在Google模型的基础上加了两层网络,因此要在原始图像中裁剪出用于训练的部分图像。
另外,在原来给的标注框的基础上,做了点框的移动。最后同类目标存储在同一文件夹中。
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
来源:https://blog.csdn.net/summermaoz/article/details/69352088
标签:Python,xml,cv2,图片
0
投稿
猜你喜欢
ASP函数过滤数组中重复数据方法
2010-01-02 20:32:00
Anaconda和Pycharm的安装配置教程分享
2022-10-22 07:12:30
教你用Python3+mysql8.0搭建Django框架
2024-01-26 23:19:39
MySQL 错误处理例子[译]
2024-01-25 09:25:10
交互设计实用指南系列(7)–避免迷路
2010-01-23 09:52:00
python基于twisted框架编写简单聊天室
2021-12-12 17:56:51
解决python打不开文件(文件不存在)的问题
2021-10-15 02:39:46
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2023-07-07 02:41:30
ASP连接MSSQL的错误: 拒绝访问
2008-11-23 20:40:00
MySQL数据库事务隔离级别详解
2024-01-26 12:34:03
pyautogui自动化控制鼠标和键盘操作的步骤
2022-04-29 21:35:14
python使用py2neo查询Neo4j的节点、关系及路径
2023-02-01 18:11:52
详解在spring中使用JdbcTemplate操作数据库的几种方式
2024-01-29 09:29:50
anaconda升级sklearn版本的实现方法
2021-08-07 02:28:18
详解解决Python memory error的问题(四种解决方案)
2021-03-11 14:59:11
MySQL 5.0触发器参考教程第1/4页
2024-01-23 17:03:58
python 实现客户端与服务端的通信
2023-11-19 01:28:33
python判断列表为空的三种方法总结
2022-09-28 21:46:46
asp如何制作一个WAP手机的WML网页?
2010-06-29 21:21:00
asp如何用ADO批量更新记录?
2010-06-10 18:42:00