对python修改xml文件的节点值方法详解


Posted in Python onDecember 24, 2018

这是我的xml文件结构

<?xml version='1.0' encoding='utf-8'?>
<annotation>
 <folder>JPEGImages</folder>
 <filename>train_2018-05-08_1000.jpg</filename>
 <path>D:\all_data\2018-05-08\JPEGImages\train_2018-05-08_1000.jpg</path>
 <source>
 <database>Unknown</database>
 </source>
 <size>
 <width>4032</width>
 <height>3024</height>
 <depth>3</depth>
 </size>
 <segmented>0</segmented>
 <object>
 <name>yl-ylhzdhmbbz-gz-hm-280g</name>
 <pose>Unspecified</pose>
 <truncated>0</truncated>
 <difficult>0</difficult>
 <bndbox>
  <xmin>1863</xmin>
  <ymin>355</ymin>
  <xmax>2512</xmax>
  <ymax>902</ymax>
 </bndbox>
 </object>
 <object>
 <name>hy-hybfbgz-hz-xcw-200ml</name>
 <pose>Unspecified</pose>
 <truncated>0</truncated>
 <difficult>0</difficult>
 <bndbox>
  <xmin>1076</xmin>
  <ymin>1602</ymin>
  <xmax>1648</xmax>
  <ymax>2105</ymax>
 </bndbox>
 </object>
 <object>
 <name>ys-zzyspyz-gz-yw-245ml</name>
 <pose>Unspecified</pose>
 <truncated>1</truncated>
 <difficult>0</difficult>
 <bndbox>
  <xmin>2017</xmin>
  <ymin>2475</ymin>
  <xmax>2681</xmax>
  <ymax>3024</ymax>
 </bndbox>
 </object>
 <object>
 <name>mn-zgl-hz-cmw-250ml</name>
 <pose>Unspecified</pose>
 <truncated>0</truncated>
 <difficult>0</difficult>
 <bndbox>
  <xmin>1849</xmin>
  <ymin>1207</ymin>
  <xmax>2242</xmax>
  <ymax>2047</ymax>
 </bndbox>
 </object>
 <object>
 <name>qc-qckf-pz-shnt-268ml</name>
 <pose>Unspecified</pose>
 <truncated>0</truncated>
 <difficult>0</difficult>
 <bndbox>
  <xmin>480</xmin>
  <ymin>1213</ymin>
  <xmax>1308</xmax>
  <ymax>1544</ymax>
 </bndbox>
 </object>
 <object>
 <name>wt-wtcyl-gz-nm-310ml</name>
 <pose>Unspecified</pose>
 <truncated>0</truncated>
 <difficult>0</difficult>
 <bndbox>
  <xmin>867</xmin>
  <ymin>488</ymin>
  <xmax>1527</xmax>
  <ymax>938</ymax>
 </bndbox>
 </object>

</annotation>

现在想实现的是修改图像的size和目标

__author__ = 'Sam'
 
import cv2
import xml.etree.ElementTree as ET
import os
import sys
import lxml
import shutil
 
#user input files path
path="E:/test_folder"
image_path = path + "/Annotations/" #image path with .jpg ending
label_path = path + "/JPEGImages/" #label path with .xml ending
min_size=800
 
def search_jpg_xml(image_dir,label_dir):
  #find out all of sepecified file
  image_ext='.jpg'
  img=[fn for fn in os.listdir(image_dir) if fn.endswith(image_ext)]
  label_ext='.xml'
  label=[fn for fn in os.listdir(label_dir) if fn.endswith(label_ext)]
  return img, label
 
def copyfile():
  if "Annotations_temp" in os.listdir(path):
    shutil.rmtree(path+"/Annotations_temp")
  if "JPEGImages_temp" in os.listdir(path):
    shutil.rmtree(path+"/JPEGImages_temp")
  save_annotation_path=path+"/Annotations_temp/"
  save_jpg_path=path+"/JPEGImages_temp/"
  shutil.copytree(path + "/Annotations",save_annotation_path)
  shutil.copytree(path + "/JPEGImages", save_jpg_path)
  return save_jpg_path ,save_annotation_path
 
def write_xml_jpg(jpg_path,annotation_path):
  img,label=search_jpg_xml(jpg_path,annotation_path)
  sorted(img)
  sorted(label)
  print(img)
  print(label)
  if "Annotations_1" not in os.listdir(path):
    os.mkdir(path+"/Annotations_1")
  if "JPEGImages_1" not in os.listdir(path):
    os.mkdir(path+"/JPEGImages_1")
  new_image_path=path+"/JPEGImages_1/"
  new_annotation_path=path+"/Annotations_1/"
  for index,file in enumerate(label):
    cur_img = cv2.imread(jpg_path+img[index])
    width=cur_img.shape[1]
    height=cur_img.shape[0]
    if width<height:
      new_width=min_size
      new_height=int(min_size*height/width)
      w_ratio=new_width/width
      h_ratio=new_height/height
    elif width>height:
      new_width=int(min_size*width/height)
      new_height=min_size
      w_ratio=new_width/width
      h_ratio=new_height/height
    elif width==height:
      new_width=min_size
      new_height=min_size
      w_ratio=new_width/width
      h_ratio=new_height/height
    cur_img = cv2.resize(cur_img, (new_width, new_height))
    cv2.imwrite(new_image_path+img[index],cur_img)
    cur_xml = ET.parse(annotation_path+file)
    root = cur_xml.getroot()
    for node in root:
      if node.tag=='size':
        node[0].text=str(new_width)
        node[1].text=str(new_height)
      elif node.tag=='object':
         xmin=int(node[4][0].text)#bbox position
         ymin=int(node[4][1].text)
         xmax=int(node[4][2].text)
         ymax=int(node[4][3].text)
         node[4][0].text=str(int(xmin*w_ratio))
         node[4][1].text=str(int(ymin*h_ratio))
         node[4][2].text=str(int(xmax*w_ratio))
         node[4][3].text=str(int(ymax*h_ratio))
    cur_xml.write(new_annotation_path+file)
  shutil.rmtree(path+"/JPEGImages_temp")
  shutil.rmtree(path+"/Annotations_temp")
 
 
