From 1cee30866b1584bca748805eda5b24ec12c8e781 Mon Sep 17 00:00:00 2001 From: Ezro Date: Thu, 9 Jun 2022 07:54:53 -0600 Subject: [PATCH] Parallelize template_finder search() --- src/template_finder.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/template_finder.py b/src/template_finder.py index a28923a06..74454c33a 100644 --- a/src/template_finder.py +++ b/src/template_finder.py @@ -9,8 +9,10 @@ from config import Config from utils.misc import cut_roi, load_template, list_files_in_folder, alpha_to_mask, roi_center, color_filter, mask_by_roi from functools import cache +import concurrent.futures templates_lock = threading.Lock() +executor = concurrent.futures.ThreadPoolExecutor() @dataclass class Template: @@ -147,13 +149,25 @@ def search( """ templates = _process_template_refs(ref) matches = [] + future_list = [] for template in templates: - match = _single_template_match(template, inp_img, roi, color_match, use_grayscale) - if match.score >= threshold: - if not best_match: - return match - else: - matches.append(match) + future = executor.submit( + _single_template_match, + template, + inp_img, + roi, + color_match, + use_grayscale + ) + future_list.append(future) + for i in range(len(future_list)): + match = future_list[i].result() + if match: + if match.score >= threshold: + if not best_match: + return match + else: + matches.append(match) if matches: matches = sorted(matches, key=lambda obj: obj.score, reverse=True) return matches[0]