imagedb.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Image DB
  3. The get and put function for image classification database
  4. and its utilities
  5. */
  6. requirelib("filelib");
  7. requirelib("imagelib");
  8. //Tags record structure
  9. /*
  10. {
  11. "filepath": {image_vpath},
  12. "tags": [
  13. {
  14. "object": {detected_object_1},
  15. "confidence": {confidence_in_percentage}
  16. },
  17. {
  18. "object": {detected_object_2},
  19. "confidence": {confidence_in_percentage}
  20. }
  21. ]
  22. }
  23. */
  24. //Get all possible roots, return array of [name, path and photo root]
  25. function getAllPossibleRoots(){
  26. function folderContainSubFiles(filepath){
  27. var results = filelib.aglob(filepath + "/*", "default");
  28. if (results.length > 0){
  29. return true;
  30. }
  31. return false;
  32. }
  33. var possibleRoots = [];
  34. for ( var i = 0; i < USER_VROOTS.length; i++){
  35. var thisRoot = USER_VROOTS[i];
  36. if (thisRoot.Filesystem != "virtual" && filelib.fileExists(thisRoot.UUID + ":/Photo") && folderContainSubFiles(thisRoot.UUID + ":/Photo/")){
  37. possibleRoots.push([thisRoot.Name, thisRoot.UUID + ":/", thisRoot.UUID + ":/Photo"]);
  38. }
  39. }
  40. return possibleRoots;
  41. }
  42. function isSupportedImage(filename){
  43. var fileExt = filename.split(".").pop();
  44. if (fileExt == "jpg" || fileExt == "png" || fileExt == "jpeg"){
  45. return true;
  46. }else{
  47. return false
  48. }
  49. }
  50. //Get all the image files exists in *:/Photo/*
  51. function getAllImageFiles(){
  52. var results = [];
  53. var possibleRoots = getAllPossibleRoots();
  54. for ( var i = 0; i < possibleRoots.length; i++){
  55. var thisRootInfo = possibleRoots[i];
  56. var allFilesInThisPhotoRoot = filelib.walk(thisRootInfo[2]);
  57. for ( var j = 0; j < allFilesInThisPhotoRoot.length; j++){
  58. var thisFile = allFilesInThisPhotoRoot[j];
  59. if (!filelib.isDir(thisFile) && isSupportedImage(thisFile)){
  60. results.push(thisFile);
  61. }
  62. }
  63. }
  64. return results;
  65. }
  66. //Get the tag of a certain image file given its filepath
  67. function getImageTags(imagefile){
  68. var results = imagelib.classify(imagefile, "darknet19");
  69. var tags = [];
  70. for (var i = 0; i < results.length; i++){
  71. if (results[i].Percentage > 50){
  72. //Confidence larger than 50
  73. tags.push({
  74. "object": results[i].Name,
  75. "confidence":results[i].Percentage
  76. });
  77. }
  78. }
  79. return tags;
  80. }
  81. function getImageTagsRecord(imagefile){
  82. var tags = getImageTags(imagefile);
  83. return {
  84. "filepath": imagefile,
  85. "tags": tags
  86. }
  87. }
  88. function loadAllTagsRecord(rootID){
  89. var tagFile = rootID + ":/Photo/tags.json"
  90. if (filelib.fileExists(tagFile)){
  91. var tagsData = filelib.readFile(tagFile)
  92. return JSON.parse(tagsData);
  93. }
  94. return [];
  95. }
  96. function saveAllTagsRecords(rootID, tagRecords){
  97. var tagFile = rootID + ":/Photo/tags.json"
  98. return filelib.writeFile(tagFile, JSON.stringify(tagRecords))
  99. }
  100. //Clearn up the record from the list of tag records that its file no longer exists
  101. function matchAndClearNonExistsRecords(tagRecords){
  102. var cleanedTagRecords = [];
  103. for ( var i = 0; i < tagRecords.length; i++){
  104. var thisRecord = tagRecords[i];
  105. var thisFilepath = thisRecord.filepath;
  106. //Check if this file exists
  107. if (filelib.fileExists(thisFilepath)){
  108. //Add it to the cleaned tag records
  109. cleanedTagRecords.push(JSON.parse(JSON.stringify(thisRecord)));
  110. }
  111. }
  112. return cleanedTagRecords;
  113. }