visual_regression.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. # (Server has to be running for this: npm start)
  17. #
  18. # npm run generate:blessed
  19. #
  20. # Make changes in OSMD, then generate your new images:
  21. #
  22. # npm run generate:current
  23. #
  24. # Run the regression tests against the blessed images in tests/blessed.
  25. #
  26. # npm run test:visual
  27. # # npm will navigate to the base folder automatically
  28. #
  29. # # or: (this should be done from the main OSMD folder)
  30. # # sh test/Util/visual_regression.sh
  31. #
  32. # Check visual_regression/diff/results.txt for results. This file is sorted
  33. # by PHASH difference (most different files on top.) The composite diff
  34. # images for failed tests (i.e., PHASH > 1.0) are stored in visual_regression/diff.
  35. #
  36. # If you are satisfied with the differences, copy *.png from visual_regression/current
  37. # into visual_regression/blessed, and submit your change.
  38. # PNG viewer on OSX. Switch this to whatever your system uses.
  39. # VIEWER=open
  40. # Show images over this PHASH threshold. This is probably too low, but
  41. # a good first pass.
  42. THRESHOLD=0.01
  43. # Set up Directories
  44. # 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)
  45. BUILDFOLDER=./visual_regression
  46. if [ "$1" != "" ]
  47. then
  48. BUILDFOLDER=$1
  49. fi
  50. BLESSED=$BUILDFOLDER/blessed
  51. CURRENT=$BUILDFOLDER/current
  52. DIFF=$BUILDFOLDER/diff
  53. # diff also acts as the temp folder here, unlike in Vexflow, where it is current.
  54. # 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
  55. # All results are stored here.
  56. RESULTS=$DIFF/results.txt
  57. WARNINGS=$DIFF/warnings.txt
  58. # If no prefix is provided, test all images.
  59. if [ "$2" == "" ]
  60. then
  61. files=*.png
  62. else
  63. files=$2*.png
  64. echo "only processing images matching bash string (not regex): $files"
  65. fi
  66. # some sanity checks: check if some png files are in the right folder and warn if not. doesn't make sure there are actual, correct png images though.
  67. folderWarningStringMsg="Exiting without running visual regression tests."
  68. # check if current directory exists / started from base OSMD folder
  69. if [ ! -e "$CURRENT" ]
  70. then
  71. echo "Warning: directory $CURRENT missing.
  72. Please run npm run generate:current (and if necessary npm run generate:blessed) first.
  73. $folderWarningStringMsg"
  74. exit 1
  75. fi
  76. # check if blessed directory exists / started from base OSMD folder
  77. if [ ! -e "$BLESSED" ]
  78. then
  79. echo "Warning: directory $BLESSED missing.
  80. Please run npm run generate:blessed first (or otherwise get the blessed images).
  81. $folderWarningStringMsg"
  82. exit 1
  83. fi
  84. # note: ls returns errors if the directory doesn't exist (that's why we do the checks above)
  85. totalCurrentImages=`ls -l $CURRENT/*.png | wc -l | sed 's/[[:space:]]//g'`
  86. totalBlessedImages=`ls -l $BLESSED/*.png | wc -l | sed 's/[[:space:]]//g'`
  87. # check if there are some current images
  88. if [ "$totalCurrentImages" -lt 1 ]
  89. then
  90. echo "Warning: Found no pngs in $CURRENT.
  91. Please run npm run generate (and if necessary npm run blessed) first.
  92. $folderWarningStringMsg"
  93. exit 1
  94. fi
  95. # check if there are some blessed images
  96. if [ "$totalBlessedImages" -lt 1 ]
  97. then
  98. echo "Warning: Found no pngs in $BLESSED.
  99. Please run npm run blessed first, ideally on the repository's develop state.
  100. $folderWarningStringMsg"
  101. exit 1
  102. fi
  103. # check that #currentImages == #blessedImages (will continue anyways)
  104. if [ ! "$totalCurrentImages" -eq "$totalBlessedImages" ]
  105. then
  106. echo "Warning: Number of current images ($totalCurrentImages) is not the same as blessed images ($totalBlessedImages). Continuing anyways."
  107. else
  108. echo "Found $totalCurrentImages current and $totalBlessedImages blessed png files (not tested if valid). Continuing."
  109. fi
  110. mkdir -p $DIFF
  111. if [ -e "$RESULTS" ]
  112. then
  113. rm $DIFF/*
  114. fi
  115. touch $RESULTS
  116. touch $RESULTS.fails
  117. # 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).
  118. touch $WARNINGS
  119. # Number of simultaneous jobs
  120. nproc=$(sysctl -n hw.physicalcpu 2> /dev/null || nproc)
  121. if [ -n "$NPROC" ]; then
  122. nproc=$NPROC
  123. fi
  124. total=`ls -l $BLESSED/$files | wc -l | sed 's/[[:space:]]//g'`
  125. echo "Running $total tests with threshold $THRESHOLD (nproc=$nproc)..."
  126. function ProgressBar {
  127. let _progress=(${1}*100/${2}*100)/100
  128. let _done=(${_progress}*4)/10
  129. let _left=40-$_done
  130. _fill=$(printf "%${_done}s")
  131. _empty=$(printf "%${_left}s")
  132. printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
  133. }
  134. function diff_image() {
  135. local image=$1
  136. local name=`basename $image .png`
  137. local blessed=$BLESSED/$name.png
  138. local current=$CURRENT/$name.png
  139. local diff=$DIFF/$name.png-temp
  140. if [ ! -e "$current" ]
  141. then
  142. echo "Warning: $name.png missing in $CURRENT." >$diff.warn
  143. return
  144. fi
  145. if [ ! -e "$blessed" ]
  146. then
  147. return
  148. fi
  149. cp $blessed $diff-a.png
  150. cp $current $diff-b.png
  151. # Calculate the difference metric and store the composite diff image.
  152. local hash=`compare -metric PHASH -highlight-color '#ff000050' $diff-b.png $diff-a.png $diff-diff.png 2>&1`
  153. local isGT=`echo "$hash > $THRESHOLD" | bc -l`
  154. if [ "$isGT" == "1" ]
  155. then
  156. # Add the result to results.txt
  157. echo $name $hash >$diff.fail
  158. # Threshold exceeded, save the diff and the original, current
  159. cp $diff-diff.png $DIFF/$name.png
  160. cp $diff-a.png $DIFF/$name'_'Blessed.png
  161. cp $diff-b.png $DIFF/$name'_'Current.png
  162. echo
  163. echo "Test: $name"
  164. echo " PHASH value exceeds threshold: $hash > $THRESHOLD"
  165. echo " Image diff stored in $DIFF/$name.png"
  166. # $VIEWER "$diff-diff.png" "$diff-a.png" "$diff-b.png"
  167. # echo 'Hit return to process next image...'
  168. # read
  169. else
  170. echo $name $hash >$diff.pass
  171. fi
  172. rm -f $diff-a.png $diff-b.png $diff-diff.png
  173. }
  174. function wait_jobs () {
  175. local n=$1
  176. while [[ "$(jobs -r | wc -l)" -ge "$n" ]] ; do
  177. # echo ===================================== && jobs -lr
  178. # wait the oldest job.
  179. local pid_to_wait=`jobs -rp | head -1`
  180. # echo wait $pid_to_wait
  181. wait $pid_to_wait &> /dev/null
  182. done
  183. }
  184. count=0
  185. for image in $CURRENT/$files
  186. do
  187. count=$((count + 1))
  188. ProgressBar ${count} ${total}
  189. wait_jobs $nproc
  190. diff_image $image &
  191. done
  192. wait
  193. cat $DIFF/*.warn 1>$WARNINGS 2>/dev/null
  194. rm -f $DIFF/*.warn
  195. ## Check for files newly built that are not yet blessed.
  196. for image in $CURRENT/$files
  197. do
  198. name=`basename $image .png`
  199. blessed=$BLESSED/$name.png
  200. current=$CURRENT/$name.png
  201. if [ ! -e "$blessed" ]
  202. then
  203. echo " Warning: $name.png missing in $BLESSED." >>$WARNINGS
  204. fi
  205. done
  206. num_warnings=`cat $WARNINGS | wc -l`
  207. cat $DIFF/*.fail 1>$RESULTS.fails 2>/dev/null
  208. num_fails=`cat $RESULTS.fails | wc -l`
  209. rm -f $DIFF/*.fail
  210. # Sort results by PHASH
  211. sort -r -n -k 2 $RESULTS.fails >$RESULTS
  212. sort -r -n -k 2 $DIFF/*.pass 1>>$RESULTS 2>/dev/null
  213. rm -f $DIFF/*.pass $RESULTS.fails
  214. echo
  215. echo Results stored in $DIFF/results.txt
  216. echo All images with a difference over threshold, $THRESHOLD, are
  217. echo available in $DIFF, sorted by perceptual hash.
  218. echo
  219. if [ "$num_warnings" -gt 0 ]
  220. then
  221. echo
  222. echo "You have $num_warnings warning(s):"
  223. cat $WARNINGS
  224. fi
  225. if [ "$num_fails" -gt 0 ]
  226. then
  227. echo "You have $num_fails fail(s):"
  228. head -n $num_fails $RESULTS
  229. else
  230. echo "Success - All diffs under threshold!"
  231. fi