Browse Source

Added a controll to the commandline. Tweaked a few other portions of the code. Need to fix that part where everything is printed to the console when running AppCLI

master
Drew Short 11 years ago
parent
commit
df31c800d5
  1. 51
      src/main/java/com/sothr/imagetools/AppCLI.java
  2. 1
      src/main/java/com/sothr/imagetools/AppConfig.java
  3. 15
      src/main/scala/com/sothr/imagetools/Engine.scala
  4. 17
      src/main/scala/com/sothr/imagetools/image/ImageCache.scala
  5. 1
      src/main/scala/com/sothr/imagetools/util/PropertiesService.scala

51
src/main/java/com/sothr/imagetools/AppCLI.java

@ -1,20 +1,59 @@
package com.sothr.imagetools;
import com.sothr.imagetools.image.ImageCache;
import com.sothr.imagetools.image.SimilarImages;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.collection.immutable.List;
import java.text.ParseException;
/**
* CLI interface for Image Tools
*/
class AppCLI {
private static Logger logger;
private static Logger logger;
public static void main(String[] args) {
AppConfig.configureApp();
logger = LoggerFactory.getLogger(AppCLI.class);
logger.info("Started Image Tools CLI");
try {
Options options = getOptions();
CommandLineParser parser = new BasicParser();
CommandLine cmd = parser.parse(options, args);
process(cmd);
} catch (Exception ex) {
logger.error("Unhandled exception in AppCLI", ex);
}
}
private static Options getOptions() {
Options options = new Options();
options.addOption(new Option("s", true, "scan directories for a list of similar images"));
return options;
}
public static void main(String[] args) {
AppConfig.configureApp();
logger = LoggerFactory.getLogger(AppCLI.class);
logger.info("Started Image Tools CLI");
System.out.println("Hello World");
private static void process(CommandLine cmd) {
//scan a comma separated list of paths to search for image similarities
Engine engine = new Engine(new ImageCache());
if (cmd.hasOption('s')) {
String scanList = cmd.getOptionValue('s');
String[] paths = scanList.split(",");
for (String path : paths) {
List<SimilarImages> similarImages = engine.getSimilarImagesForDirectory(path);
for (int index = 0; index < similarImages.length(); index++) {
SimilarImages similar = similarImages.apply(index);
System.out.println(similar.toString());
}
}
}
}
}

1
src/main/java/com/sothr/imagetools/AppConfig.java

@ -33,6 +33,7 @@ class AppConfig {
loadProperties();
}
configLogging();
logger.info("Detected Version: %s of Image Tools".format(PropertiesService.getVersion().toString()));
}
public static void configSimpleLogging() {

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

@ -1,6 +1,6 @@
package com.sothr.imagetools
import com.sothr.imagetools.image.{SimilarImages, ImageFilter, Image}
import com.sothr.imagetools.image.{ImageCache, SimilarImages, ImageFilter, Image}
import scala.collection.immutable
import scala.collection.mutable
import java.io.File
@ -9,7 +9,7 @@ import grizzled.slf4j.Logging
/**
* Created by drew on 1/26/14.
*/
class Engine extends Logging{
class Engine(var imageCache:ImageCache = new ImageCache()) extends Logging{
val imageFilter:ImageFilter = new ImageFilter()
@ -21,7 +21,11 @@ class Engine extends Logging{
val files = directory.listFiles(imageFilter)
debug(s"Found ${files.length} files that are images in directory: $directoryPath")
for (file <- files) {
images += ImageService.getImage(file)
if (imageCache.contains(file.getAbsolutePath)) {
images += imageCache.get(file.getAbsolutePath)
} else {
images += ImageService.getImage(file)
}
}
} else {
error(s"Provided path: $directoryPath is not a directory")
@ -32,10 +36,13 @@ class Engine extends Logging{
def getSimilarImagesForDirectory(directoryPath:String):List[SimilarImages] = {
debug(s"Looking for similar images in directory: $directoryPath")
val images = getImagesForDirectory(directoryPath)
info(s"Searching ${images.length} images for similarities")
val ignoreSet = new mutable.HashSet[Image]()
val allSimilarImages = new mutable.MutableList[SimilarImages]()
var processedCount = 0
for (rootImage <- images) {
if (!ignoreSet.contains(rootImage)) {
info(s"Processed ${processedCount}/${images.length - ignoreSet.size} About ${images.length - processedCount} images to go")
debug(s"Looking for images similar to: ${rootImage.imagePath}")
ignoreSet += rootImage
val similarImages = new mutable.MutableList[Image]()
@ -53,9 +60,9 @@ class Engine extends Logging{
debug(s"Found similar images: ${similar.toString}")
allSimilarImages += similar
}
processedCount += 1
}
}
allSimilarImages.toList
}
}

17
src/main/scala/com/sothr/imagetools/image/ImageCache.scala

@ -0,0 +1,17 @@
package com.sothr.imagetools.image
import scala.collection.mutable
/**
* Created by drew on 1/26/14.
*/
class ImageCache {
private val cache = new mutable.HashMap[String, Image]()
def contains(imagePath:String) = cache.contains(imagePath)
def get(imagePath:String) = cache(imagePath)
def add(imagePath:String, image:Image) = cache.put(imagePath,image)
def size:Int = cache.size
}

1
src/main/scala/com/sothr/imagetools/util/PropertiesService.scala

@ -12,6 +12,7 @@ object PropertiesService extends Logging {
private val properties:Properties = new Properties()
private var version:Version = null
def getVersion:Version = this.version
private val propertiesToClean:Array[String] = Array("version")
/*

Loading…
Cancel
Save