Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Parallelize template_finder search() #848

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/template_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down