在 1 张图像中裁剪多个边界框的最有效/最快捷的方法,超过数千张图像? [英] Most efficient/quickest way to crop multiple bounding boxes in 1 image, over thousands of images?

查看:87
本文介绍了在 1 张图像中裁剪多个边界框的最有效/最快捷的方法,超过数千张图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约 3000 张图像的数据集,如果我有边界框位置的坐标,我想裁剪每个图像的多个区域.唯一的问题是我的代码非常慢,我尝试过分析和使用 Cython,但改进很小.我正在使用 Pillow 库进行裁剪,它们可能是实现此任务的更快方法吗?

I have a dataset of around 3000 images, of which I want to crop multiple areas of each image if I have the coordinates of the bounding boxes for their location. The only problem is my code is extremely slow, I've tried profiling and using Cython, but with marginal improvements. I'm using the Pillow library for cropping, is their perhaps a faster way of achieving this task?

边界框位置存储在 CSV 文件中.下面的代码遍历每个文件

The bounding box locations are stored in a CSV file. The code below iterates over every file

train_label=pd.read_csv("train.csv")
for i in range(len(train_label.index)):
    name=train_label["image_id"][i]; labels=train_label["labels"][i]; 
    split_images(name,labels)

还有如下的重载功能.

def split_images(name, labels):
boundingboxes = np.array(labels.split(' ')).reshape(-1, 5)
for (unicode, x, y, w, h) in boundingboxes:
    try:

    # Create target Directory

        os.mkdir('unicodes/{}'.format(str(unicode)))
    except FileExistsError:
        None

    (x, y, w, h) = (int(x), int(y), int(w), int(h))
    imsource = Image.open('train_images/{}.jpg'.format(name))
    cropped_image = imsource.crop((x, y, x + w, y + h))
    cropped_image.save('unicodes/{}/{}.jpg'.format(unicode, name))

如果有帮助,我将在 Google 云平台上远程运行代码.

I'm running the code remotely on Google cloud platform if that helps.

推荐答案

图片有多大?

在枕头性能页面上研究一些其他库建议可能值得研究

It might be worth investigating some of the other library suggestions on the pillow performance page especially with regards to efficiency.

ImageMagick 是我的第一个想法.它非常强大,可以从命令行运行(参见 docs).批量裁剪整个文件夹很简单,尽管我不确定如何在不同区域裁剪每个图像(必须有办法).一个笨拙但可行的解决方案可能是使用 python 来处理 csv 并生成 ImageMagic 调用.

ImageMagick was my first thought. It is quite powerful and can be run from command line (see docs). Batch cropping a whole folder is straightforward, although I'm not sure how to crop each image in a different area (there must be a way). A hacky but workable solution could be to use python to process the csv and generate ImageMagic calls.

抱歉没有明确的解决方案.

Apologies for not having a clear solution.

这篇关于在 1 张图像中裁剪多个边界框的最有效/最快捷的方法,超过数千张图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