Skip to content

Commit d716f06

Browse files
Updated urlwatcher script to use normalized cross correlation
1 parent 895a535 commit d716f06

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

urlwatcher/urlwatcher.sh

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ TELEGRAM_SCRIPT="$(pwd)/../tools/telegram.sh"
1212
SCREENSHOT_SCRIPT="$(pwd)/../tools/screenshot.sh"
1313
# Read the first line from the BOT_TOKEN file
1414
TELEGRAM_BOT_TOKEN=$(head -n 1 ../BOT_TOKEN)
15+
# The threshold when to consider two images matching. If two images have a normalized cross correllation >= this value, they are considered identical
16+
NCC_THRESHOLD="0.99"
1517

1618
# Set the PATH variable for the python script below, so it finds the geckodriver executable when executed from cron
1719
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@@ -98,11 +100,11 @@ function screenshotsMatch {
98100
local IMAGE_OLD="$1"
99101
local IMAGE_LATEST="$2"
100102

101-
HASH_OLD=$(identify -quiet -format "%#" "$IMAGE_OLD")
102-
HASH_LATEST=$(identify -quiet -format "%#" "$IMAGE_LATEST")
103+
# Calculate the normalized cross correllation between both images
104+
NCC=$(compare -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST")
103105

104-
if [ "$HASH_OLD" != "$HASH_LATEST" ]; then
105-
# The screenshots are not identical!
106+
if [ "NCC" -lt "$NCC_THRESHOLD" ]; then
107+
# The screenshots are not identical
106108
echo "Possible change detected. Confirming..."
107109

108110
# Take another screenshot to confirm it's not just a one-time loading error
@@ -122,17 +124,19 @@ function screenshotsMatch {
122124

123125
cropScreenshot "latest.png"
124126

125-
# Compare the two cropped screenshots
126-
HASH_OLD=$(identify -quiet -format "%#" old.png)
127-
HASH_LATEST=$(identify -quiet -format "%#" latest.png)
127+
# Compare the two cropped screenshots again
128+
# Calculate the normalized cross correllation between both images
129+
NCC=$(compare -metric NCC "$IMAGE_OLD" "$IMAGE_LATEST")
128130

129-
if [ "$HASH_OLD" != "$HASH_LATEST" ]; then
131+
if [ "NCC" -lt "$NCC_THRESHOLD" ]; then
130132
# Return false, as the screenshots do not match
133+
# In bash: 1 == false
131134
return 1
132135
fi
133136
fi
134137

135138
# If statement above didn't exit, that means we have no mismatching hashes and therefore the screenshots match (return true)
139+
# In bash: 0 == true
136140
return 0
137141
}
138142

@@ -209,9 +213,9 @@ while IFS='' read -r line || [ -n "${line}" ]; do
209213
# COMPARE THE SCREENSHOTS #
210214
###########################
211215

212-
# If the new screenshot is all black (some display error), ignore it
216+
# If the new screenshot is all black or all white (some display error), ignore it
213217
mean=$(convert latest.png -format "%[mean]" info:)
214-
if [ "$mean" == "0" ]; then
218+
if [ "$mean" == "0" || "$mean" == "65535" ]; then
215219
rollBack
216220
# Skip this entry
217221
cd ../..

0 commit comments

Comments
 (0)