Created
February 12, 2010 19:11
-
-
Save sgallese/302871 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class pHash | |
{ | |
native static VideoHash videoHash(String file); | |
native static AudioHash audioHash(String file); | |
native static DCTImageHash dctImageHash(String file); | |
native static MHImageHash mhImageHash(String file); | |
native static TextHash textHash(String file); | |
native static double imageDistance(ImageHash hash1, ImageHash hash2); | |
native static double audioDistance(AudioHash hash1, AudioHash hash2); | |
native static double videoDistance(VideoHash hash1, VideoHash hash2, int threshold); | |
native static int textDistance(TextHash txtHash1, TextHash txtHash2); | |
private native static void pHashInit(); | |
private native static void cleanup(); | |
static { | |
System.loadLibrary("pHash-jni"); | |
pHashInit(); | |
} | |
public static MHImageHash[] getMHImageHashes(String d) | |
{ | |
File dir = new File(d); | |
MHImageHash[] hashes = null; | |
if(dir.isDirectory()) | |
{ | |
File[] files = dir.listFiles(); | |
hashes = new MHImageHash[files.length]; | |
for(int i = 0; i < files.length; ++i) | |
{ | |
MHImageHash mh = mhImageHash(files[i].toString()); | |
if(mh != null) | |
hashes[i] = mh; | |
} | |
} | |
return hashes; | |
} | |
public static void main(String args[]) | |
{ | |
MVPTree mvp = new MVPTree("mvp"); | |
MHImageHash[] hashes = getMHImageHashes("/home/kdge/Pictures"); | |
System.out.println("Hash 1 is :" + hashes[0]); | |
boolean result = mvp.create(hashes); | |
System.out.println("MVP creation result is:" + result); | |
if(result) | |
{ | |
System.out.println("Successfully created MVP tree"); | |
Hash[] results = mvp.query(hashes[0], 100, 20); | |
if(results != null && results.length > 0) | |
{ | |
System.out.println("Query found " + results.length + " results"); | |
for(int j = 0; j < results.length; ++j) | |
System.out.println("File: " + results[j].filename); | |
} | |
MHImageHash[] newHashes = getMHImageHashes("/home/kdge/Pictures"); | |
boolean added = mvp.add(newHashes); | |
if(added) | |
{ | |
System.out.println("Hashes added successfully."); | |
Hash[] foundHashes = mvp.query(newHashes[0], 100, 20); | |
if(foundHashes != null && foundHashes.length > 0) | |
{ | |
System.out.println("Found newly added hash."); | |
} | |
} | |
} | |
// int i = 0; | |
// if(args[i].equals("-mvp")) | |
// { | |
// MVPTree mvp = new MVPTree("mvp"); | |
// MHImageHash[] hashes = getMHImageHashes(args[1]); | |
// boolean result = mvp.create(hashes); | |
// if(result) | |
// { | |
// System.out.println("Successfully created MVP tree"); | |
// Hash[] results = mvp.query(hashes[0], 100, 20); | |
// if(results != null && results.length > 0) | |
// { | |
// System.out.println("Query found " + results.length + " results"); | |
// for(int j = 0; j < results.length; ++j) | |
// System.out.println("File: " + results[j].filename); | |
// } | |
// | |
// MHImageHash[] newHashes = getMHImageHashes(args[2]); | |
// | |
// boolean added = mvp.add(newHashes); | |
// if(added) | |
// { | |
// System.out.println("Hashes added successfully."); | |
// Hash[] foundHashes = mvp.query(newHashes[0], 100, 20); | |
// if(foundHashes != null && foundHashes.length > 0) | |
// { | |
// System.out.println("Found newly added hash."); | |
// } | |
// } | |
// } | |
// | |
// } | |
// else if(args[i].equals("-a")) | |
// { | |
// AudioHash audioHash1 = audioHash(args[1]); | |
// AudioHash audioHash2 = audioHash(args[2]); | |
// System.out.println("cs = " + audioDistance(audioHash1,audioHash2)); | |
// } | |
// else if(args[i].equals("-dct")) | |
// { | |
// DCTImageHash imHash = dctImageHash(args[1]); | |
// DCTImageHash imHash2 = dctImageHash(args[2]); | |
// System.out.println("File 1: " + imHash.filename); | |
// System.out.println("File 2: " + imHash2.filename); | |
// | |
// System.out.println(imageDistance(imHash,imHash2)); | |
// } | |
// else if(args[i].equals("-mh")) | |
// { | |
// MHImageHash imHash = mhImageHash(args[1]); | |
// MHImageHash imHash2 = mhImageHash(args[2]); | |
// System.out.println("File 1: " + imHash.filename); | |
// System.out.println("File 2: " + imHash2.filename); | |
// | |
// System.out.println(imageDistance(imHash,imHash2)); | |
// | |
// } | |
// else if(args[i].equals("-v")) | |
// { | |
// VideoHash vHash = videoHash(args[1]); | |
// VideoHash vHash2 = videoHash(args[2]); | |
// System.out.println(videoDistance(vHash,vHash2, 21)); | |
// } | |
// else if(args[i].equals("-t")) | |
// { | |
// TextHash txtHash = textHash(args[1]); | |
// TextHash txtHash2 = textHash(args[2]); | |
// System.out.println(textDistance(txtHash,txtHash2)); | |
// } | |
pHash.cleanup(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment