切割VOC的图像和他的标签

切割VOC的图像

  • 背景
  • 代码

背景

图像及对应xml文件切割

在这里插入图片描述

需要将图像切分为819,819的子图像

在这里插入图片描述
在这里插入图片描述

代码

import os
import xml.etree.ElementTree as ET
from PIL import Imagedef crop_one_image_and_xml(image_path, annotation_path, output_path, filename):# -*- coding: encoding -*-# 原图文件和标注文件的路径# image_path = 'path/to/original/image'# annotation_path = 'path/to/original/annotation'# 小块图片和标注文件保存的路径# output_path = 'path/to/output/folder'# 小块图片的尺寸和裁剪策略block_size = (819, 819)stride = (160, 160)# 解析原图的标注文件tree = ET.parse(annotation_path)root = tree.getroot()  # 获取ElementTree对象的根节点。根据XML文件的结构,每个XML文件都有一个根节点。getroot()方法返回这个根节点,并将其保存在变量root中。这个方法返回的是一个 Element 类型的对象,它代表了整个XML文件的根节点。# 获取原图的大小size = (int(root.find('size/width').text), int(root.find('size/height').text))  # 获取XML文件中 size/width 这个节点的值,并将其转换为整数类型。# 计算小块图片的行数和列数rows = 4   # 3200的那一行cols = 2   # 1638的那一行# 遍历每个小块图片的位置for row in range(rows):for col in range(cols):# 计算小块图片的左上角在原图中的位置坐标x0 = col * stride[0]y0 = row * stride[1]x1 = x0 + block_size[0]y1 = y0 + block_size[1]# 如果超出原图边界,则调整小块图片的大小和位置if x1 > size[0]:x1 = size[0]x0 = x1 - block_size[0]if y1 > size[1]:y1 = size[1]y0 = y1 - block_size[1]# 创建小块图片并保存block_image = Image.open(image_path).crop((x0, y0, x1, y1))block_filename = f'{filename}_{x0, y0}-{x1, y1}.bmp'block_image.save(os.path.join(output_path, block_filename))# 检查是否含有标注框,True代表没有标注框,False就代表有标注框check_bndbox_is_none = True# 创建小块图片的标注文件并保存'''ET 是 ElementTree 库的别名或者是导入的模块,使用 ET.Element('annotation') 创建了一个名为 annotation 的元素,它代表了这个XML文档的根节点。Element() 方法会返回一个 Element 类型的对象,该对象代表了一个XML元素。这个XML文档的根节点是 annotation,它是这个XML文档的最上层元素,它可以包含其它元素或者属性,而其它元素或者属性都是它的子元素或者子属性。你可以通过在这个根元素上调用方法,如 .append() 或 .attrib,来添加子元素或者属性。'''block_root = ET.Element('annotation')folder_elem = ET.Element('folder')folder_elem.text = str('Images')block_root.append(folder_elem)filename_elem = ET.Element('filename')filename_elem.text = str(os.path.join(filename + ".bmp"))block_root.append(filename_elem)path_elem = ET.Element('path')path_elem.text = str(os.path.join(annotation_path, filename + ".bmp"))block_root.append(path_elem)source_elem = ET.Element('source')database_elem = ET.Element('database')database_elem.text = str('Unknown')source_elem.append(database_elem)block_root.append(source_elem)# 添加图像大小信息'''使用 ElementTree 库创建一个 XML 文档中的 size 元素,并在其中添加 width 和 height 子元素,然后将它们的文本内容设置为图像 block_image 的宽度和高度,最终生成以下 XML 结构:<size><width>图像宽度</width><height>图像高度</height></size>最后将size 元素 添加到根节点 block_root = ET.Element('annotation') 中'''size_elem = ET.Element('size')width_elem = ET.Element('width')width_elem.text = str(block_image.width)height_elem = ET.Element('height')height_elem.text = str(block_image.height)depth_elem = ET.Element('depth')depth_elem.text = str('1')size_elem.append(width_elem)size_elem.append(height_elem)size_elem.append(depth_elem)block_root.append(size_elem)segmented_elem = ET.Element('segmented')segmented_elem.text = str('0')block_root.append(segmented_elem)# 遍历原图的目标,并添加到小块图片的标注文件中'''使用了 ElementTree 库中的 findall() 方法和 find() 方法来查找 XML 元素中的特定子元素,并将其文本内容转换为 Python 中的数据类型。for obj in root.findall('object'): 找到该 XML 元素中所有名为 object 的子元素,并对每一个 object 子元素执行下面的代码。在循环内部,obj.find('name').text 获取了当前 object 子元素下的名为 name 的子元素,并返回该子元素的文本内容,这里是一个字符串类型,赋值给了 name 变量。'''for obj in root.findall('object'):  # root = ET.parse(annotation_path).getroot() 原xml文件的根节点name = obj.find('name').text  # 原图标注框的类别名pose = obj.find('pose').texttruncated = obj.find('truncated').textdifficult = obj.find('difficult').textxmin = int(obj.find('bndbox/xmin').text)  # 原图的标注框的左上点的坐标ymin = int(obj.find('bndbox/ymin').text)  # 原图的标注框的左上点的坐标xmax = int(obj.find('bndbox/xmax').text)  # 原图的标注框的右下点的坐标ymax = int(obj.find('bndbox/ymax').text)  # 原图的标注框的右下点的坐标# 将目标框的坐标转换为小块图片的相对坐标   x0y0小块图片的左上角右下角在原图中的位置坐标xmin -= x0ymin -= y0xmax -= x0ymax -= y0# 如果目标框在小块图片内,则添加到标注文件中if 0 < xmin < block_image.width and 0 < ymin < block_image.height and \0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and xmax > block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and 0 < xmax < block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin < 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and 0 < ymin < block_image.height and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and ymin < 0 and xmax > block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin < 0 and 0 < xmax < block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and 0 < ymin < block_image.height and xmax > block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and ymin < 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin < 0 and xmax > block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and 0 < ymin < block_image.height and 0 < xmax < block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and xmax > block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and 0 < ymin < block_image.height and xmax > block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and ymin < 0 and xmax > block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and ymin < 0 and 0 < xmax < block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin < 0 and xmax > block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin == 0 and ymin == 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin == 0 and 0 < ymin < block_image.height and 0 < xmax < block_image.width and ymax == block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and xmax == block_image.width and ymax == block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin == 0 and xmax == block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin == 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin == 0 and ymin == 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and 0 < xmax < block_image.width and ymax == block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and xmax == block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin < 0 and xmax == block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin == 0 and ymin < 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin == 0 and 0 < ymin < block_image.height and 0 < xmax < block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and xmax == block_image.width and ymax > block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin == 0 and ymin == 0 and xmax == block_image.width and ymax == block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and ymin == 0 and 0 < xmax < block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif xmin < 0 and 0 < ymin < block_image.height and 0 < xmax < block_image.width and ymax == block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(0)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(xmax)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and ymin == 0 and xmax > block_image.width and 0 < ymax < block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(0)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(ymax)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelif 0 < xmin < block_image.width and 0 < ymin < block_image.height and xmax > block_image.width and ymax == block_image.height:obj_elem = ET.Element('object')  # 创建 object 属性name_elem = ET.Element('name')  # 创建 name 属性name_elem.text = name  # name = obj.find('name').text  name的值没有改变pose_elem = ET.Element('pose')pose_elem.text = posetruncated_elem = ET.Element('truncated')truncated_elem.text = truncateddifficult_elem = ET.Element('difficult')difficult_elem.text = difficultobj_elem.append(name_elem)  # 将name加入到 object 节点obj_elem.append(pose_elem)obj_elem.append(truncated_elem)obj_elem.append(difficult_elem)bndbox_elem = ET.Element('bndbox')  # 创建 bndbox 属性xmin_elem = ET.Element('xmin')  # 创建 xmin 属性xmin_elem.text = str(xmin)ymin_elem = ET.Element('ymin')  # 创建 ymin 属性ymin_elem.text = str(ymin)xmax_elem = ET.Element('xmax')  # 创建 xmax 属性xmax_elem.text = str(block_image.width)ymax_elem = ET.Element('ymax')  # 创建 ymax 属性ymax_elem.text = str(block_image.height)bndbox_elem.append(xmin_elem)bndbox_elem.append(ymin_elem)bndbox_elem.append(xmax_elem)bndbox_elem.append(ymax_elem)obj_elem.append(bndbox_elem)block_root.append(obj_elem)check_bndbox_is_none = Falseelse:check_bndbox_is_none = True# 保存小块图片的标注文件xml_filename = f'{filename}_{x0, y0}-{x1, y1}.xml''''这段代码使用 Python 中的 ElementTree 模块创建了一个名为 "tree" 的 XML 文档对象,并将其初始化为 "block_root" 元素的根节点。具体来说,这里的 ET 是 ElementTree 模块的别名,block_root 是一个 Element 对象,表示 XML 文档中的根元素。ET.ElementTree(block_root) 将 Element 对象包装在 ElementTree 对象中,以便使用 ElementTree 模块提供的方法来处理 XML 文档。通过将根元素传递给 ElementTree 构造函数,我们可以创建一个表示整个 XML 文档的对象,可以用于遍历、修改或序列化 XML 数据。'''tree = ET.ElementTree(block_root)tree.write(os.path.join(output_path,xml_filename))  # 使用 ElementTree 模块的 write 方法将 XML 文档对象 tree 中的数据写入到指定路径下的一个 XML 文件中。# if not check_bndbox_is_none:#     # 保存小块图片的标注文件#     xml_filename = f'{filename}_{x0, y0}-{x1, y1}.xml'#     '''#     这段代码使用 Python 中的 ElementTree 模块创建了一个名为 "tree" 的 XML 文档对象,并将其初始化为 "block_root" 元素的根节点。#     具体来说,这里的 ET 是 ElementTree 模块的别名,block_root 是一个 Element 对象,表示 XML 文档中的根元素。#     ET.ElementTree(block_root) 将 Element 对象包装在 ElementTree 对象中,以便使用 ElementTree 模块提供的方法来处理 XML 文档。#     通过将根元素传递给 ElementTree 构造函数,我们可以创建一个表示整个 XML 文档的对象,可以用于遍历、修改或序列化 XML 数据。#     '''#     tree = ET.ElementTree(block_root)#     tree.write(os.path.join(output_path,#                             xml_filename))  # 使用 ElementTree 模块的 write 方法将 XML 文档对象 tree 中的数据写入到指定路径下的一个 XML 文件中。# else:#     # 先保存后删除#     xml_filename = f'{filename}_{x0, y0}-{x1, y1}.xml'#     tree = ET.ElementTree(#         block_root)  # 这段代码使用 Python 中的 ElementTree 模块创建了一个名为 "tree" 的 XML 文档对象,并将其初始化为 "block_root" 元素的根节点。具体来说,这里的 ET 是 ElementTree 模块的别名,block_root 是一个 Element 对象,表示 XML 文档中的根元素。ET.ElementTree(block_root) 将 Element 对象包装在 ElementTree 对象中,以便使用 ElementTree 模块提供的方法来处理 XML 文档。通过将根元素传递给 ElementTree 构造函数,我们可以创建一个表示整个 XML 文档的对象,可以用于遍历、修改或序列化 XML 数据。#     tree.write(os.path.join(output_path,#                             xml_filename))#     os.remove(os.path.join(output_path,xml_filename))# xml文件地址
xml_folder = r'E:\hhbz\split\final9.14\xml'
# xml对应的图片地址
image_folder = r'E:\hhbz\split\final9.14\yes'
# 切割后的图片以及对应的xml文件保存地址
output_path = r'E:\hhbz\split\final9.14\split'
# 获取所有xml格式标注文件的文件名
xml_files = [f for f in os.listdir(xml_folder) if f.endswith('.xml')]for file in xml_files:annotation_path = os.path.join(xml_folder, file)filename = os.path.splitext(file)[0]image_path = os.path.join(image_folder, filename + ".bmp")crop_one_image_and_xml(image_path, annotation_path, output_path, filename)print(f"{filename}切割完毕")print("全部切割完毕")

在这里插入图片描述

参考文章

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/88835.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[Zer0pts2020]Can you guess it? basename特性 和preg_match 绕过

直接就可以进入看看源代码 原本以为这道题目有两个方法来做 但是 后面发现第二个没有找到漏洞 这里考点主要就是 正则和 basename的特性 首先就是正则 现在出现一个知识点 就是 /index.php/config.php 这种路径 访问的还是 index.php 我们可以试试看 还是在index.php中过…

基于微信小程序的明星应援小程序设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言系统主要功能&#xff1a;具体实现截图论文参考详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09;有保障的售后福利 代码参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌全网粉丝10W,CSDN特邀作者、博客专家、CSDN新星计…

golang工程——常用数据结构底层原理【mao、slice、func、string】

字符串 其实就是字符数组 注意 字节数组与字符串可以相互转换 a : "hello world" b : []byte(a) c : string(b)字节数组转换为字符串在运行时调用了slicebytetostring函数。需要注意的是&#xff0c;字节数组与字符串的相互转换并不是简单的指针引用&#xff0c;…

【实战详解】如何快速搭建接口自动化测试框架?Python + Requests

摘要&#xff1a; 本文主要介绍如何使用Python语言和Requests库进行接口自动化测试&#xff0c;并提供详细的代码示例和操作步骤。希望能对读者有所启发和帮助。 前言 随着移动互联网的快速发展&#xff0c;越来越多的应用程序采用Web API&#xff08;也称为RESTful API&…

分类预测 | Matlab实现GA-RF遗传算法优化随机森林多输入分类预测

分类预测 | Matlab实现GA-RF遗传算法优化随机森林多输入分类预测 目录 分类预测 | Matlab实现GA-RF遗传算法优化随机森林多输入分类预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 Matlab实现GA-RF遗传算法优化随机森林多输入分类预测&#xff08;完整源码和数据&…

如何进一步全面提高项目估算精准度?

项目估算非常重要&#xff0c;这直接关系着项目的成本和收入&#xff0c;如果估算不准确&#xff0c;将为项目带来较大风险。一般软件规模可以用多种方式进行估算&#xff0c;但是用功能点估算方式更准确&#xff0c;而自动估算让估算更快速&#xff0c;我们以CoCode开发的估算…

【Go】rsrc不是内部或外部命令、无法将“rsrc”项识别为 cmdlet、函数、脚本文件或可运行程序的名称 解决方法

前言 想尝试用go创建一个桌面应用程序&#xff0c;然后查了下决定用 walk。 我们要先下载walk&#xff0c;这里 官方链接 按照官方文档&#xff0c;我们先用go get命令下载。 go get github.com/lxn/walk然后分别创建好了 main.go、main.manifest 文件&#xff0c;代码如下…

libtorch之tensor的使用

1. tensor的创建 tensor的创建有三种常用的形式&#xff0c;如下所示 ones创建一个指定维度&#xff0c;数据全为1的tensor. 例子中的维度是2维&#xff0c;5行3列。 torch::Tensor t torch::ones({5,3}); zeros创建一个指定维度&#xff0c;数据全为0的tensor&#xff0c;例子…

Java基于SpringBoot的民宿管理系统,附源码

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝30W、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 文章目录 开发环境&#xff1a;后端&#xff1a;前端&#xff1a;数据库&#xff1a; 系统架构&#xff1a…

nginx配置密码访问

安装htpasswd 因为需要使用到htpasswd&#xff0c;htpasswd是Apache服务器中生成用户认证的一个工具&#xff0c;如果未安装&#xff0c;则使用如下命令安装htpasswd。 yum install -y httpd-tools设置用户名和密码 htpasswd 安装成功后&#xff0c;就可以设置用户名和密码&am…

Java项目-Spring Boot的生鲜网上交易系统

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝30W、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 文章目录 1 简介2 技术栈3 系统功能4 功能设计5系统详细设计5.1系统功能模块5.2后台功能模块5\.2\.1用户功…

vscode左键无法跳转到定义的文件

之前用vscode的时候&#xff0c;明明是可以ctrl键鼠标左键跳转到定义文件的&#xff0c;突然之间就不行了&#xff0c;鼠标移到引入上根本都没有下划线&#xff0c;无法跳转 解决方法&#xff1a; 项目的根目录新建 jsconfig.json 文件&#xff0c;代码如下 {"compiler…

http基础教程(超详细)

HTTP HTTP 一 、基础概念 请求和响应报文URL 二、HTTP 方法 GETHEADPOSTPUTPATCHDELETEOPTIONSCONNECTTRACE 三、HTTP 状态码 1XX 信息2XX 成功3XX 重定向4XX 客户端错误5XX 服务器错误 四、HTTP 首部 通用首部字段请求首部字段响应首部字段实体首部字段 五、具体应用 连接管理…

敏捷发布列车初探2 ---- Agile Release Train

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 敏捷发布列车二、ART的特性2.敏捷团队为列车提供动力3.与共同节奏保持一致4.关键角色启用 三、ART的责任总结 敏捷发布列车 敏捷发布列车&#xff08;ART&#xff…

CentOS 7.5 centos failed to load selinux policy 错误解决方法

这是个 selinux 使能导致的&#xff0c; 关闭即可 在进入到内核选中界面&#xff0c;选中要启动的内核&#xff0c; 按键盘 e 就会进入启动参数界面 进入启动参数界面如图&#xff0c;按上下键找到 UTF8 UTF8如图&#xff0c; 添加 selinux0 添加完成如图&#xff0c; 按 ctr…

MQTT协议知识梳理,看完你就懂了

目录 一、MQTT简介 二、MQTT框架图 三、MQTT特点 四、MQTT协议原理 1.MQTT协议实现框图 3.网络传输与应用消息 4.MQTT客户端 5.MQTT服务器 6.MQTT协议中的订阅、主题、会话 五、MQTT优缺点 优点 缺点 一、MQTT简介 MQTT是基于TCP/IP协议栈构建的异步通信消息协议&a…

比特币 ZK 赏金系列:第 2 部分——查找哈希冲突

在我们的零知识赏金 (ZKB) 系列的第二部分中&#xff0c;我们将其应用于解决哈希冲突难题。在这样的谜题中&#xff0c;两个不同的输入散列到相同的输出。此类赏金可用于&#xff1a; 充当煤矿中的金丝雀&#xff0c;给我们一个有价值的提醒。存在冲突是散列函数较弱的标志&…

linux内网渗透

一、信息收集 主机发现&#xff1a; nmap -sP 192.168.16.0/24 端口探测 masscan -p 1-65535 192.168.16.168 --rate1000 开放端口如下 nmap端口详细信息获取 nmap -sC -p 8888,3306,888,21,80 -A 192.168.16.168 -oA ddd4-port目录扫描 gobuster dir…

19.组合模式(Composite)

意图&#xff1a;将对象组成树状结构以表示“部分&#xff0d;整体”的层次结构&#xff0c;使得Client对单个对象和组合对象的使用具有一致性。 上下文&#xff1a;在树型结构的问题中&#xff0c;Client必须以不同的方式处理单个对象和组合对象。能否提供一种封装&#xff0c…

如何使用ArcGIS Pro制作标准地图样式国界

相信大家都浏览过标准地图服务提供的标准地图&#xff0c;不知道你有没有想过尝试制作里面的国界&#xff0c;这里为大家介绍一下制作方法&#xff0c;希望能对你有所帮助。 制作已定国界 在地图数据内&#xff0c;国界分为已定国界、未定国界和海岸线&#xff0c;我们先对已定…