|
@@ -5,6 +5,24 @@
|
|
|
*/
|
|
|
|
|
|
requirelib("filelib");
|
|
|
+requirelib("imagelib");
|
|
|
+
|
|
|
+//Tags record structure
|
|
|
+/*
|
|
|
+ {
|
|
|
+ "filepath": {image_vpath},
|
|
|
+ "tags": [
|
|
|
+ {
|
|
|
+ "object": {detected_object_1},
|
|
|
+ "confidence": {confidence_in_percentage}
|
|
|
+ },
|
|
|
+ {
|
|
|
+ "object": {detected_object_2},
|
|
|
+ "confidence": {confidence_in_percentage}
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+*/
|
|
|
|
|
|
//Get all possible roots, return array of [name, path and photo root]
|
|
|
function getAllPossibleRoots(){
|
|
@@ -27,6 +45,7 @@ function getAllPossibleRoots(){
|
|
|
}
|
|
|
|
|
|
function isSupportedImage(filename){
|
|
|
+ var fileExt = filename.split(".").pop();
|
|
|
if (fileExt == "jpg" || fileExt == "png" || fileExt == "jpeg"){
|
|
|
return true;
|
|
|
}else{
|
|
@@ -50,4 +69,60 @@ function getAllImageFiles(){
|
|
|
}
|
|
|
|
|
|
return results;
|
|
|
-}
|
|
|
+}
|
|
|
+
|
|
|
+//Get the tag of a certain image file given its filepath
|
|
|
+function getImageTags(imagefile){
|
|
|
+ var results = imagelib.classify(imagefile, "darknet19");
|
|
|
+ var tags = [];
|
|
|
+ for (var i = 0; i < results.length; i++){
|
|
|
+ if (results[i].Percentage > 50){
|
|
|
+ //Confidence larger than 50
|
|
|
+ tags.push({
|
|
|
+ "object": results[i].Name,
|
|
|
+ "confidence":results[i].Percentage
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return tags;
|
|
|
+}
|
|
|
+
|
|
|
+function getImageTagsRecord(imagefile){
|
|
|
+ var tags = getImageTags(imagefile);
|
|
|
+ return {
|
|
|
+ "filepath": imagefile,
|
|
|
+ "tags": tags
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function loadAllTagsRecord(rootID){
|
|
|
+ var tagFile = rootID + ":/Photo/tags.json"
|
|
|
+ if (filelib.fileExists(tagFile)){
|
|
|
+ var tagsData = filelib.readFile(tagFile)
|
|
|
+ return JSON.parse(tagsData);
|
|
|
+ }
|
|
|
+ return [];
|
|
|
+}
|
|
|
+
|
|
|
+function saveAllTagsRecords(rootID, tagRecords){
|
|
|
+ var tagFile = rootID + ":/Photo/tags.json"
|
|
|
+ return filelib.writeFile(tagFile, JSON.stringify(tagRecords))
|
|
|
+}
|
|
|
+
|
|
|
+//Clearn up the record from the list of tag records that its file no longer exists
|
|
|
+function matchAndClearNonExistsRecords(tagRecords){
|
|
|
+ var cleanedTagRecords = [];
|
|
|
+ for ( var i = 0; i < tagRecords.length; i++){
|
|
|
+ var thisRecord = tagRecords[i];
|
|
|
+ var thisFilepath = thisRecord.filepath;
|
|
|
+ //Check if this file exists
|
|
|
+ if (filelib.fileExists(thisFilepath)){
|
|
|
+ //Add it to the cleaned tag records
|
|
|
+ cleanedTagRecords.push(JSON.parse(JSON.stringify(thisRecord)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return cleanedTagRecords;
|
|
|
+}
|
|
|
+
|