123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- /*
- Image DB
- The get and put function for image classification database
- and its utilities
- */
- requirelib("filelib");
- requirelib("imagelib");
- //Tags record structure
- /*
- {
- "filepath": {image_vpath},
- "tags": [
- {
- "object": {detected_object_1},
- "confidence": {confidence_in_percentage}
- },
- {
- "object": {detected_object_2},
- "confidence": {confidence_in_percentage}
- }
- ]
- }
- */
- //Get all possible roots, return array of [name, path and photo root]
- function getAllPossibleRoots(){
- function folderContainSubFiles(filepath){
- var results = filelib.aglob(filepath + "/*", "default");
- if (results.length > 0){
- return true;
- }
- return false;
- }
- var possibleRoots = [];
- for ( var i = 0; i < USER_VROOTS.length; i++){
- var thisRoot = USER_VROOTS[i];
- if (thisRoot.Filesystem != "virtual" && filelib.fileExists(thisRoot.UUID + ":/Photo") && folderContainSubFiles(thisRoot.UUID + ":/Photo/")){
- possibleRoots.push([thisRoot.Name, thisRoot.UUID + ":/", thisRoot.UUID + ":/Photo"]);
- }
- }
- return possibleRoots;
- }
- function isSupportedImage(filename){
- var fileExt = filename.split(".").pop();
- if (fileExt == "jpg" || fileExt == "png" || fileExt == "jpeg"){
- return true;
- }else{
- return false
- }
- }
- //Get all the image files exists in *:/Photo/*
- function getAllImageFiles(){
- var results = [];
- var possibleRoots = getAllPossibleRoots();
- for ( var i = 0; i < possibleRoots.length; i++){
- var thisRootInfo = possibleRoots[i];
- var allFilesInThisPhotoRoot = filelib.walk(thisRootInfo[2]);
- for ( var j = 0; j < allFilesInThisPhotoRoot.length; j++){
- var thisFile = allFilesInThisPhotoRoot[j];
- if (!filelib.isDir(thisFile) && isSupportedImage(thisFile)){
- results.push(thisFile);
- }
- }
- }
- return results;
- }
- //Get the tag of a certain image file given its filepath
- function getImageTags(imagefile){
- var results = imagelib.classify(imagefile, "darknet19");
- var tags = [];
- for (var i = 0; i < results.length; i++){
- if (results[i].Percentage > 50){
- //Confidence larger than 50
- tags.push({
- "object": results[i].Name,
- "confidence":results[i].Percentage
- });
- }
- }
- return tags;
- }
- function getImageTagsRecord(imagefile){
- var tags = getImageTags(imagefile);
- return {
- "filepath": imagefile,
- "tags": tags
- }
- }
- function loadAllTagsRecord(rootID){
- var tagFile = rootID + ":/Photo/tags.json"
- if (filelib.fileExists(tagFile)){
- var tagsData = filelib.readFile(tagFile)
- return JSON.parse(tagsData);
- }
- return [];
- }
- function saveAllTagsRecords(rootID, tagRecords){
- var tagFile = rootID + ":/Photo/tags.json"
- return filelib.writeFile(tagFile, JSON.stringify(tagRecords))
- }
- //Clearn up the record from the list of tag records that its file no longer exists
- function matchAndClearNonExistsRecords(tagRecords){
- var cleanedTagRecords = [];
- for ( var i = 0; i < tagRecords.length; i++){
- var thisRecord = tagRecords[i];
- var thisFilepath = thisRecord.filepath;
- //Check if this file exists
- if (filelib.fileExists(thisFilepath)){
- //Add it to the cleaned tag records
- cleanedTagRecords.push(JSON.parse(JSON.stringify(thisRecord)));
- }
- }
- return cleanedTagRecords;
- }
|