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.

410 lines
16 KiB

  1. package com.sothr.imagetools.hash
  2. import com.sothr.imagetools.{AppConfig, BaseTest, TestParams}
  3. import javax.imageio.ImageIO
  4. import java.io.File
  5. import com.sothr.imagetools.dto.ImageHashDTO
  6. import net.sf.ehcache.{Cache, Element}
  7. import scala.collection.mutable
  8. /**
  9. * Created by dev on 1/23/14.
  10. */
  11. class HashServiceTest extends BaseTest {
  12. // Define the number of runs the benchmarking tests should use
  13. val benchmarkRuns = 10
  14. def dhashTestCase(filePath:String):Long = {
  15. val sample = new File(filePath)
  16. val image = ImageIO.read(sample)
  17. HashService.getDhash(image)
  18. }
  19. test("Benchmark DHash") {
  20. info("Benchmarking DHash")
  21. info("DHash Large Image 3684x2736")
  22. val time = new mutable.MutableList[Long]()
  23. for (runNum <- 0 until benchmarkRuns) {
  24. time += getTime { dhashTestCase(TestParams.LargeSampleImage1) }
  25. }
  26. val largeMean = getMean(time.toArray[Long])
  27. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  28. time.clear()
  29. info("DHash Medium Image 1824x1368")
  30. for (runNum <- 0 until benchmarkRuns) {
  31. time += getTime { dhashTestCase(TestParams.MediumSampleImage1) }
  32. }
  33. val mediumMean = getMean(time.toArray[Long])
  34. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  35. time.clear()
  36. info("DHash Small Image 912x684")
  37. for (runNum <- 0 until benchmarkRuns) {
  38. time += getTime { dhashTestCase(TestParams.SmallSampleImage1) }
  39. }
  40. val smallMean = getMean(time.toArray[Long])
  41. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  42. time.clear()
  43. assert(true)
  44. }
  45. test("Confirm Largest DHash Output ") {
  46. val testData:Array[Array[Int]] = Array(
  47. Array(1,2,3,4,5,6,7,8),
  48. Array(16,15,14,13,12,11,10,9),
  49. Array(17,18,19,20,21,22,23,24),
  50. Array(32,31,30,29,28,27,26,25),
  51. Array(33,34,35,36,37,38,39,40),
  52. Array(48,47,46,45,44,43,42,41),
  53. Array(49,50,51,52,53,54,55,56),
  54. Array(64,63,62,61,60,59,58,57))
  55. val hash = DHash.getHash(testData)
  56. debug(s"Hash of test array: $hash")
  57. assert(hash == (Long.MaxValue))
  58. }
  59. test("Calculate DHash Large Sample Image 1") {
  60. debug("Starting 'Calculate DHash Large Sample Image 1' test")
  61. val sample = new File(TestParams.LargeSampleImage1)
  62. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  63. val image = ImageIO.read(sample)
  64. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  65. val hash = HashService.getDhash(image)
  66. debug(s"Testing that $hash = 4004374827879799635L")
  67. assert(hash == 4004374827879799635L)
  68. }
  69. test("Calculate DHash Medium Sample Image 1") {
  70. debug("Starting 'Calculate DHash Medium Sample Image 1' test")
  71. val sample = new File(TestParams.MediumSampleImage1)
  72. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  73. val image = ImageIO.read(sample)
  74. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  75. val hash = HashService.getDhash(image)
  76. debug(s"Testing that $hash = 4004374827879799635L")
  77. assert(hash == 4004374827879799635L)
  78. }
  79. test("Calculate DHash Small Sample Image 1") {
  80. debug("Starting 'Calculate DHash Small Sample Image 1' test")
  81. val sample = new File(TestParams.SmallSampleImage1)
  82. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  83. val image = ImageIO.read(sample)
  84. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  85. val hash = HashService.getDhash(image)
  86. debug(s"Testing that $hash = 4004383623972821843L")
  87. assert(hash == 4004383623972821843L)
  88. }
  89. test("DHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
  90. val largeHash = dhashTestCase(TestParams.LargeSampleImage1)
  91. val mediumHash = dhashTestCase(TestParams.MediumSampleImage1)
  92. val smallHash = dhashTestCase(TestParams.SmallSampleImage1)
  93. assert(HashService.areDhashSimilar(largeHash,mediumHash))
  94. assert(HashService.areDhashSimilar(largeHash,smallHash))
  95. assert(HashService.areDhashSimilar(mediumHash,smallHash))
  96. }
  97. def ahashTestCase(filePath:String):Long = {
  98. val sample = new File(filePath)
  99. val image = ImageIO.read(sample)
  100. HashService.getAhash(image)
  101. }
  102. test("Benchmark AHash") {
  103. info("Benchmarking AHash")
  104. info("AHash Large Image 3684x2736")
  105. val time = new mutable.MutableList[Long]()
  106. for (runNum <- 0 until benchmarkRuns) {
  107. time += getTime { ahashTestCase(TestParams.LargeSampleImage1) }
  108. }
  109. val largeMean = getMean(time.toArray[Long])
  110. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  111. time.clear()
  112. info("AHash Medium Image 1824x1368")
  113. for (runNum <- 0 until benchmarkRuns) {
  114. time += getTime { ahashTestCase(TestParams.MediumSampleImage1) }
  115. }
  116. val mediumMean = getMean(time.toArray[Long])
  117. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  118. time.clear()
  119. info("AHash Small Image 912x684")
  120. for (runNum <- 0 until benchmarkRuns) {
  121. time += getTime { ahashTestCase(TestParams.SmallSampleImage1) }
  122. }
  123. val smallMean = getMean(time.toArray[Long])
  124. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  125. time.clear()
  126. assert(true)
  127. }
  128. test("Calculate AHash Large Sample Image 1") {
  129. debug("Starting 'Calculate AHash Large Sample Image 1' test")
  130. val sample = new File(TestParams.LargeSampleImage1)
  131. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  132. val image = ImageIO.read(sample)
  133. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  134. val hash = HashService.getAhash(image)
  135. debug(s"Testing that $hash = 36070299219713907L")
  136. assert(hash == 36070299219713907L)
  137. }
  138. test("Calculate AHash Medium Sample Image 1") {
  139. debug("Starting 'Calculate AHash Medium Sample Image 1' test")
  140. val sample = new File(TestParams.MediumSampleImage1)
  141. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  142. val image = ImageIO.read(sample)
  143. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  144. val hash = HashService.getAhash(image)
  145. debug(s"Testing that $hash = 36070299219713907L")
  146. assert(hash == 36070299219713907L)
  147. }
  148. test("Calculate AHash Small Sample Image 1") {
  149. debug("Starting 'Calculate AHash Small Sample Image 1' test")
  150. val sample = new File(TestParams.SmallSampleImage1)
  151. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  152. val image = ImageIO.read(sample)
  153. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  154. val hash = HashService.getAhash(image)
  155. debug(s"Testing that $hash = 36070299219713907L")
  156. assert(hash == 36070299219713907L)
  157. }
  158. test("AHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
  159. val largeHash = ahashTestCase(TestParams.LargeSampleImage1)
  160. val mediumHash = ahashTestCase(TestParams.MediumSampleImage1)
  161. val smallHash = ahashTestCase(TestParams.SmallSampleImage1)
  162. assert(HashService.areAhashSimilar(largeHash,mediumHash))
  163. assert(HashService.areAhashSimilar(largeHash,smallHash))
  164. assert(HashService.areAhashSimilar(mediumHash,smallHash))
  165. }
  166. def phashTestCase(filePath:String):Long = {
  167. val sample = new File(filePath)
  168. val image = ImageIO.read(sample)
  169. HashService.getPhash(image)
  170. }
  171. test("Benchmark PHash") {
  172. info("Benchmarking PHash")
  173. info("PHash Large Image 3684x2736")
  174. val time = new mutable.MutableList[Long]()
  175. for (runNum <- 0 until benchmarkRuns) {
  176. time += getTime { phashTestCase(TestParams.LargeSampleImage1) }
  177. }
  178. val largeMean = getMean(time.toArray[Long])
  179. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  180. time.clear()
  181. info("PHash Medium Image 1824x1368")
  182. for (runNum <- 0 until benchmarkRuns) {
  183. time += getTime { phashTestCase(TestParams.MediumSampleImage1) }
  184. }
  185. val mediumMean = getMean(time.toArray[Long])
  186. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  187. time.clear()
  188. info("PHash Small Image 912x684")
  189. for (runNum <- 0 until benchmarkRuns) {
  190. time += getTime { phashTestCase(TestParams.SmallSampleImage1) }
  191. }
  192. val smallMean = getMean(time.toArray[Long])
  193. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  194. time.clear()
  195. assert(true)
  196. }
  197. test("Calculate PHash Large Sample Image 1") {
  198. debug("Starting 'Calculate PHash Large Sample Image 1' test")
  199. val sample = new File(TestParams.LargeSampleImage1)
  200. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  201. val image = ImageIO.read(sample)
  202. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  203. val hash = HashService.getPhash(image)
  204. debug(s"Testing that $hash = -9154554603604154117L")
  205. assert(hash == -9154554603604154117L)
  206. }
  207. test("Calculate PHash Medium Sample Image 1") {
  208. debug("Starting 'Calculate PHash Medium Sample Image 1' test")
  209. val sample = new File(TestParams.MediumSampleImage1)
  210. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  211. val image = ImageIO.read(sample)
  212. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  213. val hash = HashService.getPhash(image)
  214. debug(s"Testing that $hash = -9154589787976242949L")
  215. assert(hash == -9154589787976242949L)
  216. }
  217. test("Calculate PHash Small Sample Image 1") {
  218. debug("Starting 'Calculate PHash Small Sample Image 1' test")
  219. val sample = new File(TestParams.SmallSampleImage1)
  220. debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
  221. val image = ImageIO.read(sample)
  222. debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
  223. val hash = HashService.getPhash(image)
  224. debug(s"Testing that $hash = -9154589787976242949L")
  225. assert(hash == -9154589787976242949L)
  226. }
  227. test("PHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
  228. val largeHash = phashTestCase(TestParams.LargeSampleImage1)
  229. val mediumHash = phashTestCase(TestParams.MediumSampleImage1)
  230. val smallHash = phashTestCase(TestParams.SmallSampleImage1)
  231. assert(HashService.arePhashSimilar(largeHash,mediumHash))
  232. assert(HashService.arePhashSimilar(largeHash,smallHash))
  233. assert(HashService.arePhashSimilar(mediumHash,smallHash))
  234. }
  235. def md5TestCase(filePath:String):String = {
  236. HashService.getMD5(filePath)
  237. }
  238. test("Benchmark MD5") {
  239. info("Benchmarking MD5")
  240. info("MD5 Large Image 3684x2736")
  241. val time = new mutable.MutableList[Long]()
  242. for (runNum <- 0 until benchmarkRuns) {
  243. time += getTime { md5TestCase(TestParams.LargeSampleImage1) }
  244. }
  245. val largeMean = getMean(time.toArray[Long])
  246. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  247. time.clear()
  248. info("MD5 Medium Image 1824x1368")
  249. for (runNum <- 0 until benchmarkRuns) {
  250. time += getTime { md5TestCase(TestParams.MediumSampleImage1) }
  251. }
  252. val mediumMean = getMean(time.toArray[Long])
  253. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  254. time.clear()
  255. info("MD5 Small Image 912x684")
  256. for (runNum <- 0 until benchmarkRuns) {
  257. time += getTime { md5TestCase(TestParams.SmallSampleImage1) }
  258. }
  259. val smallMean = getMean(time.toArray[Long])
  260. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  261. time.clear()
  262. assert(true)
  263. }
  264. test("Calculate MD5 Large Sample Image 1") {
  265. debug("Starting 'Calculate MD5 Large Sample Image 1' test")
  266. val hash = HashService.getMD5(TestParams.LargeSampleImage1)
  267. debug(s"Testing that $hash = 3fbccfd5faf3f991435b827ee5961862")
  268. assert(hash == "3fbccfd5faf3f991435b827ee5961862")
  269. }
  270. test("Calculate MD5 Medium Sample Image 1") {
  271. debug("Starting 'Calculate MD5 Medium Sample Image 1' test")
  272. val hash = HashService.getMD5(TestParams.MediumSampleImage1)
  273. debug(s"Testing that $hash = a95e2cc4610307eb957e9c812429c53e")
  274. assert(hash == "a95e2cc4610307eb957e9c812429c53e")
  275. }
  276. test("Calculate MD5 Small Sample Image 1") {
  277. debug("Starting 'Calculate MD5 Small Sample Image 1' test")
  278. val hash = HashService.getMD5(TestParams.SmallSampleImage1)
  279. debug(s"Testing that $hash = b137131bd55896c747286e4d247b845e")
  280. assert(hash == "b137131bd55896c747286e4d247b845e")
  281. }
  282. def imageHashTestWithCacheCase(filePath:String):ImageHashDTO = {
  283. val cache = AppConfig.cacheManager.getCache("images")
  284. var result:ImageHashDTO = null
  285. if (cache.get(filePath) != null) {
  286. result = cache.get(filePath).getObjectValue.asInstanceOf[ImageHashDTO]
  287. } else {
  288. result = imageHashTestCase(filePath)
  289. cache.put(new Element(filePath,result))
  290. }
  291. result
  292. }
  293. def imageHashTestCase(filePath:String):ImageHashDTO = {
  294. HashService.getImageHashes(filePath)
  295. }
  296. test("Benchmark getImageHashes with cache") {
  297. info("Benchmarking getImageHashes with cache")
  298. info("getImageHashes with cache Large Image 3684x2736")
  299. val time = new mutable.MutableList[Long]()
  300. for (runNum <- 0 until benchmarkRuns) {
  301. time += getTime { imageHashTestWithCacheCase(TestParams.LargeSampleImage1) }
  302. }
  303. val largeMean = getMean(time.toArray[Long])
  304. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  305. time.clear()
  306. info("getImageHashes with cache Medium Image 1824x1368")
  307. for (runNum <- 0 until benchmarkRuns) {
  308. time += getTime { imageHashTestWithCacheCase(TestParams.MediumSampleImage1) }
  309. }
  310. val mediumMean = getMean(time.toArray[Long])
  311. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  312. time.clear()
  313. info("getImageHashes with cache Small Image 912x684")
  314. for (runNum <- 0 until benchmarkRuns) {
  315. time += getTime { imageHashTestWithCacheCase(TestParams.SmallSampleImage1) }
  316. }
  317. val smallMean = getMean(time.toArray[Long])
  318. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  319. time.clear()
  320. assert(true)
  321. }
  322. test("Benchmark getImageHashes") {
  323. info("Benchmarking getImageHashes")
  324. info("getImageHashes Large Image 3684x2736")
  325. val time = new mutable.MutableList[Long]()
  326. for (runNum <- 0 until benchmarkRuns) {
  327. time += getTime { imageHashTestCase(TestParams.LargeSampleImage1) }
  328. }
  329. val largeMean = getMean(time.toArray[Long])
  330. info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
  331. time.clear()
  332. info("getImageHashes Medium Image 1824x1368")
  333. for (runNum <- 0 until benchmarkRuns) {
  334. time += getTime { imageHashTestCase(TestParams.MediumSampleImage1) }
  335. }
  336. val mediumMean = getMean(time.toArray[Long])
  337. info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
  338. time.clear()
  339. info("getImageHashes Small Image 912x684")
  340. for (runNum <- 0 until benchmarkRuns) {
  341. time += getTime { imageHashTestCase(TestParams.SmallSampleImage1) }
  342. }
  343. val smallMean = getMean(time.toArray[Long])
  344. info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
  345. time.clear()
  346. assert(true)
  347. }
  348. test("ImageHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
  349. val largeHash = imageHashTestCase(TestParams.LargeSampleImage1)
  350. val mediumHash = imageHashTestCase(TestParams.MediumSampleImage1)
  351. val smallHash = imageHashTestCase(TestParams.SmallSampleImage1)
  352. assert(HashService.areImageHashesSimilar(largeHash,mediumHash))
  353. assert(HashService.areImageHashesSimilar(largeHash,smallHash))
  354. assert(HashService.areImageHashesSimilar(mediumHash,smallHash))
  355. }
  356. test("Calculate ImageHash Large Sample Image 1") {
  357. debug("Starting 'Calculate ImageHash Large Sample Image 1' test")
  358. val hash = HashService.getImageHashes(TestParams.LargeSampleImage1)
  359. debug(s"Testing that ${hash.hashCode} = -812844858")
  360. assert(hash.hashCode == -812844858)
  361. }
  362. test("Calculate ImageHash Medium Sample Image 1") {
  363. debug("Starting 'Calculate ImageHash Medium Sample Image 1' test")
  364. val hash = HashService.getImageHashes(TestParams.MediumSampleImage1)
  365. debug(s"Testing that ${hash.hashCode} = -812836666")
  366. assert(hash.hashCode == -812836666)
  367. }
  368. test("Calculate ImageHash Small Sample Image 1") {
  369. debug("Starting 'Calculate ImageHash Small Sample Image 1' test")
  370. val hash = HashService.getImageHashes(TestParams.SmallSampleImage1)
  371. debug(s"Testing that ${hash.hashCode} = -812840762")
  372. assert(hash.hashCode == -812840762)
  373. }
  374. }