Python 读取xml数据,cv2裁剪图片实例

所属分类: 脚本专栏 / python 阅读数: 1769
收藏 0 赞 0 分享

下载的数据是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

以上这篇Python 读取xml数据,cv2裁剪图片实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多