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.
 
 
 

93 lines
3.2 KiB

package com.sothr.imagetools.hash
import java.io.File
import javax.imageio.ImageIO
import scala.collection.mutable
class HashServicePHashTest extends HashServiceBaseTest {
def phashTestCase(filePath: String): Long = {
val sample = new File(filePath)
val image = ImageIO.read(sample)
HashService.getPhash(phashSetting, image)
}
test("Calculate PHash Large Sample Image 1") {
debug("Starting 'Calculate PHash Large Sample Image 1' test")
val sample = new File(LargeSampleImage1)
debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
val image = ImageIO.read(sample)
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
val hash = HashService.getPhash(phashSetting, image)
debug(s"Testing that $hash = -9154554603604154117L")
assert(hash == -9154554603604154117L)
}
test("Calculate PHash Medium Sample Image 1") {
debug("Starting 'Calculate PHash Medium Sample Image 1' test")
val sample = new File(MediumSampleImage1)
debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
val image = ImageIO.read(sample)
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
val hash = HashService.getPhash(phashSetting, image)
debug(s"Testing that $hash = -9154589787976242949L")
assert(hash == -9154589787976242949L)
}
test("Calculate PHash Small Sample Image 1") {
debug("Starting 'Calculate PHash Small Sample Image 1' test")
val sample = new File(SmallSampleImage1)
debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
val image = ImageIO.read(sample)
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
val hash = HashService.getPhash(phashSetting, image)
debug(s"Testing that $hash = -9154589787976242949L")
assert(hash == -9154589787976242949L)
}
test("PHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
val largeHash = phashTestCase(LargeSampleImage1)
val mediumHash = phashTestCase(MediumSampleImage1)
val smallHash = phashTestCase(SmallSampleImage1)
assert(HashService.areHashSimilar(phashSetting, largeHash, mediumHash))
assert(HashService.areHashSimilar(phashSetting, largeHash, smallHash))
assert(HashService.areHashSimilar(phashSetting, mediumHash, smallHash))
}
test("Benchmark PHash") {
info("Benchmarking PHash")
info("PHash Large Image 3684x2736")
val time = new mutable.MutableList[Long]()
for (_ <- 0 until benchmarkRuns) {
time += getTime {
phashTestCase(LargeSampleImage1)
}
}
val largeMean = getMean(time.toArray[Long])
info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
time.clear()
info("PHash Medium Image 1824x1368")
for (_ <- 0 until benchmarkRuns) {
time += getTime {
phashTestCase(MediumSampleImage1)
}
}
val mediumMean = getMean(time.toArray[Long])
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
time.clear()
info("PHash Small Image 912x684")
for (_ <- 0 until benchmarkRuns) {
time += getTime {
phashTestCase(SmallSampleImage1)
}
}
val smallMean = getMean(time.toArray[Long])
info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
time.clear()
assert(true)
}
}