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.4 KiB

  1. package com.sothr.imagetools.engine
  2. import java.io.File
  3. import akka.actor.{ActorRef, Props}
  4. import com.sothr.imagetools.engine.image.{SimilarImages, ImageService, Image}
  5. import com.sothr.imagetools.image.SimilarImages
  6. import grizzled.slf4j.Logging
  7. import scala.collection.mutable
  8. /**
  9. * Engine that works sequentially
  10. * Very Slow, but consistent. Excellent for testing
  11. *
  12. * Created by drew on 1/26/14.
  13. */
  14. class SequentialEngine extends Engine with Logging {
  15. var processedListener = system.actorOf(Props[DefaultLoggingEngineListener],
  16. name = "ProcessedEngineListener")
  17. var similarityListener = system.actorOf(Props[DefaultLoggingEngineListener],
  18. name = "SimilarityEngineListener")
  19. override def setProcessedListener(listenerRef: ActorRef) = {
  20. this.processedListener = listenerRef
  21. }
  22. override def setSimilarityListener(listenerRef: ActorRef) = {
  23. this.similarityListener = listenerRef
  24. }
  25. def getImagesForDirectory(directoryPath:String, recursive:Boolean=false, recursiveDepth:Int=500):List[Image] = {
  26. debug(s"Looking for images in directory: $directoryPath")
  27. val images:mutable.MutableList[Image] = new mutable.MutableList[Image]()
  28. val imageFiles = getAllImageFiles(directoryPath, recursive, recursiveDepth)
  29. val directory:File = new File(directoryPath)
  30. var count = 0
  31. for (file <- imageFiles) {
  32. count += 1
  33. if (count % 25 == 0) {
  34. //info(s"Processed ${count}/${imageFiles.size}")
  35. processedListener ! ScannedFileCount(count,imageFiles.size)
  36. }
  37. val image = ImageService.getImage(file)
  38. if (image != null) {
  39. images += image
  40. }
  41. }
  42. images.toList
  43. }
  44. def getSimilarImagesForDirectory(directoryPath:String, recursive:Boolean=false, recursiveDepth:Int=500):List[SimilarImages] = {
  45. debug(s"Looking for similar images in directory: $directoryPath")
  46. val images = getImagesForDirectory(directoryPath, recursive, recursiveDepth)
  47. info(s"Searching ${images.length} images for similarities")
  48. val ignoreSet = new mutable.HashSet[Image]()
  49. val allSimilarImages = new mutable.MutableList[SimilarImages]()
  50. var processedCount = 0
  51. var similarCount = 0
  52. for (rootImage <- images) {
  53. if (!ignoreSet.contains(rootImage)) {
  54. if (processedCount % 25 == 0) {
  55. //info(s"Processed ${processedCount}/${images.length - ignoreSet.size} About ${images.length -
  56. // processedCount} images to go")
  57. similarityListener ! ScannedFileCount(processedCount,images.length-ignoreSet.size)
  58. }
  59. debug(s"Looking for images similar to: ${rootImage.imagePath}")
  60. ignoreSet += rootImage
  61. val similarImages = new mutable.MutableList[Image]()
  62. for (image <- images) {
  63. if (!ignoreSet.contains(image)) {
  64. if (rootImage.isSimilarTo(image)) {
  65. debug(s"Image: ${image.imagePath} is similar")
  66. similarImages += image
  67. ignoreSet += image
  68. similarCount += 1
  69. }
  70. }
  71. }
  72. if (similarImages.length > 1) {
  73. val similar = new SimilarImages(rootImage, similarImages.toList)
  74. debug(s"Found similar images: ${similar.toString}")
  75. allSimilarImages += similar
  76. }
  77. processedCount += 1
  78. }
  79. }
  80. info(s"Finished processing ${images.size} images. Found $similarCount similar images")
  81. allSimilarImages.toList
  82. }
  83. }