B-roll computational photography
Visual recognition in video -draft notes
//Included libraries
var glob = require('glob');
var watson = require('watson-developer-cloud');
var fs = require('fs-extra');
var sync = require('child_process').spawnSync;
//Visual Recognition API key setup
var visual_recognition = watson.visual_recognition({
api_key: [YOUR API KEY HERE], // Demo Credentials.
version: 'v3',
version_date: '2016-05-20'
});
//Cut the video into frames: One every 30 frames
const procVid = sync('ffmpeg', ["-i" ,"video2.mp4","-vsync" ,"0", "-vf","select='not(mod(n,30))'","image_%d.jpg"]);
//Initialize the arrays and lists
var files= glob.sync('*.jpg' );
var x = files.length;
var j =0;
tags = []
//Tally up the results
function count(tags){
var counts = {};
for(var k = 0;k<tags.length;k++){
counts[tags[k]] = (counts[tags[k]] || 0)+1;
if(k==tags.length-1){
console.log(counts)
}
}
}
//Iterate over the list of images
for(var i =0;i<x;i++){
//Build request with the image you want to tag
var params = {
images_file: fs.createReadStream(files[i])
};
//Send image to tag to the Classify endpoint
visual_recognition.classify(params, function(err, res) {
if (err){
console.log(err);
}
//Error Checking
if(res.images[0].hasOwnProperty('error')||res.images[0]['classifiers'][0]['classes'].length ==0){
tags.push("NA");
j+=1
}
//No errors: continue with storing tags
else {
tags.push(res.images[0]['classifiers'][0]['classes'][0]['class']);
console.log("Done Getting tags for: "+res.images[0].image);
j+=1;
}
//Once you're done with the tags, count them up
if(j==x){count(tags)}
});
}Last updated