I have the following function:
def resize(path_to_image):
img = cv2.imread(path_to_image)
return img
This function takes in an image, upscales the image, and then writes the image to a folder and returns the resulting image array. There is a list of PNG image paths, which is of the following form:
file_list = ['page.png',...etc]
I attempted to use multithreading to make the process faster, as each image takes 95 sec to complete (there are hundreds of images). The code for this is the following:
import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
res=[]
with ThreadPoolExecutor(max_workers=10) as executor:
future_to_response = {
executor.submit(Upscale, f'C:\\Users\\user\\Desktop\\DPI_proj\\images\\{i}'): i for i in file_list
}
t = tqdm.tqdm(total=len(future_to_response))
for future in as_completed(future_to_response):
res.append(future.result())
for i in range(len(res)):
cv2.imwrite(f'{i}.png',res[i])
The above code multithreads the process, and then has a simple loop to run through the list I am appending to in order to store the images. This process takes just as long as it would take to run each image consecutively (the multithreading does not make the process faster).
I attempted to fix this by instead using multiprocessing, and the code for this is as follows:
import multiprocessing
res = []
for i in file_list:
p = multiprocessing.Process(target=Upscale((f'C:\\Users\\user\\Desktop\\DPI_proj\\images\\{i}')))
res.append(p)
p.start()
However, this takes just as long (does not decrease the time it takes to compute each image individually), and furthermore, the output of my res array is not a list of arrays, rather it is:
[<Process name='Process-54' pid=28028 parent=27048 stopped exitcode=1>,
<Process name='Process-55' pid=18272 parent=27048 stopped exitcode=1>,
<Process name='Process-56' pid=23116 parent=27048 stopped exitcode=1>,
<Process name='Process-57' pid=5536 parent=27048 stopped exitcode=1>,
<Process name='Process-58' pid=14496 parent=27048 stopped exitcode=1>,
<Process name='Process-59' pid=16964 parent=27048 stopped exitcode=1>,
<Process name='Process-60' pid=14832 parent=27048 stopped exitcode=1>,
<Process name='Process-61' pid=19584 parent=27048 stopped exitcode=1>,
<Process name='Process-62' pid=20244 parent=27048 stopped exitcode=1>,
<Process name='Process-63' pid=28768 parent=27048 stopped exitcode=1>,
<Process name='Process-64' pid=16164 parent=27048 stopped exitcode=1>,
<Process name='Process-65' pid=21196 parent=27048 stopped exitcode=1>]
Does anyone have an idea of how I can accomplish making this process faster with either multithreading or multiprocessing?