if __name__ == "__main__":
  jpg_path,annotation_path=copyfile()
  write_xml_jpg(jpg_path,annotation_path)

最关键语句是:

node[4][3].text=str(int(ymax*h_ratio)),注意xml节点的操作是字符型!!!

以上这篇对python修改xml文件的节点值方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
深入Python函数编程的一些特性
Apr 13 Python
详解Python中的Descriptor描述符类
Jun 14 Python
Python中实现变量赋值传递时的引用和拷贝方法
Apr 29 Python
Python中的函数作用域
May 07 Python
基于python的socket实现单机五子棋到双人对战
Mar 24 Python
python取均匀不重复的随机数方式
Nov 27 Python
Python迭代器模块itertools使用原理解析
Dec 11 Python
如何表示python中的相对路径
Jul 08 Python
Python如何设置指定窗口为前台活动窗口
Aug 12 Python
python实现视频压缩功能
Dec 18 Python
tensorflow与numpy的版本兼容性问题的解决
Jan 08 Python
PYTHON InceptionV3模型的复现详解
May 06 Python
用Python读取几十万行文本数据
Dec 24 #Python
python实现flappy bird小游戏
Dec 24 #Python
python实现Flappy Bird源码
Dec 24 #Python
python3安装speech语音模块的方法
Dec 24 #Python
对Python 语音识别框架详解
Dec 24 #Python
python抓取网页内容并进行语音播报的方法
Dec 24 #Python
解决pyttsx3无法封装的问题
Dec 24 #Python
You might like
解决中英文字符串长度问题函数
2007/01/16 PHP
在Thinkphp中使用ajax实现无刷新分页的方法
2016/10/25 PHP
PHP实现的62进制转10进制,10进制转62进制函数示例
2019/06/06 PHP
google jQuery 引用文件,jQuery 引用地址集合(jquery 1.2.6至jquery1.5.2)
2011/04/24 Javascript
A标签触发onclick事件而不跳转的多种解决方法
2013/06/27 Javascript
鼠标经过显示二级菜单js特效
2013/08/13 Javascript
JavaScript中定时控制Throttle、Debounce和Immediate详解
2016/11/17 Javascript
javascript 判断一个对象为数组的方法
2017/05/03 Javascript
BackBone及其实例探究_动力节点Java学院整理
2017/07/14 Javascript
web前端vue之CSS过渡效果示例
2018/01/10 Javascript
通过GASP让vue实现动态效果实例代码详解
2019/11/24 Javascript
Vue 如何使用props、emit实现自定义双向绑定的实现
2020/06/05 Javascript
JavaScript实现沿五角星形线摆动的小圆实例详解
2020/07/28 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
[03:49]辉夜杯现场龙骑士COSER秀情商“我喜欢芬队!”
2015/12/27 DOTA
[02:00]DAC2018主宣传片——龙征四海,剑问东方
2018/03/20 DOTA
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
python3库numpy数组属性的查看方法
2018/04/17 Python
python利用微信公众号实现报警功能
2018/06/10 Python
Pycharm中使用git进行合作开发的教程详解
2020/11/17 Python
在HTML5中使用MathML数学公式的简单讲解
2016/02/19 HTML / CSS
HTML5本地存储之IndexedDB
2017/06/16 HTML / CSS
SKECHERS斯凯奇中国官网:来自美国的运动休闲品牌
2018/11/14 全球购物
架构师岗位职责
2013/11/18 职场文书
餐饮营销方案
2014/02/23 职场文书
汽车维修求职信
2014/06/15 职场文书
个人四风问题对照检查材料
2014/09/26 职场文书
计划生育证明格式及范本
2014/10/09 职场文书
2014年小学少先队工作总结
2014/12/18 职场文书
少先队工作总结2015
2015/05/13 职场文书
车辆安全隐患排查制度
2015/08/05 职场文书
素质教育培训心得体会
2016/01/19 职场文书
nginx 反向代理之 proxy_pass的实现
2021/03/31 Servers
巧用 -webkit-box-reflect 倒影实现各类动效(小结)
2021/04/22 HTML / CSS
日本十大血腥动漫,那些被禁播的动漫盘点
2022/03/21 日漫
MySQL中LAG()函数和LEAD()函数的使用
2022/08/14 MySQL