Created
September 30, 2018 21:36
-
-
Save abfo/34a3e1cc520903830d8ffa6e66cd2f9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var OAuthCreds = { | |
"type": "service_account", | |
//... | |
}; | |
var SendEmailTo = ''; | |
var MonitorImageUrl = ''; | |
function main() { | |
var timestamp = Date.now().toString(); | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
var currentProps = scriptProperties.getProperties(); | |
Logger.log('Grabbing a frame'); | |
var url = MonitorImageUrl + '&cb=' + timestamp; | |
var response = UrlFetchApp.fetch(url); | |
var image = response.getBlob(); | |
image.setName('image.jpg'); | |
var bytes = image.getBytes(); | |
var encodedImage = Utilities.base64EncodeWebSafe(bytes); | |
Logger.log('Calling cloud vision'); | |
var service = getService(); | |
if (service.hasAccess()) { | |
var request = { | |
"requests":[ | |
{ | |
"image":{ | |
"content": encodedImage | |
}, | |
"features":[ | |
{ | |
"type": "LABEL_DETECTION", | |
"maxResults":50 | |
} | |
] | |
} | |
] | |
} | |
var annotateUrl = 'https://vision.googleapis.com/v1/images:annotate'; | |
var annotateResponse = UrlFetchApp.fetch(annotateUrl, { | |
"headers": { | |
Authorization: 'Bearer ' + service.getAccessToken() | |
}, | |
"method" : "post", | |
"contentType" : "application/json", | |
"payload" : JSON.stringify(request, null, 2) | |
}); | |
var json = JSON.parse(annotateResponse.getContentText()); | |
var anythingNew = false; | |
var newText = ''; | |
for (var l = 0; l < json.responses[0].labelAnnotations.length; l++) { | |
var description = json.responses[0].labelAnnotations[l].description; | |
var score = json.responses[0].labelAnnotations[l].score; | |
if (!(description in currentProps)) { | |
Logger.log('Found new feature: ' + description); | |
scriptProperties.setProperty(description, score); | |
anythingNew = true; | |
newText += 'Found: ' + description + ' (score: ' + score + ')\r\n'; | |
} | |
} | |
if (anythingNew) { | |
MailApp.sendEmail(SendEmailTo, 'Found something new on the webcam ' + new Date(), newText, { | |
attachments: [image] | |
}); | |
} | |
} else { | |
Logger.log(service.getLastError()); | |
} | |
} | |
// modified from https://github.com/googlesamples/apps-script-oauth2/blob/master/samples/GoogleServiceAccount.gs#L50 below... | |
function getService() { | |
return OAuth2.createService('CloudVision') | |
// Set the endpoint URL. | |
.setTokenUrl(OAuthCreds.token_uri) | |
// Set the private key and issuer. | |
.setPrivateKey(OAuthCreds.private_key) | |
.setIssuer(OAuthCreds.client_email) | |
// Set the name of the user to impersonate. This will only work for | |
// Google Apps for Work/EDU accounts whose admin has setup domain-wide | |
// delegation: | |
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority | |
// .setSubject(USER_EMAIL) | |
// Set the property store where authorized tokens should be persisted. | |
.setPropertyStore(PropertiesService.getScriptProperties()) | |
// Set the scope. This must match one of the scopes configured during the | |
// setup of domain-wide delegation. | |
.setScope('https://www.googleapis.com/auth/cloud-platform'); | |
} | |
function reset() { | |
var service = getService(); | |
service.reset(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment