visual_regression.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/bin/bash
  2. # This script runs a visual regression test on all the images
  3. # generated from OSMD samples (npm run generate:current and npm run generate:blessed)
  4. #
  5. # inspired by and adapted from Vexflow's visual regression tests.
  6. #
  7. # Prerequisites: ImageMagick
  8. #
  9. # On OSX: $ brew install imagemagick
  10. # On Linux: $ apt-get install imagemagick
  11. #
  12. # Usage:
  13. #
  14. #
  15. # First generate the known good or previous state PNG images you want to compare to, e.g. the develop branch or last release:
  16. #
  17. # npm run generate:blessed
  18. #
  19. # Make changes in OSMD, then generate your new images:
  20. #
  21. # npm run generate:current
  22. #
  23. # Run the regression tests against the blessed images in visual_regression/blessed.
  24. #
  25. # npm run test:visual
  26. # # npm will navigate to the base folder automatically
  27. #
  28. # # or: (this should be done from the main OSMD folder)
  29. # # sh test/Util/visual_regression.sh [imageBaseFolder] [sampleShellPrefix]
  30. # # example: sh test/Util/visual_regression.sh ./visual_regression OSMD_function_test_
  31. # # will run visual regression tests for all images matching OSMD_function_test_*.png.
  32. #
  33. # Check visual_regression/diff/results.txt for results. This file is sorted
  34. # by PHASH difference (most different files on top.) The composite diff
  35. # images for failed tests (i.e., PHASH > 1.0) are stored in visual_regression/diff.
  36. #
  37. # (If you are satisfied with the differences, copy *.png from visual_regression/current
  38. # into visual_regression/blessed, and submit your change (TODO))
  39. # PNG viewer on OSX. Switch this to whatever your system uses.
  40. # VIEWER=open
  41. # Show images over this PHASH threshold.
  42. # 0.01 is probably too low, but a good first pass.
  43. # 0.0001 catches for example a repetition ending not having a down line at the end (see Saltarello bar 10) (0.001 doesn't catch this)
  44. # 0.0000001 (6 0s after the dot) catches e.g. a chord symbol moving about 3 pixels to the right (on a canvas of ~1450px width)
  45. THRESHOLD=0.00000001
  46. # Set up Directories
  47. # It does not matter where this script is executed, as long as these folders are given correctly (and blessed/current have png images set up correctly)
  48. BUILDFOLDER=./visual_regression
  49. if [ "$1" != "" ]
  50. then
  51. BUILDFOLDER=$1
  52. fi
  53. BLESSED=$BUILDFOLDER/blessed
  54. CURRENT=$BUILDFOLDER/current
  55. DIFF=$BUILDFOLDER/diff
  56. # diff also acts as the temp folder here, unlike in Vexflow, where it is current.
  57. # it would be nice to have a tmp folder (for temporary files), but we'd want to delete the folder entirely, and we'd better not risk using rm -rf in a script
  58. # All results are stored here.
  59. RESULTS=$DIFF/results.txt
  60. WARNINGS=$DIFF/warnings.txt
  61. # If no prefix is provided, test all images.
  62. if [ "$2" == "" ]
  63. then
  64. files=*.png
  65. else
  66. files=$2*.png
  67. echo "image filter (shell): $files"
  68. fi
  69. ## Sanity checks: some simple checks that the script can run correctly (doesn't validate pngs)
  70. folderWarningStringMsg="Exiting without running visual regression tests."
  71. totalCurrentImages=`ls -1 $CURRENT/$files | wc -l | xargs` # xargs trims space
  72. if [ $? -ne 0 ] || [ "$totalCurrentImages" -lt 1 ] # $? returns the exit code of the previous command (ls). (0 is success)
  73. then
  74. echo Missing images in $CURRENT.
  75. echo Please run \"npm run generate:current\"
  76. exit 1
  77. fi
  78. totalBlessedImages=`ls -1 $BLESSED/$files | wc -l | xargs`
  79. if [ $? -ne 0 ] || [ "$totalBlessedImages" -lt 1 ]
  80. then
  81. echo Missing images in $BLESSED.
  82. echo Please run \"npm run generate:blessed\"
  83. exit 1
  84. fi
  85. # check that #currentImages == #blessedImages (will continue anyways)
  86. if [ ! "$totalCurrentImages" -eq "$totalBlessedImages" ]
  87. then
  88. echo "Warning: Number of current images ($totalCurrentImages) is not the same as blessed images ($totalBlessedImages). Continuing anyways."
  89. else
  90. echo "Found $totalCurrentImages current and $totalBlessedImages blessed png files (not tested if valid). Continuing."
  91. fi
  92. # ----------------- end of sanity checks -----------------
  93. mkdir -p $DIFF
  94. if [ -e "$RESULTS" ]
  95. then
  96. rm $DIFF/*
  97. fi
  98. touch $RESULTS
  99. touch $RESULTS.fails
  100. # this shouldn't be named .fail because we have a *.fail shell match further below, which will loop endlessly if files are in the same folder (diff).
  101. touch $WARNINGS
  102. # Number of simultaneous jobs
  103. nproc=$(sysctl -n hw.physicalcpu 2> /dev/null || nproc)
  104. if [ -n "$NPROC" ]; then
  105. nproc=$NPROC
  106. fi
  107. total=`ls -l $BLESSED/$files | wc -l | sed 's/[[:space:]]//g'`
  108. echo "Running $total tests with threshold $THRESHOLD (nproc=$nproc)..."
  109. function ProgressBar {
  110. let _progress=(${1}*100/${2}*100)/100
  111. let _done=(${_progress}*4)/10
  112. let _left=40-$_done
  113. _fill=$(printf "%${_done}s")
  114. _empty=$(printf "%${_left}s")
  115. printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
  116. }
  117. function diff_image() {
  118. local image=$1
  119. local name=`basename $image .png`
  120. local blessed=$BLESSED/$name.png
  121. local current=$CURRENT/$name.png
  122. local diff=$DIFF/$name.png-temp
  123. if [ ! -e "$current" ]
  124. then
  125. echo " Warning: $name.png missing in $CURRENT. Skipped." >$diff.warn
  126. #((total--))
  127. return
  128. fi
  129. if [ ! -e "$blessed" ]
  130. then
  131. echo " Warning: $name.png doesn't exist in $BLESSED. Skipped." >$diff.warn
  132. #((total--))
  133. return
  134. fi
  135. cp $blessed $diff-a.png
  136. cp $current $diff-b.png
  137. # Calculate the difference metric and store the composite diff image.
  138. local hash=`compare -metric PHASH -highlight-color '#ff000050' $diff-b.png $diff-a.png $diff-diff.png 2>&1`
  139. # convert hash to decimal if it was in scientific notation (e.g. 1.5e-2 -> 0.015)
  140. # otherwise, syntax error will be returned for $hash > $THRESHOLD" | bc -l
  141. if [ ! $hash == 0 ] # don't change a "0" string
  142. then
  143. export LC_NUMERIC="en_US.UTF-8" # use dot instead of comma for decimals (1.5 instead of 1,5)
  144. hash=$(printf "%.14f" $hash) # precision seems limited to 15 digits in shell/awk(?)
  145. hash=$(echo $hash | bc -l | grep -o '.*[1-9]') # remove trailing 0s
  146. if (( $(echo "$hash < 1" |bc -l) ))
  147. then
  148. hash="0$hash" # add leading 0 (e.g. .01 -> 0.01), just for readability/display
  149. fi
  150. fi
  151. local isGT=`echo "$hash > $THRESHOLD" | bc -l`
  152. if [ "$isGT" == "1" ]
  153. then
  154. # Add the result to results.txt
  155. echo $name $hash >$diff.fail
  156. # Threshold exceeded, save the diff and the original, current
  157. cp $diff-diff.png $DIFF/$name.png
  158. cp $diff-a.png $DIFF/$name'_'Blessed.png
  159. cp $diff-b.png $DIFF/$name'_'Current.png
  160. echo
  161. echo "Test: $name"
  162. echo " PHASH value exceeds threshold: $hash > $THRESHOLD"
  163. echo " Image diff stored in $DIFF/$name.png"
  164. # $VIEWER "$diff-diff.png" "$diff-a.png" "$diff-b.png"
  165. # echo 'Hit return to process next image...'
  166. # read
  167. else
  168. echo $name $hash >$diff.pass
  169. fi
  170. rm -f $diff-a.png $diff-b.png $diff-diff.png
  171. }
  172. function wait_jobs () {
  173. local n=$1
  174. while [[ "$(jobs -r | wc -l)" -ge "$n" ]] ; do
  175. # echo ===================================== && jobs -lr
  176. # wait the oldest job.
  177. local pid_to_wait=`jobs -rp | head -1`
  178. # echo wait $pid_to_wait
  179. wait $pid_to_wait &> /dev/null
  180. done
  181. }
  182. count=0
  183. for image in $CURRENT/$files
  184. do
  185. count=$((count + 1))
  186. ProgressBar ${count} ${total}
  187. wait_jobs $nproc
  188. diff_image $image &
  189. done
  190. wait
  191. cat $DIFF/*.warn 1>$WARNINGS 2>/dev/null
  192. rm -f $DIFF/*.warn
  193. ## Check for files newly built that are not yet blessed.
  194. for image in $CURRENT/$files
  195. do
  196. name=`basename $image .png`
  197. blessed=$BLESSED/$name.png
  198. current=$CURRENT/$name.png
  199. done
  200. num_warnings=`cat $WARNINGS | wc -l`
  201. cat $DIFF/*.fail 1>$RESULTS.fails 2>/dev/null
  202. num_fails=`cat $RESULTS.fails | wc -l`
  203. rm -f $DIFF/*.fail
  204. # Sort results by PHASH
  205. sort -r -n -k 2 $RESULTS.fails >$RESULTS
  206. sort -r -n -k 2 $DIFF/*.pass 1>>$RESULTS 2>/dev/null
  207. rm -f $DIFF/*.pass $RESULTS.fails
  208. echo
  209. echo Results stored in $DIFF/results.txt
  210. echo All images with a difference over threshold, $THRESHOLD, are
  211. echo available in $DIFF, sorted by perceptual hash.
  212. echo
  213. if [ "$num_warnings" -gt 0 ]
  214. then
  215. echo
  216. echo "You have $num_warnings warning(s):"
  217. cat $WARNINGS
  218. fi
  219. if [ "$num_fails" -gt 0 ]
  220. then
  221. echo "You have $num_fails fail(s):"
  222. head -n $num_fails $RESULTS
  223. else
  224. echo "Success - All diffs under threshold!"
  225. fi