【发布时间】:2020-11-11 16:18:27
【问题描述】:
这是我的代码
#crop images
import numpy as np # linear algebra
import xml.etree.ElementTree as ET # for parsing XML
import matplotlib.pyplot as plt # to show images
from PIL import Image # to read images
import os
import glob
root_images="../img"
root_annots="../xmls"
all_images=os.listdir("../img")
print(f"Total images : {len(all_images)}")
breeds = glob.glob('../xmls')
annotation=[]
for b in breeds:
annotation+=glob.glob(b+"/*")
print(f"Total annotation : {len(annotation)}")
breed_map={}
for annot in annotation:
breed=annot.split("../")[-2]
index=breed.split("-")[0]
breed_map.setdefault(index,breed)
print(f"Total Breeds : {len(breed_map)}")
def bounding_box(image):
bpath=root_annots+"/"+str(image.split(".")[0]+".xml")
tree = ET.parse(bpath)
root = tree.getroot()
objects = root.findall('object')
for o in objects:
bndbox = o.find('bndbox') # reading bound box
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
return (xmin,ymin,xmax,ymax)
plt.figure(figsize=(10,10))
bbox=[]
for i,image in enumerate(all_images):
bbox=bounding_box(image)
print(box)
im=Image.open(os.path.join(root_images,image))
im=im.crop(bbox)
im.save('../result_imgs/{}.jpeg'.format(i,im))
这段代码适用于图像列表,但它只裁剪每个图像的一个边界框,而 XML 文件中存在多个边界框。 但我想从图像列表中裁剪每个图像的多个边界框。 那么有人可以帮我解决这个问题吗? 谢谢。
【问题讨论】:
-
每次迭代都会覆盖 xmin、xmax、ymin、ymax 的值。你返回最后一个
标签: python image python-imaging-library crop