对python修改xml文件的节点值方法详解
作者:老司机的诗和远方 发布时间:2021-02-21 19:54:43
标签:python,xml,节点值
这是我的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节点的操作是字符型!!!
来源:https://blog.csdn.net/Touch_Dream/article/details/80435767


猜你喜欢
- 1. 抓取街拍图片街拍图片网址2. 分析街拍图片结构keyword: 街拍pd: atlasdvpf: pcaid: 4916page_nu
- 1、pyecharts绘制饼图(显示百分比)# 导入模块from pyecharts import options as optsfrom
- Python字典的遍历方法有好几种,其中一种是for...in,这个我就不说明,在Python了几乎随处都可见for...in。下面说的这种
- 写完调用天气接口的demo之后,小程序调用天气接口并且渲染在页面,顺便再调用了一下美图的接口API:美图APIurlwxml:<vie
- 新的 Python 版本推出了有趣的新功能。Python 是当今最流行的编程语言之一。它有广泛的领域和应用,从学习计算机科学的基础,到执行复
- 工作时同事间几mb小文件的传输,一般使用QQ或者微信就足够了,但当传输文件几百MB或者几十G时,这种方法的效率就显得不足了。本篇就是简单说明
- 前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。以下文章来源于Python进击者 ,
- 看过 Vue 源码的同学可以知道,<keep-alive>、<transition>、<transition-
- NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供
- MySQL DATE_FORMAT函数简介要将日期值格式化为特定格式,请使用DATE_FORMAT函数。 DATE_FORMAT函数的语法如
- 引言软件开发经历了许多阶段,如需求收集和分析、设计、软件开发、测试和发布。测试是 SDLC 不可或缺的一部分,单元测试是一种可靠的测试类型。
- 昨天面试上来就是一个算法,平时基本的算法还行,结果变个法就不会了。。。感觉应该刷一波Leecode冷静下。。。今天抽空看下。题目就是要求O(
- 通过亲密性原则,我们可以将一个页面中的元素按照某种逻辑理解上的差异划分成不同的元素组合;再通过对齐原则,使这些不同的元素组合在视觉上看起来彼
- MySQL和MariaDB的关系MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。MariaDB
- 一般来说,通过c.Request.FormFile()获取文件的时候,所有内容都全部读到了内存。如果是个巨大的文件,则可能内存会爆掉;且,有
- <div> <a 
- 一、什么是变量在读这篇文章前,我们需要搞懂到底是什么变量,其实一句话就能概括:变量是一个可以保存任何数据类型值的命名占位符。本篇文章将会介绍
- 如下所示:i=0sum1=0sum2=0while i<=100: if i%2==0: sum
- 前言:在转换操作中,我们执行各种操作,例如更改系列的数据类型,将系列更改为列表等。为了执行转换操作,我们有各种有助于转换的功能,例如.ast
- pydev debugger: process 10341 is connecting无法debu今天在Pycharm中debug时无法正常