classify.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Aroz Photo
  3. Image Classification Script
  4. Generate classification for the given image path
  5. Paramters:
  6. ws (Optional), upgrade this connection to websocket for realtime progress viewing
  7. */
  8. requirelib("filelib");
  9. requirelib("imagelib");
  10. requirelib("websocket");
  11. includes("imagedb.js");
  12. function returnError(msg){
  13. sendJSONResp(JSON.stringify({"error": msg}));
  14. }
  15. function init(){
  16. newDBTableIfNotExists("photo");
  17. }
  18. function main(){
  19. var roots = getAllPossibleRoots();
  20. for (var i = 0; i < roots.length; i++){
  21. var thisVroot = roots[i][1];
  22. //Check if there is a lock for this tag file
  23. var lockFile = thisVroot + "Photo/tags.json.lock";
  24. if (filelib.fileExists(lockFile)){
  25. //Check if the file was > 24hs old. If yes, ignore it (maybe the previous rendering process crashed)
  26. var lockFileCreationTime = filelib.mtime(lockFile, true);
  27. if (Date.now()/1000 - lockFileCreationTime > 86400){
  28. //Delete the file and continue
  29. filelib.deleteFile(lockFile);
  30. }else{
  31. //Skip this vroot
  32. continue;
  33. }
  34. }
  35. //Create a lock file
  36. filelib.writeFile(lockFile, "")
  37. //Load all tags record for this vroot
  38. var tagsRecords = loadAllTagsRecord(thisVroot);
  39. //Clear up all images tags that no longer exists
  40. tagsRecords = matchAndClearNonExistsRecords(tagsRecords);
  41. //Convert it to a path:value keypair
  42. var filepathMap = summarizeAndRestructureFilepaths(tagsRecords);
  43. //sendResp(JSON.stringify(filepathMap));
  44. //Scan for new images that is not classified and add them to the root tag file
  45. var allValidImagesInThisDir = getAllImageFilesInRoot(thisVroot);
  46. var websocketMode = false;
  47. if (typeof(ws) != "undefined"){
  48. websocketMode = true;
  49. websocket.upgrade(10);
  50. delay(100);
  51. }
  52. var counter = 1;
  53. for ( var j = 0; j < allValidImagesInThisDir.length; j++){
  54. var thisImageFile = allValidImagesInThisDir[j];
  55. //Check if this filepath is already been classified.
  56. if (typeof(filepathMap[thisImageFile]) == "undefined"){
  57. //Not found in cache. New photo!
  58. console.log("[Photo] Analysising photo with " + getNNModel() + ": " + thisImageFile);
  59. var thisImageTagRecord = getImageTagsRecord(thisImageFile);
  60. if (websocketMode){
  61. websocket.send(JSON.stringify(thisImageFile));
  62. }
  63. //Push it into the record
  64. tagsRecords.push(thisImageTagRecord);
  65. counter++;
  66. }
  67. if (counter%5 == 0){
  68. //Auto save every 5 photos
  69. console.log("[Photo] Auto saved")
  70. saveAllTagsRecords(thisVroot, tagsRecords);
  71. }
  72. }
  73. //Final save
  74. saveAllTagsRecords(thisVroot, tagsRecords);
  75. //Delete lock file on this vroot
  76. filelib.deleteFile(lockFile);
  77. }
  78. console.log("[Photo] Automatic tag generation - Done")
  79. websocket.close();
  80. sendResp("OK");
  81. }
  82. init();
  83. main();