Last active
November 19, 2017 16:55
-
-
Save savelee/48d8642ffe0a99348cfeca56485181c2 to your computer and use it in GitHub Desktop.
Google Cloud Function - On video upload, detect tags with Video Intelligence API
This file contains 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
/** | |
{ | |
kind: 'storage#object', | |
resourceState: 'exists', | |
id: 'leeboonstra-videoapi/mov_bbb.mp4/1490796080783211', | |
selfLink: 'https://www.googleapis.com/storage/v1/b/leeboonstra-videoapi/o/mov_bbb.mp4', | |
name: 'mov_bbb.mp4', | |
bucket: 'leeboonstra-videoapi', | |
generation: '1490796080783211', | |
metageneration: '1', | |
contentType: 'video/mp4', | |
timeCreated: '2017-03-29T14:01:20.649Z', | |
updated: '2017-03-29T14:01:20.649Z', | |
storageClass: 'MULTI_REGIONAL', | |
size: '788493', | |
md5Hash: 'MTk4OTE4ZjQwZWNjN2NhYjBmYzQyMzFhZGFmNjdjOTY=', | |
mediaLink: 'https://www.googleapis.com/storage/v1/b/leeboonstra-videoapi/o/mov_bbb.mp4?generation=1490796080783211&alt=media', | |
crc32c: 'uAuiBQ==' | |
}*/ | |
const Video = require('@google-cloud/videointelligence').v1beta1(); | |
const video = Video.videoIntelligenceServiceClient({ | |
projectId: process.env.GCLOUD_PROJECT, | |
keyFilename: process.env.GCLOUD_KEY_FILE //create a service account with JSON key | |
}); | |
const request = require('request'); | |
const BUCKET_NAME = "leeboonstra-videoapi"; | |
exports.videoapi = function videoapi(event, callback){ | |
var d = event.data; | |
console.log(event.timestamp); | |
console.log(d.bucket + "-" + d.contentType); | |
if(d.bucket == BUCKET_NAME && d.contentType == "video/mp4"){ | |
let tags = []; | |
let formData = { | |
"inputUri": 'gs://' + d.bucket + "/" + d.name, | |
"features": [ | |
"LABEL_DETECTION" | |
] | |
}; | |
//"SHOT_CHANGE_DETECTION", | |
//"FACE_DETECTION" | |
console.log(formData); | |
video.annotateVideo(formData) | |
.then((startResponse) => { | |
//console.log(startResponse); | |
var operation = startResponse[0]; | |
console.log('Waiting for operation to complete...'); | |
return operation.promise(); | |
}) | |
.then((doneResponse) => { | |
//get the labels | |
console.log(doneResponse); | |
var labels = doneResponse[0].annotationResults[0]; | |
for (key in labels){ | |
if(key == "faceAnnotations"){ | |
labels[key].forEach((face, faceIdx) => { | |
//console.log('Thumbnail size:', face.thumbnail.buffer.length); | |
face.segments.forEach((segment, segmentIdx) => { | |
tags.push(faceIdx); | |
//console.log(`Track ${segmentIdx} of face ${faceIdx}: frames ${segment.startTimeOffset} to ${segment.endTimeOffset}`); | |
}); | |
}); | |
} | |
if(key == "labelAnnotations"){ | |
labels[key].forEach((label) => { | |
//console.log('Label description:', label.description); | |
tags.push(label.description); | |
//console.log('Locations:'); | |
label.locations.forEach((location) => { | |
//console.log(`\tFrames ${location.segment.startTimeOffset} to ${location.segment.endTimeOffset}`); | |
}); | |
}); | |
} | |
if(key == "shotAnnotations"){ | |
labels[key].forEach((shot, shotIdx) => { | |
tags.push(shot); | |
//console.log(`Scene ${shotIdx}:`); | |
//console.log(`\tStart: ${shot.startTimeOffset}`); | |
//console.log(`\tEnd: ${shot.endTimeOffset}`); | |
}); | |
} | |
} | |
var vid = {}; | |
vid.name = d.name; | |
vid.selfLink = 'gs://' + d.bucket + "/" + d.name; | |
vid.bucket = d.bucket; | |
vid.timeCreated = d.timeCreated; | |
vid.tags = tags; | |
console.log(vid); | |
request.post({ | |
url: "https://us-central1-leeboonstra-blogdemos.cloudfunctions.net/datastore/", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body: vid, | |
json: true | |
}, function(error, response, body){ | |
callback(); | |
}) | |
}).catch(function(e) { | |
console.log("something goes wrong"); | |
console.log(e); // This is never called | |
}); | |
} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment