classify.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. //Load all tags record for this vroot
  23. var tagsRecords = loadAllTagsRecord(thisVroot);
  24. //Clear up all images tags that no longer exists
  25. tagsRecords = matchAndClearNonExistsRecords(tagsRecords);
  26. //Convert it to a path:value keypair
  27. var filepathMap = summarizeAndRestructureFilepaths(tagsRecords);
  28. //sendResp(JSON.stringify(filepathMap));
  29. //Scan for new images that is not classified and add them to the root tag file
  30. var allValidImagesInThisDir = getAllImageFiles();
  31. var websocketMode = false;
  32. if (typeof(ws) != "undefined"){
  33. websocketMode = true;
  34. websocket.upgrade(10);
  35. delay(100);
  36. }
  37. var counter = 1;
  38. for ( var i = 0; i < allValidImagesInThisDir.length; i++){
  39. var thisImageFile = allValidImagesInThisDir[i];
  40. //Check if this filepath is already been classified.
  41. if (typeof(filepathMap[thisImageFile]) == "undefined"){
  42. //Not found in cache. New photo!
  43. console.log("[Photo] Analysising photo with neuralnet: " + thisImageFile);
  44. var thisImageTagRecord = getImageTagsRecord(thisImageFile);
  45. if (websocketMode){
  46. websocket.send(JSON.stringify(thisImageFile));
  47. }
  48. //Push it into the record
  49. tagsRecords.push(thisImageTagRecord);
  50. counter++;
  51. }
  52. if (counter%5 == 0){
  53. //Auto save every 5 photos
  54. console.log("[Photo] Auto saved")
  55. saveAllTagsRecords(thisVroot, tagsRecords);
  56. }
  57. }
  58. //Final save
  59. saveAllTagsRecords(thisVroot, tagsRecords);
  60. }
  61. console.log("[Photo] Automatic tag generation - Done")
  62. websocket.close();
  63. sendResp("OK");
  64. }
  65. init();
  66. main();