Skip to content

Instantly share code, notes, and snippets.

@ZackAkil
Created May 25, 2020 12:18
Show Gist options
  • Save ZackAkil/c3488f8a9afd16e200c2162a6daced9b to your computer and use it in GitHub Desktop.
Save ZackAkil/c3488f8a9afd16e200c2162a6daced9b to your computer and use it in GitHub Desktop.
const drive_url_col = 2
const ouput_text_col = 3
function myFunction(e) {
var row_added = e.range.getRow()
Logger.log(row_added)
var drive_url = SpreadsheetApp.getActiveSheet().getRange(row_added, drive_url_col).getValue();
Logger.log(drive_url)
//get image from drive
var id = drive_url.split('=')[1]
var file = DriveApp.getFileById(id)
var file_bytes = file.getBlob().getBytes()
var api_response = CloudVisionAPI(file_bytes)
Logger.log(api_response)
SpreadsheetApp.getActiveSheet().getRange(row_added, ouput_text_col).setValue(api_response.join(', '))
}
function CloudVisionAPI(image_bytes) {
const payload = JSON.stringify({
requests: [{
image: {
content: Utilities.base64Encode(image_bytes)
},
features: [
{
type:"LABEL_DETECTION",
maxResults: 10
},
// {
// type: "TEXT_DETECTION"
// },
// {
// type: "LOGO_DETECTION",
// maxResults: 10
// }
]
}]
});
const url = 'https://vision.googleapis.com/v1/images:annotate?key=' + API_KEY;
const response = UrlFetchApp.fetch(url, {
method: 'POST',
contentType: 'application/json',
payload: payload,
muteHttpExceptions: true
}).getContentText();
const json_resp = JSON.parse(response)
Logger.log(json_resp)
var labels = []
json_resp.responses[0].labelAnnotations.forEach(function (label) {
labels.push(label.description)
})
return labels
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment