I have the following function:
def Upscale(file):
img = cv2.imread(file)
sr = cv2.dnn_superres.DnnSuperResImpl_create()
path = 'LapSRN_x8.pb'
sr.readModel(path)
sr.setModel('lapsrn',8)
result = sr.upsample(img)
cv2.imwrite(f'{file}\output.png',result)
return None
I am trying to upscale a series of images, but each image takes 20 seconds to process. I want to use multithreading in order to create a faster program.
I have a list of my images :
file_list = ['page-136.jpg',
'page-0.jpg',
'page-11.jpg',
'page-12.jpg',
'page-13.jpg',
'page-14.jpg',
'page-37.jpg',
'page-58.jpg',
'page-62.jpg',
'page-64.jpg',
'page-134.jpg',
'page-135.jpg']
and I have the following code to add multithreading:
import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
with ThreadPoolExecutor(max_workers=1000) as executor:
future_to_response = {
executor.submit(Upscale, i): i for i in file_list
}
t = tqdm.tqdm(total=len(future_to_response))
for future in as_completed(future_to_response):
domain = future_to_response[future]
result = future.result()
t.update()
This however does not work, and I appear to be stuck. Can someone guide me in the right direction?
CodePudding user response:
Instead of collecting the results in a container you simply assign it to a variable every iteration. When iteration stops result
will only point-to the result of the last future to complete
....
result = []
for future in as_completed(future_to_response):
...
result.append(future.result())
...
CodePudding user response:
You could use the threading library:
import threading
threads_lst = []
for file in file_list:
threads_lst.append(threading.Thread(target=Upscale, args=(file,)))
threads_lst[-1].start()
for t in threads_lst:
t.join()