Search
📄

3) Img & xml resizing

Image resizing

from PIL import Image import os img_path = r"./img" img_files = os.listdir(img_path) new_img_path = r"./resize_img" def resizeImg(files, imgPath, newImgPath): if not os.path.exists(newImgPath): os.makedirs(newImgPath) for image_file in files: image = Image.open(imgPath + '/' + image_file) resize_image = image.resize((108, 192)) resize_image.save(newImgPath + '/' + image_file) resizeImg(img_files, img_path, new_img_path)
Python
복사

xml resizing

import cv2 import os import xml.etree.ElementTree as ET xml_path = r"./xml" xml_files = os.listdir(xml_dir) new_xml_path = r"./resize_xml" def resizeLabel(files, xmlPath, newXmlPath): if not os.path.exists(newXmlPath): os.makedirs(newImgPath) for xml_file in files: tree = ET.parse(xmlPath + '/' + xml_file) new_file = newXmlPath + '/' + xml_file # bounding box 변경 objects = tree.findall('./object') for i, object_ in enumerate(objects): bndbox = object_.find('./bndbox') bndbox.find('./xmin').text = str(float(bndbox.find('./xmin').text) / 10) bndbox.find('./ymin').text = str(float(bndbox.find('./ymin').text) / 10) bndbox.find('./xmax').text = str(float(bndbox.find('./xmax').text) / 10) bndbox.find('./ymax').text = str(float(bndbox.find('./ymax').text) / 10) tree.write(new_file, encoding='utf8') resizeLabel(xml_files, xml_path, new_xml_path)
Python
복사
•
If path, width, height change is needed in xml, I will add a method.

Result

Image
xml