切割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("全部切割完毕")
参考文章