From 1daa7f97c06883aae504e3e8a2b76c84eac4d63b Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Fri, 4 Nov 2016 11:29:00 +0000 Subject: [PATCH] ReventWorkload: Modified statedection to vary scale of images Due to inaccuracies in revent playback, the resultant state does not always match the templates precisely causing state detection to fail. To help this images are now scaled to different sizes before being matched to the templates to compensate for slight variations in size. --- wlauto/utils/statedetect.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/wlauto/utils/statedetect.py b/wlauto/utils/statedetect.py index 94790659..7ce4b2c4 100755 --- a/wlauto/utils/statedetect.py +++ b/wlauto/utils/statedetect.py @@ -91,14 +91,24 @@ def match_state(screenshot_file, defpath, state_definitions): # pylint: disable for template_png in template_list: template = cv2.imread(os.path.join(defpath, 'templates', template_png + '.png'), 0) template_edge = auto_canny(template) + template_height, template_width = template_edge.shape[:2] - res = cv2.matchTemplate(img_edge, template_edge, cv2.TM_CCOEFF_NORMED) - threshold = 0.5 - loc = np.where(res >= threshold) - zipped = zip(*loc[::-1]) + # loop over the scales of the image + for scale in np.linspace(1.4, 0.6, 61): + resized = imutils.resize(img_edge, width=int(img_edge.shape[1] * scale)) - if len(zipped) > 0: - matched_templates.append(template_png) + # skip if the resized image is smaller than the template + if resized.shape[0] < template_height or resized.shape[1] < template_width: + break + + res = cv2.matchTemplate(resized, template_edge, cv2.TM_CCOEFF_NORMED) + threshold = 0.4 + loc = np.where(res >= threshold) + zipped = zip(*loc[::-1]) + + if len(zipped) > 0: + matched_templates.append(template_png) + break # determine the state according to the matched templates matched_state = "none"