Skip to content

Instantly share code, notes, and snippets.

@pepetox
Created May 21, 2017 06:57
Show Gist options
  • Save pepetox/de5cb5b81bf7d423f5d3444676c14b1f to your computer and use it in GitHub Desktop.
Save pepetox/de5cb5b81bf7d423f5d3444676c14b1f to your computer and use it in GitHub Desktop.
Drive-gas-vision-api-clasificator
//The next recipe shows how to use cloud vision api https://cloud.google.com/vision/ in order to classify images in one drive folder
//process configuration
var api_key = "AIzaSyXXXXXXXXXXXX" //api key of a cloud project with vision api enabled
var origin_folder_id = "0B4_0XXXXXXXXXXXXE" //Drive id of the origin folder
var clasification_tag_one = 'printer'; //first label to classify
var clasification_tag_two = 'cat'; //second label to classify
var clasification_tag_three = 'laptop';//third label to classify
var result_folder_one_id = "0B4_0XXXXXXXXXXXX" //Drive id of the folder to put in the first label images
var result_folder_two_id = "0B4_0XXXXXXXXXXXX" //Drive id of the folder to put in the second label images
var result_folder_three_id = "0B4_0cXXXXXXXXXXXX" //Drive id of the folder to put in the third label images
function classify() {
//load the folder for the process
var my_origin_folder = DriveApp.getFolderById(origin_folder_id);
var my_result_folder_one = DriveApp.getFolderById(result_folder_one_id);
var my_result_folder_two = DriveApp.getFolderById(result_folder_two_id);
var my_result_folder_three = DriveApp.getFolderById(result_folder_three_id);
//obtain all the files of the origin folder
var files = my_origin_folder.getFiles();
//iterate all our images
while (files.hasNext()) {
var my_file = files.next(); //get the file
var my_blob = my_file.getBlob(); //Transform to blob
var encoded = Utilities.base64Encode(my_blob.getBytes()); //Encode the blob in b64
var type= "LABEL_DETECTION"; //feature for the vision api
//prepare the data of the vision api request
var data = "{\n \"requests\": [\n {\n \"image\": {\n \"content\": \""+encoded+"\"\n },\n \"features\": [\n {\n \"type\": \""+type+"\"\n }\n ]\n }\n ]\n}";
//add the api key to the api url call
var url = "https://vision.googleapis.com/v1/images:annotate?key=" + api_key;
//prepare the call to the api
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : data
};
//using UrlFetchApp we can call external url from GAS
var response = UrlFetchApp.fetch(url, options);
Logger.log(response)
try{
//Take the respond and parse to JSON
var text_result = JSON.parse(response.getContentText()).responses[0].labelAnnotations[0].description;
//if the first label match with one of our three label move de file to the folder destiny
if(text_result===clasification_tag_one){
my_result_folder_one.addFile(my_file);
my_origin_folder.removeFile(my_file);
}
if(text_result===clasification_tag_two){
my_result_folder_two.addFile(my_file);
my_origin_folder.removeFile(my_file);
}
if(text_result===clasification_tag_three){
my_result_folder_three.addFile(my_file);
my_origin_folder.removeFile(my_file);
}
}
catch(e){
//if somethig is work let´s log for information
Logger.log("Error processing the file.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment