对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 相关文章推荐
Windows 64位下python3安装nltk模块
Sep 19 Python
使用pip发布Python程序的方法步骤
Oct 11 Python
Python设计模式之策略模式实例详解
Jan 21 Python
详解python中的线程与线程池
May 10 Python
对Python3之方法的覆盖与super函数详解
Jun 26 Python
解决python中用matplotlib画多幅图时出现图形部分重叠的问题
Jul 07 Python
python 函数的缺省参数使用注意事项分析
Sep 17 Python
详解从Django Allauth中进行登录改造小结
Dec 18 Python
利用django model save方法对未更改的字段依然进行了保存
Mar 28 Python
python实现人性化显示金额数字实例详解
Sep 25 Python
Python 微信公众号文章爬取的示例代码
Nov 30 Python
Selenium浏览器自动化如何上传文件
Apr 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
一个简单php扩展介绍与开发教程
2010/08/19 PHP
PHP中如何判断AJAX提交的数据
2012/02/05 PHP
学习thinkphp5.0验证类使用方法
2017/11/16 PHP
PHP hebrev()函数用法讲解
2019/02/21 PHP
jQuery 入门讲解1
2009/04/15 Javascript
用JS实现一个TreeMenu效果分享
2011/08/28 Javascript
jquer之ajaxQueue简单实现代码
2011/09/15 Javascript
关于二级域名下使用一级域名下的COOKIE的问题
2011/11/07 Javascript
IE6-IE9不支持table.innerHTML的解决方法分享
2012/09/14 Javascript
Jquery时间验证和转换工具小例子
2013/07/01 Javascript
JavaScript改变HTML元素的样式改变CSS及元素属性
2013/11/12 Javascript
使用jquery实现IE下按backspace相当于返回操作
2014/03/18 Javascript
js只执行1次的函数示例
2016/07/20 Javascript
jquery获取下拉框中的循环值
2017/02/08 Javascript
Vue应用部署到服务器的正确方式
2017/07/15 Javascript
iscroll.js滚动加载实例详解
2017/07/18 Javascript
微信小程序实现滑动切换自定义页码的方法分析
2018/12/29 Javascript
详解a标签添加onclick事件的几种方式
2019/03/29 Javascript
js 使用ajax设置和获取自定义header信息的方法小结
2020/03/12 Javascript
微信小程序使用GoEasy实现websocket实时通讯
2020/05/19 Javascript
Python脚本实现自动发带图的微博
2016/04/27 Python
Python获取文件所在目录和文件名的方法
2017/01/12 Python
Python基于Matplotlib库简单绘制折线图的方法示例
2017/08/14 Python
Python读写及备份oracle数据库操作示例
2018/05/17 Python
对Python中创建进程的两种方式以及进程池详解
2019/01/14 Python
对Python3之进程池与回调函数的实例详解
2019/01/22 Python
Django集成CAS单点登录的方法示例
2019/06/10 Python
关于Python Tkinter Button控件command传参问题的解决方式
2020/03/04 Python
NBA欧洲商店(西班牙):NBA Europe Store ES
2019/04/16 全球购物
英国名牌服装购物网站:OD’s Designer
2019/09/02 全球购物
市场营销管理制度
2014/01/29 职场文书
公司财务经理岗位职责
2015/04/08 职场文书
青年干部培训班学习心得体会
2016/01/06 职场文书
python b站视频下载的五种版本
2021/05/27 Python
使用Redis做预定库存缓存功能
2022/04/02 Redis
使用Postman测试需要授权的接口问题
2022/06/21 Java/Android