Commit a7efc8f8 authored by anonym's avatar anonym
Browse files

Test suite: make it possible to display OpenCV matches via pop-up.

This will be useful for a image bumping mode I'm about to add where
the user is shown what a candidate image matches on the screen, so the
user can verify that it's correct.
parent adf5c87c
......@@ -9,8 +9,15 @@ module OpenCV
@python = 'python3'
end
def self.matchTemplate(image, screen, sensitivity)
def self.matchTemplate(image, screen, sensitivity, show_match)
assert(sensitivity < 1.0)
# Do a deep-copy so we don't mess up the outer environment
env = Hash[ENV]
if ENV['USER_DISPLAY'].nil? || ENV['USER_DISPLAY'] == ''
show_match = false
else
env['DISPLAY'] = ENV['USER_DISPLAY']
end
code = <<EOF
#!/usr/bin/env python3
from __future__ import print_function
......@@ -24,7 +31,7 @@ class FindFailed(RuntimeError):
pass
# Returns the pos of candidate inside image, or raises if no match
def match(image, candidate, sensitivity):
def match(image, candidate, sensitivity, show_match=False):
assert(sensitivity < 1.0)
image_rgb = cv2.imread(image, 1)
image_gray = cv2.cvtColor(image_rgb, cv2.COLOR_BGR2GRAY)
......@@ -37,16 +44,21 @@ def match(image, candidate, sensitivity):
raise FindFailed
cv2.imwrite(os.environ['TMPDIR'] + '/last_opencv_match.png',
image_rgb[y:y+h, x:x+w])
if show_match:
cv2.rectangle(image_rgb, pos, (x + w, y + h), (0, 0, 255), 1)
cv2.imshow('Found match!', image_rgb)
cv2.waitKey(0)
return [x, y, w, h]
try:
print(*match("#{screen}", "#{image}", #{sensitivity}))
print(*match("#{screen}", "#{image}", #{sensitivity},
#{show_match.to_s.capitalize}))
except FindFailed:
sys.exit(1)
except:
sys.exit(127)
EOF
p = IO.popen([@python, '-c', code], err: [:child, :out])
p = IO.popen([env, @python, '-c', code], err: [:child, :out])
out = p.readlines.join("\n")
p.close
case $?.exitstatus
......
......@@ -42,11 +42,11 @@ class Screen
assert(out.empty?, "xdotool reported an error:\n" + out)
end
def match_screen(image, sensitivity)
def match_screen(image, sensitivity, show_image)
screenshot = "#{$config["TMPDIR"]}/screenshot.png"
$vm.display.screenshot(screenshot)
return OpenCV.matchTemplate("#{OPENCV_IMAGE_PATH}/#{image}",
screenshot, sensitivity)
screenshot, sensitivity, show_image)
ensure
FileUtils.rm_f(screenshot)
end
......@@ -62,7 +62,7 @@ class Screen
raise "unsupported type: #{pattern.class}"
end
debug_log("Screen: trying to find #{image}") if opts[:log]
p = match_screen(image, opts[:sensitivity])
p = match_screen(image, opts[:sensitivity], false)
if p.nil?
raise FindFailed.new("cannot find #{image} on the screen")
end
......
......@@ -267,6 +267,7 @@ if [ -n "${VNC_VIEWER:-}" ]; then
start_vnc_viewer
fi
export USER_DISPLAY="${DISPLAY:-}"
export DISPLAY=${TARGET_DISPLAY}
cucumber ${@}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment