python-3.x 使用多重处理来处理图像

huwehgph  于 2023-01-03  发布在  Python
关注(0)|答案(1)|浏览(226)

我创建了一个for循环,它会遍历一个图像目录,调整每个图像的大小,然后将其保存到另一个目录。代码可以工作,但我试图并行化这个过程,使其更快。
这是调整大小函数

import cv2 
import os

def resize_image(img):
    # get the name of the file
    name = os.path.basename(img)
    # read the image
    img = cv2.imread(img)
    # resize and save to new directory
    resize = cv2.resize(img, (700, 700)) 
    resized_image = cv2.imwrite("Resized/"+name, resize)

下面是for循环,它将遍历目录中的图像(调整目录中所有图像的大小大约需要700秒)。

SOURCE_DIRECTORY = "Source/"
directory_list = os.listdir(SOURCE_DIRECTORY)

for source_file in directory_list:
    source_path = os.path.join(SOURCE_DIRECTORY, source_file) 
    if os.path.isfile(source_path):
        resize_image(source_path)

为了并行化这个过程,我尝试使用concurrent.futures并将其Map到resize函数。

import concurrent.futures

SOURCE_DIRECTORY = "Source/"
directory_list = os.listdir(SOURCE_DIRECTORY)

with concurrent.futures.ProcessPoolExecutor() as executor: 
    executor.map(resize_image, directory_list)

但我立刻得到这个错误。

BrokenProcessPool: A child process terminated abruptly, the process pool is not usable anymore

我怎样才能并行化的过程中调整图像。任何帮助将不胜感激。

wooyq4lh

wooyq4lh1#

以下是可用于并行化任务的示例框架(使用multiprocessing.Pool):

import os
from multiprocessing import Pool

import cv2

def resize_image(file_name):
    # get the name of the file
    name = os.path.basename(file_name)

    # just to be sure the file exists (skip if not necessary):
    if not os.path.exists(name):
        return f"{name} does not exist!"

    # read the image
    img = cv2.imread(img)
    # resize and save to new directory
    resize = cv2.resize(img, (700, 700))
    resized_image = cv2.imwrite("Resized/" + name, resize)

    return f"{name} resized."

if __name__ == "__main__":

    SOURCE_DIRECTORY = "Source/"
    directory_list = os.listdir(SOURCE_DIRECTORY)

    filelist = []
    for source_file in directory_list:
        source_path = os.path.join(SOURCE_DIRECTORY, source_file)
        if os.path.isfile(source_path):
            filelist.append(source_path)

    with Pool(4) as pool:  # 4 is number of processes we want to use
        for result in pool.imap_unordered(resize_image, filelist):
            print(result)

相关问题