Browse Source

Fixed scheduler log filename

Toby Chui 3 years ago
parent
commit
f34b71a8c4
3 changed files with 89 additions and 32 deletions
  1. 1 1
      mod/time/scheduler/scheduler.go
  2. 12 30
      web/Photo/backend/classify.js
  3. 76 1
      web/Photo/backend/imagedb.js

+ 1 - 1
mod/time/scheduler/scheduler.go

@@ -295,7 +295,7 @@ func cronlog(message string) {
 	currentTime := time.Now()
 	timestamp := currentTime.Format("2006-01-02 15:04:05")
 	message = timestamp + " " + message
-	currentLogFile := filepath.ToSlash(filepath.Clean(logFolder)) + "/" + time.Now().Format("01-02-2006") + ".log"
+	currentLogFile := filepath.ToSlash(filepath.Clean(logFolder)) + "/" + time.Now().Format("2006-02-01") + ".log"
 	f, err := os.OpenFile(currentLogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
 	if err != nil {
 		//Unable to write to file. Log to STDOUT instead

+ 12 - 30
web/Photo/backend/classify.js

@@ -4,8 +4,6 @@
     Image Classification Script
     Generate classification for the given image path
 
-    Require Data: 
-    imagepath (string)
 */
 
 
@@ -18,36 +16,20 @@ function returnError(msg){
 }
 
 function main(){
-    //Check if the imagepath exists
-    if (!filelib.fileExists(imagepath)){
-        returnError("file not exists");
-        return
-    }
+    var roots = getAllPossibleRoots();
+    for (var i = 0; i < roots.length; i++){
+        var thisVroot = roots[i];
+        //Load all tags record for this vroot
+        var tagsRecords = loadAllTagsRecord(thisVroot);
 
-    //Check if it is a supported image format
-    var fileExt = imagepath.split(".").pop();
-    if (fileExt == "jpg" || fileExt == "png" || fileExt == "jpeg"){
-        //Get image classification, will take a bit time
-        var results = imagelib.classify(imagepath, "darknet19"); 
-        var responses = [];
-        for (var i = 0; i < results.length; i++){
-            responses.push({
-                "object": results[i].Name,
-                "confidence": results[i].Percentage,
-                "position_x": results[i].Positions[0],
-                "position_y": results[i].Positions[1],
-                "width": results[i].Positions[2],
-                "height": results[i].Positions[3]
-            });
-        }
-
-        sendJSONResp(JSON.stringify(responses));
-    }else{
-        //Not supported format
-        returnError("format not supported");
-        return
-    }
+        //Clear up all images tags that no longer exists
+        tagsRecords = matchAndClearNonExistsRecords(tagsRecords);
+        //Do resummarize of tags info
 
+        //Scan for new images that is not classified and add them to the root tag file
+
+        //
+    }
    
 }
 

+ 76 - 1
web/Photo/backend/imagedb.js

@@ -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;
+}
+