imagedb.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. function getExcludeFolders(){
  57. newDBTableIfNotExists("image");
  58. var excludeList = readDBItem("image", "excludes");
  59. if (excludeList == ""){
  60. //Initialize the records
  61. writeDBItem("image", "excludes", JSON.stringify(["Manga","thumbnails"]));
  62. return ["Manga","thumbnails"];
  63. }else{
  64. return JSON.parse(excludeList);
  65. }
  66. }
  67. function setExcludeFolders(newFolderList){
  68. newDBTableIfNotExists("image");
  69. return writeDBItem("image", "excludes", JSON.stringify(newFolderList));
  70. }
  71. function getNNModel(){
  72. newDBTableIfNotExists("image");
  73. var nnmodel = readDBItem("image", "nnm");
  74. if (nnmodel == ""){
  75. return "yolo3";
  76. }else{
  77. return nnmodel;
  78. }
  79. }
  80. function setNNModel(newNNM){
  81. newDBTableIfNotExists("image");
  82. return writeDBItem("image", "nnm", newNNM);
  83. }
  84. //Check if this photo shd be rendered
  85. function checkIsInExcludeFolders(filename, excludeFiles){
  86. var excludeRootFolders = excludeFiles;
  87. var pathinfo = filename.split("/");
  88. if (pathinfo.length > 2){
  89. var basefolder = pathinfo[2];
  90. for (var i = 0; i < excludeRootFolders.length; i++){
  91. if (basefolder == excludeRootFolders[i]){
  92. return true;
  93. }
  94. }
  95. return false;
  96. }else{
  97. return false;
  98. }
  99. }
  100. //Get all the image files exists in *:/Photo/*
  101. function getAllImageFiles(){
  102. var results = [];
  103. var possibleRoots = getAllPossibleRoots();
  104. var excludeFiles = getExcludeFolders();
  105. for ( var i = 0; i < possibleRoots.length; i++){
  106. var thisRootInfo = possibleRoots[i];
  107. var allFilesInThisPhotoRoot = filelib.walk(thisRootInfo[2]);
  108. for ( var j = 0; j < allFilesInThisPhotoRoot.length; j++){
  109. var thisFile = allFilesInThisPhotoRoot[j];
  110. if (!filelib.isDir(thisFile) && isSupportedImage(thisFile) && !inCacheFolder(thisFile)){
  111. if (!checkIsInExcludeFolders(thisFile, excludeFiles)){
  112. results.push(thisFile);
  113. }
  114. }
  115. }
  116. }
  117. return results;
  118. }
  119. //Get all the image files exists in given root as rootID:/Photo/*
  120. function getAllImageFilesInRoot(targetRootID){
  121. var excludeFiles = getExcludeFolders();
  122. if (targetRootID.indexOf(":") >= 0){
  123. targetRootID = targetRootID.split(":")[0];
  124. }
  125. var results = [];
  126. var allFilesInThisPhotoRoot = filelib.walk(targetRootID + ":/Photo/");
  127. for ( var j = 0; j < allFilesInThisPhotoRoot.length; j++){
  128. var thisFile = allFilesInThisPhotoRoot[j];
  129. if (!filelib.isDir(thisFile) && isSupportedImage(thisFile) && !inCacheFolder(thisFile)){
  130. if (!checkIsInExcludeFolders(thisFile, excludeFiles)){
  131. results.push(thisFile);
  132. }
  133. }
  134. }
  135. return results;
  136. }
  137. //Get the tag of a certain image file given its filepath
  138. function getImageTags(imagefile){
  139. var results = imagelib.classify(imagefile, getNNModel());
  140. var tags = [];
  141. for (var i = 0; i < results.length; i++){
  142. console.log(results[i].Name, results[i].Percentage);
  143. if (results[i].Percentage > 10){
  144. //Confidence larger than 50
  145. tags.push({
  146. "object": results[i].Name,
  147. "confidence":results[i].Percentage
  148. });
  149. }
  150. }
  151. return tags;
  152. }
  153. function getImageTagsRecord(imagefile){
  154. var tags = getImageTags(imagefile);
  155. return {
  156. "filepath": imagefile,
  157. "tags": tags
  158. }
  159. }
  160. function loadAllTagsRecord(rootID){
  161. var tagFile = rootID + "Photo/tags.json"
  162. if (filelib.fileExists(tagFile)){
  163. var tagsData = filelib.readFile(tagFile)
  164. return JSON.parse(tagsData);
  165. }
  166. return [];
  167. }
  168. function saveAllTagsRecords(rootID, tagRecords){
  169. var tagFile = rootID + "Photo/tags.json"
  170. return filelib.writeFile(tagFile, JSON.stringify(tagRecords))
  171. }
  172. //Clearn up the record from the list of tag records that its file no longer exists
  173. function matchAndClearNonExistsRecords(tagRecords){
  174. var cleanedTagRecords = [];
  175. for ( var i = 0; i < tagRecords.length; i++){
  176. var thisRecord = tagRecords[i];
  177. var thisFilepath = thisRecord.filepath;
  178. //Check if this file exists
  179. if (filelib.fileExists(thisFilepath)){
  180. //Add it to the cleaned tag records
  181. cleanedTagRecords.push(JSON.parse(JSON.stringify(thisRecord)));
  182. }
  183. }
  184. return cleanedTagRecords;
  185. }
  186. //Translate the record array into keyvalue paris of [filepath](record object)
  187. function summarizeAndRestructureFilepaths(tagRecords){
  188. var filepathMap = {};
  189. for ( var i = 0; i < tagRecords.length; i++){
  190. var thisRecord = tagRecords[i];
  191. filepathMap[thisRecord.filepath] = JSON.parse(JSON.stringify(thisRecord));
  192. }
  193. return filepathMap;
  194. }
  195. //Translate the tag array into key-value pairs of [tag](filepath)
  196. function summarizeAndrestructureTags(tagRecords){
  197. var tags = {};
  198. for ( var i = 0; i < tagRecords.length; i++){
  199. var thisRecord = tagRecords[i];
  200. for ( var j = 0; j < thisRecord.tags.length; j++){
  201. var thisTag = thisRecord.tags[j];
  202. if (typeof(tags[thisTag.object]) == "undefined"){
  203. //Not exists. Create it
  204. tags[thisTag.object] = [thisRecord.filepath];
  205. }else{
  206. //Already exists. Remove duplicate
  207. var alreadyExists = false;
  208. for ( var k = 0; k < tags[thisTag.object].length; k++){
  209. if (tags[thisTag.object][k] == thisRecord.filepath){
  210. alreadyExists = true;
  211. }
  212. }
  213. if (!alreadyExists){
  214. tags[thisTag.object].push(thisRecord.filepath);
  215. }
  216. }
  217. }
  218. }
  219. return tags;
  220. }