对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之开始真正编程
Sep 12 Python
以windows service方式运行Python程序的方法
Jun 03 Python
python实现数独算法实例
Jun 09 Python
python 爬取微信文章
Jan 30 Python
python实现闹钟定时播放音乐功能
Jan 25 Python
python format 格式化输出方法
Jul 16 Python
Tensorflow 自定义loss的情况下初始化部分变量方式
Jan 06 Python
Python GUI自动化实现绕过验证码登录
Jan 10 Python
python无序链表删除重复项的方法
Jan 17 Python
python自动化unittest yaml使用过程解析
Feb 03 Python
Django CBV模型源码运行流程详解
Aug 17 Python
Python实现扫码工具的示例代码
Oct 09 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
php使用curl发送json格式数据实例
2013/12/17 PHP
PHP中使用smarty生成静态文件的例子
2014/04/24 PHP
PHP面向对象程序设计(OOP)之方法重写(override)操作示例
2018/12/21 PHP
laravel返回统一格式错误码问题
2019/11/04 PHP
PHP使用PDO 连接与连接管理操作实例分析
2020/04/21 PHP
获取body标签的两种方法
2011/10/13 Javascript
关于jQuery参考实例2.0 用jQuery选择元素
2013/04/07 Javascript
Nodejs实现的一个静态服务器实例
2014/12/06 NodeJs
Javascript中拼接大量字符串的方法
2015/02/05 Javascript
websocket+node.js实现实时聊天系统问题咨询
2017/05/17 Javascript
详解vue嵌套路由-query传递参数
2017/05/23 Javascript
vue如何从接口请求数据
2017/06/22 Javascript
jsTree事件和交互以及插件plugins详解
2017/08/29 Javascript
ES6 javascript的异步操作实例详解
2017/10/30 Javascript
elementUI select组件默认选中效果实现的方法
2019/03/25 Javascript
js实现查询商品案例
2020/07/22 Javascript
跟老齐学Python之不要红头文件(2)
2014/09/28 Python
python查找指定具有相同内容文件的方法
2015/06/28 Python
python字典基本操作实例分析
2015/07/11 Python
详解Python开发中如何使用Hook技巧
2017/11/01 Python
对Python正则匹配IP、Url、Mail的方法详解
2018/12/25 Python
Python中最大递归深度值的探讨
2019/03/05 Python
python flask安装和命令详解
2019/04/02 Python
Python操作Sonqube API获取检测结果并打印过程解析
2019/11/27 Python
基于python生成英文版词云图代码实例
2020/05/16 Python
CSS3实现全景图特效示例代码
2018/03/26 HTML / CSS
HTML5移动端手机网站开发流程
2016/04/25 HTML / CSS
英国赛车、汽车改装和摩托车零件购物网站:Demon Tweeks
2018/10/29 全球购物
人事主管岗位职责范本
2013/12/04 职场文书
奉献爱心演讲稿
2014/09/04 职场文书
2014年图书室工作总结
2014/12/09 职场文书
抗洪救灾感谢信
2015/01/22 职场文书
行政助理岗位职责
2015/02/10 职场文书
八月迷情观后感
2015/06/11 职场文书
Python实现智慧校园自动评教全新版
2021/06/18 Python
如何Python使用re模块实现okenizer
2022/04/30 Python