imagedb.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. function inCacheFolder(filename){
  51. if (filename.indexOf(".cache") >= 0){
  52. return true;
  53. }
  54. return false;
  55. }
  56. //Get all the image files exists in *:/Photo/*
  57. function getAllImageFiles(){
  58. var results = [];
  59. var possibleRoots = getAllPossibleRoots();
  60. for ( var i = 0; i < possibleRoots.length; i++){
  61. var thisRootInfo = possibleRoots[i];
  62. var allFilesInThisPhotoRoot = filelib.walk(thisRootInfo[2]);
  63. for ( var j = 0; j < allFilesInThisPhotoRoot.length; j++){
  64. var thisFile = allFilesInThisPhotoRoot[j];
  65. if (!filelib.isDir(thisFile) && isSupportedImage(thisFile) && !inCacheFolder(thisFile)){
  66. results.push(thisFile);
  67. }
  68. }
  69. }
  70. return results;
  71. }
  72. //Get the tag of a certain image file given its filepath
  73. function getImageTags(imagefile){
  74. var results = imagelib.classify(imagefile, "yolo3");
  75. var tags = [];
  76. for (var i = 0; i < results.length; i++){
  77. console.log(results[i].Name, results[i].Percentage);
  78. if (results[i].Percentage > 10){
  79. //Confidence larger than 50
  80. tags.push({
  81. "object": results[i].Name,
  82. "confidence":results[i].Percentage
  83. });
  84. }
  85. }
  86. return tags;
  87. }
  88. function getImageTagsRecord(imagefile){
  89. var tags = getImageTags(imagefile);
  90. return {
  91. "filepath": imagefile,
  92. "tags": tags
  93. }
  94. }
  95. function loadAllTagsRecord(rootID){
  96. var tagFile = rootID + "Photo/tags.json"
  97. if (filelib.fileExists(tagFile)){
  98. var tagsData = filelib.readFile(tagFile)
  99. return JSON.parse(tagsData);
  100. }
  101. return [];
  102. }
  103. function saveAllTagsRecords(rootID, tagRecords){
  104. var tagFile = rootID + "Photo/tags.json"
  105. return filelib.writeFile(tagFile, JSON.stringify(tagRecords))
  106. }
  107. //Clearn up the record from the list of tag records that its file no longer exists
  108. function matchAndClearNonExistsRecords(tagRecords){
  109. var cleanedTagRecords = [];
  110. for ( var i = 0; i < tagRecords.length; i++){
  111. var thisRecord = tagRecords[i];
  112. var thisFilepath = thisRecord.filepath;
  113. //Check if this file exists
  114. if (filelib.fileExists(thisFilepath)){
  115. //Add it to the cleaned tag records
  116. cleanedTagRecords.push(JSON.parse(JSON.stringify(thisRecord)));
  117. }
  118. }
  119. return cleanedTagRecords;
  120. }
  121. //Translate the record array into keyvalue paris of [filepath](record object)
  122. function summarizeAndRestructureFilepaths(tagRecords){
  123. var filepathMap = {};
  124. for ( var i = 0; i < tagRecords.length; i++){
  125. var thisRecord = tagRecords[i];
  126. filepathMap[thisRecord.filepath] = JSON.parse(JSON.stringify(thisRecord));
  127. }
  128. return filepathMap;
  129. }
  130. //Translate the tag array into key-value pairs of [tag](filepath)
  131. function summarizeAndrestructureTags(tagRecords){
  132. var tags = {};
  133. for ( var i = 0; i < tagRecords.length; i++){
  134. var thisRecord = tagRecords[i];
  135. for ( var j = 0; j < thisRecord.tags.length; j++){
  136. var thisTag = thisRecord.tags[j];
  137. if (typeof(tags[thisTag.object]) == "undefined"){
  138. //Not exists. Create it
  139. tags[thisTag.object] = [thisRecord.filepath];
  140. }else{
  141. //Already exists. Remove duplicate
  142. var alreadyExists = false;
  143. for ( var k = 0; k < tags[thisTag.object].length; k++){
  144. if (tags[thisTag.object][k] == thisRecord.filepath){
  145. alreadyExists = true;
  146. }
  147. }
  148. if (!alreadyExists){
  149. tags[thisTag.object].push(thisRecord.filepath);
  150. }
  151. }
  152. }
  153. }
  154. return tags;
  155. }