Created
February 9, 2016 08:40
-
-
Save mousetraps/1630e9ed38879a717912 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 express = require('express'); | |
var router = express.Router(); | |
/* GET home page. */ | |
router.get('/', function (req, res, next) { | |
res.render('index', { title: 'Bobblehead Generator' }); | |
}); | |
var oxford = require('project-oxford'); | |
var client = new oxford.Client(process.env.OXFORD_API); | |
var fs = require('fs'); | |
var GIFEncoder = require('gifencoder'); | |
var pngFileStream = require('png-file-stream'); | |
var Jimp = require("jimp"); | |
function cropHeadAndPasteRotated(inputFile, faceRectangle, degrees, outputName) { | |
return new Promise (function (resolve, reject) { | |
Jimp.read(inputFile).then(function (image) { | |
// Face detection only capture sa small portion of the face, | |
// so compensate for this by expanding the area appropriately. | |
var height = faceRectangle['height']; | |
var top = faceRectangle['top'] - height * 0.5; | |
height *= 1.6; | |
var left = faceRectangle['left']; | |
var width = faceRectangle['width']; | |
// Crop head, scale up slightly, rotate, and paste on original image | |
image.crop(left, top, width, height) | |
.scale(1.05) | |
.rotate(degrees, function(err, rotatedImage) { | |
Jimp.read(inputFile).then(function (original) { | |
original.composite(rotatedImage, left - 0.1*width, top - 0.05*height) | |
.write(outputName, function () { | |
resolve([original.bitmap.width, original.bitmap.height]); | |
}); | |
}); | |
}); | |
}); | |
}); | |
} | |
router.post('/', function (req, res, next) { | |
Promise.resolve(req.file.path) | |
.then(function detectFace(image) { | |
console.log("TODO: detect face using Oxford API.") | |
return client.face.detect({ | |
path: image, | |
analyzesAge: true, | |
analyzesGender: true | |
}); | |
}) | |
.then(function generateBobblePermutations (response) { | |
console.log("TODO: generate multiple images with head rotated.") | |
var promises = []; | |
var degrees = [10, 0, -10]; | |
for (var i = 0; i < degrees.length; i++) { | |
var outputName = req.file.path + '-' + i + '.png'; | |
promises.push(cropHeadAndPasteRotated(req.file.path, response[0].faceRectangle, degrees[i], outputName)) | |
} | |
return Promise.all(promises); | |
}) | |
.then(function generateGif (dimensions) { | |
console.log('TODO: generate GIF') | |
return new Promise(function (resolve, reject) { | |
var encoder = new GIFEncoder(dimensions[0][0], dimensions[0][1]); | |
pngFileStream(req.file.path + '-?.png') | |
.pipe(encoder.createWriteStream({ repeat: 0, delay: 500 })) | |
.pipe(fs.createWriteStream(req.file.path + '.gif')) | |
.on('finish', function () { | |
resolve(req.file.path + '.gif'); | |
}); | |
}) | |
return req.file.path; | |
}).then(function displayGif(gifLocation) { | |
res.render('index', { title: 'Done!', image: gifLocation }) | |
}); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment