python读取目标检测PASCAL VOC数据集,将图像采用边缘填充的方法缩放到相应大小(例如640x640),并修改对应的xml文件
单个文件处理
import cv2
import os
import xml.etree.ElementTree as ET
def resize_image_and_xml(image_path, xml_path, target_size):
image = cv2.imread(image_path)
original_height, original_width = image.shape[:2]
scale_x = target_size / original_width
scale_y = target_size / original_height
resized_image = cv2.resize(image, (target_size, target_size))
cv2.imwrite('resized_image.jpg', resized_image)
tree = ET.parse(xml_path)
root = tree.getroot()
size_tag = root.find('size')
size_tag.find('width').text = str(target_size)
size_tag.find('height').text = str(target_size)
for obj in root.findall('object'):
bbox = obj.find('bndbox')
bbox.find('xmin').text = str(int(float(bbox.find('xmin').text) * scale_x))
bbox.find('xmax').text = str(int(float(bbox.find('xmax').text) * scale_x))
bbox.find('ymin').text = str(int(float(bbox.find('ymin').text) * scale_y))
bbox.find('ymax').text = str(int(float(bbox.find('ymax').text) * scale_y))
tree.write('resized_annotation.xml')
image_path = r'C:\Users\GuoQingru\Downloads\pp_fall\temp\fall_0.jpg'
xml_path = r'C:\Users\GuoQingru\Downloads\pp_fall\temp\fall_0.xml'
target_size = 640
resize_image_and_xml(image_path, xml_path, target_size)
批处理脚本
import cv2
import os
import xml.etree.ElementTree as ET
def resize_image_and_xml(images_root_old,xml_root_old,image_root_new,xml_root_new,image_name, target_size):
image_path=os.path.join(images_root_old,image_name)
image = cv2.imread(image_path)
original_height, original_width = image.shape[:2]
scale_x = target_size / original_width
scale_y = target_size / original_height
resized_image = cv2.resize(image, (target_size, target_size))
cv2.imwrite(os.path.join(image_root_new,image_name), resized_image)
tree = ET.parse(os.path.join(xml_root_old,os.path.splitext(image_name)[0]+'.xml'))
root = tree.getroot()
size_tag = root.find('size')
size_tag.find('width').text = str(target_size)
size_tag.find('height').text = str(target_size)
for obj in root.findall('object'):
bbox = obj.find('bndbox')
bbox.find('xmin').text = str(int(float(bbox.find('xmin').text) * scale_x))
bbox.find('xmax').text = str(int(float(bbox.find('xmax').text) * scale_x))
bbox.find('ymin').text = str(int(float(bbox.find('ymin').text) * scale_y))
bbox.find('ymax').text = str(int(float(bbox.find('ymax').text) * scale_y))
tree.write(os.path.join(xml_root_new,os.path.splitext(image_name)[0]+'.xml'))
target_size = 640
images_root_old=r'C:\Users\GuoQingru\Downloads\pp_fall\JEPGImages'
xml_root_old=r'C:\Users\GuoQingru\Downloads\pp_fall\Annotations'
image_root_new=r'C:\Users\GuoQingru\Downloads\pp_fall\JEPGImages_640'
xml_root_new=r'C:\Users\GuoQingru\Downloads\pp_fall\Annotations_640'
images_list=[]
for image_name in os.listdir(images_root_old):
images_list.append(image_name)
for image_name in images_list:
resize_image_and_xml(images_root_old,xml_root_old,image_root_new,xml_root_new,image_name, target_size)