Browse Source

Added a getSimilarImagesForDirectory method

master
Drew Short 11 years ago
parent
commit
2de7c85c10
  1. 28
      src/main/scala/com/sothr/imagetools/Engine.scala
  2. 7
      src/main/scala/com/sothr/imagetools/image/Image.scala
  3. 15
      src/main/scala/com/sothr/imagetools/image/SimilarImages.scala
  4. 7
      src/test/scala/com/sothr/imagetools/EngineTest.scala

28
src/main/scala/com/sothr/imagetools/Engine.scala

@ -14,6 +14,7 @@ class Engine extends Logging{
val imageFilter:ImageFilter = new ImageFilter()
def getImagesForDirectory(directoryPath:String):List[Image] = {
debug(s"Looking for images in directory: $directoryPath")
val images:mutable.MutableList[Image] = new mutable.MutableList[Image]()
val directory:File = new File(directoryPath)
if (directory.isDirectory) {
@ -29,7 +30,32 @@ class Engine extends Logging{
}
def getSimilarImagesForDirectory(directoryPath:String):List[SimilarImages] = {
null
debug(s"Looking for similar images in directory: $directoryPath")
val images = getImagesForDirectory(directoryPath)
val ignoreSet = new mutable.HashSet[Image]()
val allSimilarImages = new mutable.MutableList[SimilarImages]()
for (rootImage <- images) {
if (!ignoreSet.contains(rootImage)) {
debug(s"Looking for images similar to: ${rootImage.imagePath}")
ignoreSet += rootImage
val similarImages = new mutable.MutableList[Image]()
for (image <- images) {
if (!ignoreSet.contains(image)) {
if (rootImage.isSimilarTo(image)) {
debug(s"Image: ${image.imagePath} is similar")
similarImages += image
ignoreSet += image
}
}
}
if (similarImages.length > 1) {
val similar = new SimilarImages(rootImage, similarImages.toList)
debug(s"Found similar images: ${similar.toString}")
allSimilarImages += similar
}
}
}
allSimilarImages.toList
}
}

7
src/main/scala/com/sothr/imagetools/image/Image.scala

@ -22,5 +22,12 @@ class Image(val imagePath:String, val thumbnailPath:String, val imageSize:Tuple2
override def toString:String = {
s"Image: $imagePath Thumbnail: $thumbnailPath Image Size: ${imageSize._1}x${imageSize._2} Hashes: $hashes"
}
override def hashCode:Int = {
var result = 365
result = 37 * result + imagePath.hashCode
result = 41 * result + hashes.hashCode()
result
}
}

15
src/main/scala/com/sothr/imagetools/image/SimilarImages.scala

@ -7,4 +7,19 @@ import grizzled.slf4j.Logging
*/
class SimilarImages(val rootImage:Image, val similarImages:List[Image]) extends Logging {
protected def getPrettySimilarImagesList:String = {
val sb = new StringBuilder()
for (image <- similarImages) {
sb.append(image.imagePath)
sb.append(System.lineSeparator())
}
sb.toString()
}
override def toString:String = {
s"""RootImage: ${rootImage.imagePath}
Similar Images:
${getPrettySimilarImagesList}""".stripMargin
}
}

7
src/test/scala/com/sothr/imagetools/EngineTest.scala

@ -8,4 +8,11 @@ class EngineTest extends BaseTest{
val engine:Engine = new Engine()
assertResult(3) { engine.getImagesForDirectory("sample").length }
}
test("Test getSimilarImagesForDirectory for sample directory") {
val engine = new Engine()
val similarImages = engine.getSimilarImagesForDirectory("sample")
assertResult(1) { similarImages.length }
assertResult(2) { similarImages(0).similarImages.length }
}
}
Loading…
Cancel
Save