visual_regression.sh 7.1 KB

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