You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.2 KiB

  1. package com.sothr.imagetools.hash
  2. import java.io.File
  3. import javax.imageio.ImageIO
  4. import scala.collection.mutable
  5. class HashServicePHashTest extends HashServiceBaseTest {
  6. def phashTestCase(filePath: String): Long = {
  7. val sample = new File(filePath)
  8. val image = ImageIO.read(sample)
  9. HashService.getPhash(phashSetting, image)
  10. }
  11. test("Calculate PHash Large Sample Image 1") {
  12. debug("Starting 'Calculate PHash Large Sample Image 1' test")
  13. val sample = new File(LargeSampleImage1)
  14. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  15. val image = ImageIO.read(sample)
  16. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  17. val hash = HashService.getPhash(phashSetting, image)
  18. debug(s"Testing that $hash = -9154554603604154117L")
  19. assert(hash == -9154554603604154117L)
  20. }
  21. test("Calculate PHash Medium Sample Image 1") {
  22. debug("Starting 'Calculate PHash Medium Sample Image 1' test")
  23. val sample = new File(MediumSampleImage1)
  24. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  25. val image = ImageIO.read(sample)
  26. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  27. val hash = HashService.getPhash(phashSetting, image)
  28. debug(s"Testing that $hash = -9154589787976242949L")
  29. assert(hash == -9154589787976242949L)
  30. }
  31. test("Calculate PHash Small Sample Image 1") {
  32. debug("Starting 'Calculate PHash Small Sample Image 1' test")
  33. val sample = new File(SmallSampleImage1)
  34. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  35. val image = ImageIO.read(sample)
  36. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  37. val hash = HashService.getPhash(phashSetting, image)
  38. debug(s"Testing that $hash = -9154589787976242949L")
  39. assert(hash == -9154589787976242949L)
  40. }
  41. test("PHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
  42. val largeHash = phashTestCase(LargeSampleImage1)
  43. val mediumHash = phashTestCase(MediumSampleImage1)
  44. val smallHash = phashTestCase(SmallSampleImage1)
  45. assert(HashService.areHashSimilar(phashSetting, largeHash, mediumHash))
  46. assert(HashService.areHashSimilar(phashSetting, largeHash, smallHash))
  47. assert(HashService.areHashSimilar(phashSetting, mediumHash, smallHash))
  48. }
  49. test("Benchmark PHash") {
  50. info("Benchmarking PHash")
  51. info("PHash Large Image 3684x2736")
  52. val time = new mutable.MutableList[Long]()
  53. for (_ <- 0 until benchmarkRuns) {
  54. time += getTime {
  55. phashTestCase(LargeSampleImage1)
  56. }
  57. }
  58. val largeMean = getMean(time.toArray[Long])
  59. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  60. time.clear()
  61. info("PHash Medium Image 1824x1368")
  62. for (_ <- 0 until benchmarkRuns) {
  63. time += getTime {
  64. phashTestCase(MediumSampleImage1)
  65. }
  66. }
  67. val mediumMean = getMean(time.toArray[Long])
  68. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  69. time.clear()
  70. info("PHash Small Image 912x684")
  71. for (_ <- 0 until benchmarkRuns) {
  72. time += getTime {
  73. phashTestCase(SmallSampleImage1)
  74. }
  75. }
  76. val smallMean = getMean(time.toArray[Long])
  77. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  78. time.clear()
  79. assert(true)
  80. }
  81. }