Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(google vision api): face detection change #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 89 additions & 107 deletions 9-photoanalysis/index.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,114 @@
var Twit = require('twit');
var fs = require('fs');
var request = require('request');
var vision = require('@google-cloud/vision')({
projectId: 'twitterbot',
keyFilename: './keyfile.json'
var Twit = require("twit");
var fs = require("fs");
var request = require("request");
var vision = require("@google-cloud/vision")({
projectId: "twitterbot",
keyFilename: "./keyfile.json"
});

var bot = new Twit({
consumer_key: process.env.LEARNINGBOT_CONSUMER_KEY,
consumer_secret: process.env.LEARNINGBOT_CONSUMER_SECRET,
access_token: process.env.LEARNINGBOT_ACCESS_TOKEN,
access_token_secret: process.env.LEARNINGBOT_ACCESS_TOKEN_SECRET,
timeout_ms: 60 * 1000
consumer_key: process.env.LEARNINGBOT_CONSUMER_KEY,
consumer_secret: process.env.LEARNINGBOT_CONSUMER_SECRET,
access_token: process.env.LEARNINGBOT_ACCESS_TOKEN,
access_token_secret: process.env.LEARNINGBOT_ACCESS_TOKEN_SECRET,
timeout_ms: 60 * 1000
});

function downloadPhoto(url, replyToName, tweetId){
var parameters = {
url: url,
encoding: 'binary'
};
request.get(parameters, function(err, response, body){
var filename = 'photo'+Date.now()+'.jpg';
fs.writeFile(filename, body, 'binary', function(err){
console.log('Downloaded photo.');
analyzePhoto(filename, replyToName, tweetId);
});
function downloadPhoto(url, replyToName, tweetId) {
var paramaters = {
url: url,
encoding: "binary"
};
request.get(paramaters, function(err, response, body) {
var filename = "photo" + Date.now() + ".jpg";
fs.writeFile(filename, body, "binary", function(err) {
console.log("Downloaded Photo.");
analyzePhoto(filename, replyToName, tweetId);
});
});
}

function analyzePhoto(filename, replyToName, tweetId){
vision.detectFaces(filename, function(err, faces){
var allEmotions = [];
faces.forEach(function(face){
extractFaceEmotions(face).forEach(function(emotion){
if (allEmotions.indexOf(emotion) === -1){
allEmotions.push(emotion);
}
});
});
postStatus(allEmotions, replyToName, tweetId);
function analyzePhoto(filename, replyToName, tweetId) {
var request = { source: { filename: filename } };
vision.faceDetection(request, function(err, faces) {
console.log("faces.faceAnnotations:", faces.faceAnnotations);
var allEmotions = [];
faces.faceAnnotations.forEach(function(face) {
extractFaceEmotions(face).forEach(function(emotion) {
if (allEmotions.indexOf(emotion) === -1) {
allEmotions.push(emotion);
}
});
});
postStatus(allEmotions, replyToName, tweetId);
});
}

function extractFaceEmotions(face){
var emotions = ['joy', 'anger', 'sorrow', 'surprise'];
return emotions.filter(function(emotion){
return face[emotion];
});
function extractFaceEmotions(face) {
var emotions = ["joyLikelihood", "sorrowLikelihood", "angerLikelihood", "surpriseLikelihood"];
return emotions.filter(function(emotion) {
console.log(face[emotion]);
return face[emotion] === "LIKELY" || face[emotion] === "VERY_LIKELY";
});
}

function postStatus(allEmotions, replyToName, tweetId){
var status = formatStatus(allEmotions, replyToName);
bot.post('statuses/update', {status: status, in_reply_to_status_id: tweetId}, function(err, data, response){
if (err){
console.log(err);
}else{
console.log('Bot has tweeted ' + status);
}
});
function postStatus(allEmotions, replyToName, tweetId) {
var status = formatStatus(allEmotions, replyToName);
bot.post(
"statuses/update",
{
status: status,
in_reply_to_status_id: tweetId
},
function(err, data, response) {
if (err) {
console.error(err);
} else {
console.log(`Bot has tweeted ${status}`);
}
}
);
}

function formatStatus(allEmotions, replyToName){
var reformatEmotions = {
joy: 'happy',
anger: 'angry',
surprise: 'surprised',
sorrow: 'sad'
};
var status = '@'+replyToName+' Looking ';
if (allEmotions.length>0){
allEmotions.forEach(function(emotion, i){
if (i === 0){
status = status + reformatEmotions[emotion];
}else{
status = status + ' and ' + reformatEmotions[emotion];
}
});
status = status + '!';
}else{
status = status + 'neutral!';
}
return status;
function formatStatus(allEmotions, replyToName) {
var reformatEmotions = {
joyLikelihood: "happy",
sorrowLikelihood: "sad",
angerLikelihood: "angry",
surpriseLikelihood: "surprised"
};
var status = `@${replyToName} looking`;
if (allEmotions.length > 0) {
allEmotions.forEach(function(emotion, i) {
if (i === 0) {
status = `${status} ${reformatEmotions[emotion]}`;
} else {
status = `${status} and ${reformatEmotions[emotion]}`;
}
});
} else {
status = `${status} neutral`;
}
status = `${status}!`;
return status;
}

var stream = bot.stream('statuses/filter', {track: '@bots_with_han'});
var stream = bot.stream("statuses/filter", { track: "@bots_with_han" });

stream.on('connecting', function(response){
console.log('connecting...');
stream.on("connecting", function(response) {
console.log("connecting...");
});

stream.on('connected', function(response){
console.log('connected!');
stream.on("connected", function(response) {
console.log("connected!");
});

stream.on('error', function(err){
console.log(err);
stream.on("error", function(err) {
console.log(err);
});

stream.on('tweet', function(tweet){
if (tweet.entities.media){
downloadPhoto(tweet.entities.media[0].media_url, tweet.user.screen_name, tweet.id_str);
}
stream.on("tweet", function(tweet) {
if (tweet.entities.media) {
downloadPhoto(tweet.entities.media[0].media_url, tweet.user.screen_name, tweet.id_str);
}
});