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

  

<强>这是我的xml文件结构

        & lt; & # 63; xml version=' 1.0 '编码=皍tf - 8”& # 63;比;   & lt; annotation>   & lt; folder> JPEGImages   & lt; filename> train_2018-05-08_1000.jpg   & lt; path> D: \ all_data \ 2018-05-08 \ JPEGImages \ train_2018-05-08_1000.jpg   & lt; source>   & lt; database> Unknown   & lt;/source>   & lt; size>   & lt; width> 4032 & lt;/width>   & lt; height> 3024 & lt;/height>   & lt; depth> 3 & lt;/depth>   & lt;/size>   & lt; segmented> 0 & lt;/segmented>   & lt; object>   & lt; name> yl - ylhzdhmbbz -广州- hm - 280 g   & lt; pose> Unspecified   & lt; truncated> 0 & lt;/truncated>   & lt; difficult> 0 & lt;/difficult>   & lt; bndbox>   & lt; xmin> 1863 & lt;/xmin>   & lt; ymin> 355 & lt;/ymin>   & lt; xmax> 2512 & lt;/xmax>   & lt; ymax> 902 & lt;/ymax>   & lt;/bndbox>   & lt;/object>   & lt; object>   & lt; name> hy - hybfbgz hz - xcw - 200 ml   & lt; pose> Unspecified   & lt; truncated> 0 & lt;/truncated>   & lt; difficult> 0 & lt;/difficult>   & lt; bndbox>   & lt; xmin> 1076 & lt;/xmin>   & lt; ymin> 1602 & lt;/ymin>   & lt; xmax> 1648 & lt;/xmax>   & lt; ymax> 2105 & lt;/ymax>   & lt;/bndbox>   & lt;/object>   & lt; object>   & lt; name> y - zzyspyz -广州- yw - 245 - ml   & lt; pose> Unspecified   & lt; truncated> 1 & lt;/truncated>   & lt; difficult> 0 & lt;/difficult>   & lt; bndbox>   & lt; xmin> 2017 & lt;/xmin>   & lt; ymin> 2475 & lt;/ymin>   & lt; xmax> 2681 & lt;/xmax>   & lt; ymax> 3024 & lt;/ymax>   & lt;/bndbox>   & lt;/object>   & lt; object>   & lt; name> mn - zgl hz -小王- 250 ml   & lt; pose> Unspecified   & lt; truncated> 0 & lt;/truncated>   & lt; difficult> 0 & lt;/difficult>   & lt; bndbox>   & lt; xmin> 1849 & lt;/xmin>   & lt; ymin> 1207 & lt;/ymin>   & lt; xmax> 2242 & lt;/xmax>   & lt; ymax> 2047 & lt;/ymax>   & lt;/bndbox>   & lt;/object>   & lt; object>   & lt; name> qc - qckf pz - shnt - 268 ml   & lt; pose> Unspecified   & lt; truncated> 0 & lt;/truncated>   & lt; difficult> 0 & lt;/difficult>   & lt; bndbox>   & lt; xmin> 480 & lt;/xmin>   & lt; ymin> 1213 & lt;/ymin>   & lt; xmax> 1308 & lt;/xmax>   & lt; ymax> 1544 & lt;/ymax>   & lt;/bndbox>   & lt;/object>   & lt; object>   & lt; name> wt - wtcyl -广州- nm - 310 ml   & lt; pose> Unspecified   & lt; truncated> 0 & lt;/truncated>   & lt; difficult> 0 & lt;/difficult>   & lt; bndbox>   & lt; xmin> 867 & lt;/xmin>   & lt; ymin> 488 & lt;/ymin>   & lt; xmax> 1527 & lt;/xmax>   & lt; ymax> 938 & lt;/ymax>   & lt;/bndbox>   & lt;/object>      & lt;/annotation>   之前      

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

        __author__=吧侥贰?      进口cv2   进口xml.etree。ElementTree在等   进口操作系统   导入系统   进口lxml   进口shutil      #用户输入文件路径   路径=" E:/test_folder "   image_path=路径+注释“//?与jpg图像路径的结局   label_path=路径+/JPEGImages/?标签路径与xml的结局   min_size=800      def search_jpg_xml (image_dir label_dir):   #发现sepecified所有的文件   image_ext=' jpg '   img=(fn在os.listdir fn (image_dir)如果fn.endswith (image_ext)]   label_ext=' . xml '   标签=(fn在os.listdir fn (label_dir)如果fn.endswith (label_ext)]   返回img标签      def拷贝文件():   如果“Annotations_temp”os.listdir(路径):   shutil.rmtree(路径+“/Annotations_temp”)   如果“JPEGImages_temp”os.listdir(路径):   shutil.rmtree(路径+“/JPEGImages_temp”)   save_annotation_path=路径+“Annotations_temp/?   save_jpg_path=路径+“JPEGImages_temp/?   shutil。copytree(路径+注释“/?save_annotation_path)   shutil。copytree(路径+“/JPEGImages save_jpg_path)   返回save_jpg_path save_annotation_path      def write_xml_jpg (jpg_path annotation_path):   img标签=search_jpg_xml (jpg_path annotation_path)   排序(img)   排序(标签)   打印(img)   打印(标签)   如果“Annotations_1”不是os.listdir(路径):   os.mkdir(路径+“/Annotations_1”)   如果“JPEGImages_1”不是os.listdir(路径):   os.mkdir(路径+“/JPEGImages_1”)   new_image_path=路径+“JPEGImages_1/?   new_annotation_path=路径+“Annotations_1/?   索引文件中列举(标签):   cur_img=cv2.imread (jpg_path + img(指数))   宽度=cur_img.shape [1]   身高=cur_img.shape [0]   如果width

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