├── overlays ├── beard.png ├── clown.png ├── jason.png ├── rohan.png ├── sopa.png ├── batman.png ├── googly.png ├── hipster.png ├── monocle.png ├── phantom.png ├── scumbag.png ├── disapprove.png ├── disguise.png ├── eyepatch.png ├── mustache.png └── mustache2.png ├── public ├── image │ ├── jon.jpg │ ├── casey.jpeg │ ├── derek.png │ ├── noise.png │ ├── wade.jpeg │ └── tom-hanks.jpg ├── images │ └── fail.png ├── fonts │ └── OpenSans-Semibold.ttf ├── stylesheets │ └── style.css └── index.html ├── server.js ├── package.json ├── routes └── index.js ├── transforms.js ├── LICENSE ├── app.js ├── lib ├── util.js └── image.js └── mapping.coffee /overlays/beard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/beard.png -------------------------------------------------------------------------------- /overlays/clown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/clown.png -------------------------------------------------------------------------------- /overlays/jason.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/jason.png -------------------------------------------------------------------------------- /overlays/rohan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/rohan.png -------------------------------------------------------------------------------- /overlays/sopa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/sopa.png -------------------------------------------------------------------------------- /overlays/batman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/batman.png -------------------------------------------------------------------------------- /overlays/googly.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/googly.png -------------------------------------------------------------------------------- /overlays/hipster.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/hipster.png -------------------------------------------------------------------------------- /overlays/monocle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/monocle.png -------------------------------------------------------------------------------- /overlays/phantom.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/phantom.png -------------------------------------------------------------------------------- /overlays/scumbag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/scumbag.png -------------------------------------------------------------------------------- /public/image/jon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/image/jon.jpg -------------------------------------------------------------------------------- /overlays/disapprove.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/disapprove.png -------------------------------------------------------------------------------- /overlays/disguise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/disguise.png -------------------------------------------------------------------------------- /overlays/eyepatch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/eyepatch.png -------------------------------------------------------------------------------- /overlays/mustache.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/mustache.png -------------------------------------------------------------------------------- /overlays/mustache2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/overlays/mustache2.png -------------------------------------------------------------------------------- /public/image/casey.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/image/casey.jpeg -------------------------------------------------------------------------------- /public/image/derek.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/image/derek.png -------------------------------------------------------------------------------- /public/image/noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/image/noise.png -------------------------------------------------------------------------------- /public/image/wade.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/image/wade.jpeg -------------------------------------------------------------------------------- /public/images/fail.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/images/fail.png -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | require('./app').listen(process.env.PORT || 3000, process.env.LHOST || undefined) 2 | -------------------------------------------------------------------------------- /public/image/tom-hanks.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/image/tom-hanks.jpg -------------------------------------------------------------------------------- /public/fonts/OpenSans-Semibold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/defunkt/faceup/master/public/fonts/OpenSans-Semibold.ttf -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "faceup", 3 | "version": "0.0.1", 4 | "private": true, 5 | "dependencies": { 6 | "express": "2.5.0", 7 | "jade": ">= 0.0.1", 8 | "imagemagick": "~0.1.2", 9 | "request": "~2.1.1", 10 | "buffers": "~0.1.1", 11 | "async": "~0.1.12", 12 | "coffee-script": "~1.1.2", 13 | "lru-cache": "~1.0.4" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var image = require('../lib/image') 2 | 3 | exports.index = function(req, res, next) { 4 | return res.sendfile('public/index.html') 5 | } 6 | 7 | exports.img = function(req, res, next) { 8 | var data = { 9 | image: req.param('src', 'http://www.librarising.com/astrology/celebs/images2/QR/queenelizabethii.jpg'), 10 | overlay: req.param('overlay', 'mustache') 11 | } 12 | 13 | image.mash(data, function(err, image) { 14 | if (err) { 15 | console.log(err.stack || err) 16 | return res.sendfile('public/images/fail.png') 17 | } 18 | if (!image) { 19 | return res.sendfile('public/images/fail.png') 20 | } 21 | res.send(image, {'Content-Type': 'image/jpeg'}, 200) 22 | }) 23 | }; 24 | -------------------------------------------------------------------------------- /transforms.js: -------------------------------------------------------------------------------- 1 | var util = require('./lib/util') 2 | , x = util.x 3 | , y = util.y 4 | 5 | exports.srt = function(func) { 6 | return function(face, photo) { 7 | var coords = func.call(this, face, photo) 8 | return ['SRT', coords.map(function(part, i) { 9 | if (i == 3) { 10 | part = [x(part), y(part)] 11 | } 12 | return (Array.isArray(part) ? part.join(',') : part) 13 | }).join(' ')] 14 | } 15 | } 16 | exports.affine = function(func) { 17 | return function(face, photo) { 18 | var result = func.call(this, face, photo) 19 | return ['Affine', result.map(function(part, i) { 20 | // Convert the destination coords from percentages to pixels 21 | if (part.length == 3) { 22 | part = [part[0], part[1], x(part[2]), y(part[2])] 23 | } 24 | 25 | return part[0]+','+part[1]+' '+part[2]+','+part[3] 26 | }).join(' ')] 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Wade Simmons 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | if (!process.env.FACE_API_KEY || !process.env.FACE_API_SECRET) { 2 | throw new Error("Need to set FACE_API_KEY and FACE_API_SECRET") 3 | } 4 | 5 | /** 6 | * Module dependencies. 7 | */ 8 | 9 | var express = require('express') 10 | , routes = require('./routes') 11 | 12 | var app = module.exports = express.createServer(); 13 | 14 | // Configuration 15 | 16 | app.configure(function(){ 17 | app.set('views', __dirname + '/views'); 18 | app.set('view engine', 'jade'); 19 | app.use(express.bodyParser()); 20 | app.use(express.methodOverride()); 21 | app.use(app.router); 22 | app.use(express.static(__dirname + '/public')); 23 | }); 24 | 25 | app.configure('development', function(){ 26 | app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 27 | }); 28 | 29 | app.configure('production', function(){ 30 | app.use(express.errorHandler()); 31 | }); 32 | 33 | // Routes 34 | 35 | app.get('/', routes.index); 36 | app.get('/img', routes.img); 37 | app.get('/img.jpg', routes.img); 38 | app.get('/img.jpeg', routes.img); 39 | 40 | if (require.main === module) { 41 | app.listen(process.env.PORT || 3000); 42 | console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); 43 | } 44 | -------------------------------------------------------------------------------- /lib/util.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var LRU = require("lru-cache") 4 | , lru = LRU(100) 5 | 6 | var loading = {} 7 | 8 | var x = exports.x = function(e) { 9 | return e.x || e[0] 10 | } 11 | 12 | var y = exports.y = function(e) { 13 | return e.y || e[1] 14 | } 15 | 16 | exports.distance = function(a, b) { 17 | return Math.sqrt( 18 | Math.pow(Math.abs(x(a) - x(b)), 2) + 19 | Math.pow(Math.abs(y(a) - y(b)), 2) 20 | ) 21 | } 22 | 23 | exports.angle = function(a, b) { 24 | return Math.atan( 25 | (y(b) - y(a)) / 26 | (x(b) - x(a)) 27 | ) / Math.PI * 180 28 | } 29 | 30 | exports.middle = function(a, b) { 31 | return [ 32 | (x(b) - x(a)) * 0.5 + x(a), 33 | (y(b) - y(a)) * 0.5 + y(a) 34 | ] 35 | } 36 | 37 | exports.cache = function(data, loader, callback) { 38 | var key = JSON.stringify(data) 39 | 40 | var result = lru.get(key) 41 | if (result) { 42 | return callback.apply(null, result) 43 | } 44 | 45 | if (loading[key]) { 46 | loading[key].push(callback) 47 | } else { 48 | loading[key] = [callback] 49 | loader(data, function(err) { 50 | var args = arguments 51 | , callbacks = loading[key] 52 | 53 | delete loading[key] 54 | 55 | if (!err) { 56 | lru.set(key, args) 57 | } 58 | 59 | callbacks.forEach(function(callback) { 60 | callback.apply(null, args) 61 | }) 62 | }) 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /mapping.coffee: -------------------------------------------------------------------------------- 1 | {affine, srt} = require './transforms' 2 | 3 | # -- AFFINE 4 | # affine mappings have a list of control points. the first two numbers are 5 | # the x,y coords of a pixel on the overlay image. The next two numbers are 6 | # a point on the destination image (expressed percentage, 0-100). You can 7 | # use the various face.{eye_left,eye_right,nose,mouth_left,...} helpers. 8 | 9 | module.exports = 10 | hipster: affine (face) -> [ 11 | [138, 80, face.eye_left] 12 | [314, 80, face.eye_right] 13 | ] 14 | 15 | disguise: affine (face) -> [ 16 | [148, 106, face.eye_left] 17 | [282, 106, face.eye_right] 18 | [222, 185, face.nose] 19 | ] 20 | 21 | phantom: affine (face) -> [ 22 | [211, 217, face.eye_left] 23 | [345, 217, face.eye_right] 24 | [278, 296, face.nose] 25 | ] 26 | 27 | googly: affine (face) -> [ 28 | [107, 165, face.eye_left] 29 | [372, 165, face.eye_right] 30 | ] 31 | 32 | eyepatch: affine (face) -> [ 33 | [135, 145, face.eye_left] 34 | [326, 145, face.eye_right] 35 | ] 36 | 37 | # hipster: srt (face) -> [ 38 | # [234.5, 72] 39 | # face.width * 1.1 / 469 40 | # face.roll 41 | # @middle(face.eye_left, face.eye_right) 42 | # ] 43 | 44 | mustache: srt (face) -> [ 45 | [249, 105] 46 | face.width / 491 47 | face.roll 48 | face.mouth_center 49 | ] 50 | 51 | beard: srt (face) -> [ 52 | [343, 200] 53 | face.width / 650 54 | face.roll 55 | face.mouth_center 56 | ] 57 | 58 | monocle: srt (face) -> [ 59 | [68, 72] 60 | face.width / 256 61 | face.roll 62 | face.eye_right 63 | ] 64 | 65 | sopa: srt (face) -> [ 66 | [64, 19] 67 | face.width / 128 68 | face.roll 69 | face.mouth_center 70 | ] 71 | 72 | # The original mustachio algorithm 73 | mustache2: srt (face) -> [ 74 | [249, 105] 75 | @distance(face.nose, face.mouth_center) / 105 76 | @angle(face.mouth_left, face.mouth_right) 77 | face.mouth_center 78 | ] 79 | 80 | clown: srt (face, photo) -> [ 81 | [108, 111] 82 | face.width * 0.5 / 202 83 | 0 84 | face.nose 85 | ] 86 | 87 | scumbag: affine (face) -> [ 88 | [79, 129, face.eye_left] 89 | [137, 120, face.eye_right] 90 | ] 91 | 92 | jason: affine (face) -> [ 93 | [95, 176, face.eye_left] 94 | [223, 208, face.eye_right] 95 | [131, 316, face.mouth_center] 96 | ] 97 | 98 | batman: affine (face) -> [ 99 | [43, 217, face.eye_left] 100 | [121, 207, face.eye_right] 101 | [93, 299, face.mouth_center] 102 | ] 103 | 104 | disapprove: affine (face) -> [ 105 | [160, 178, face.eye_left] 106 | [535, 178, face.eye_right] 107 | ] 108 | 109 | rohan: affine (face) -> [ 110 | [49, 107, face.eye_left] 111 | [135, 107, face.eye_right] 112 | [88, 202, face.mouth_center] 113 | ] 114 | -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | /* @group Reset */ 2 | 3 | /* Reset.less 4 | * Props to Eric Meyer (meyerweb.com) for his CSS reset file. We're using an adapted version here that cuts out some of the reset HTML elements we will never need here (i.e., dfn, samp, etc). 5 | * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ 6 | 7 | 8 | // ERIC MEYER RESET 9 | // ---------------- 10 | 11 | html, body { margin: 0; padding: 0; } 12 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, cite, code, del, dfn, em, img, q, s, samp, small, strike, strong, sub, sup, tt, var, dd, dl, dt, li, ol, ul, fieldset, form, label, legend, button, table, caption, tbody, tfoot, thead, tr, th, td { margin: 0; padding: 0; border: 0; font-weight: normal; font-style: normal; font-size: 100%; font-family: inherit; } 13 | table { border-collapse: collapse; border-spacing: 0; } 14 | ol, ul { list-style: none; } 15 | q:before, q:after, blockquote:before, blockquote:after { content: ""; } 16 | 17 | 18 | // HTML5 19 | // ----- 20 | 21 | header, section, footer, article, aside { 22 | display: block; 23 | } 24 | /* @end */ 25 | 26 | @font-face { 27 | font-family: 'Open Sans'; 28 | src: url('../fonts/OpenSans-Semibold.ttf'); 29 | } 30 | 31 | html { 32 | background:#2ca8c6 url(../image/noise.png) repeat 0 0; 33 | } 34 | 35 | body { 36 | font-family: 'Open Sans', Arial, sans-serif; 37 | font-size: 15px; 38 | line-height:1.5; 39 | padding:50px 0; 40 | color: rgb(85,85,85); 41 | } 42 | 43 | a { 44 | color: rgb(217,91,67); 45 | text-decoration:none; 46 | font-weight:bold; 47 | } 48 | 49 | a:hover { 50 | color: rgba(217,91,67,0.6); 51 | } 52 | 53 | .wrapper { 54 | width:960px; 55 | margin:0 auto; 56 | } 57 | 58 | aside { 59 | float:right; 60 | width:240px; 61 | } 62 | 63 | aside h1 { 64 | font-size:38px; 65 | margin:20px; 66 | text-shadow:#70AFC4 2px 0 0px; 67 | } 68 | 69 | .about h2 { 70 | font-size:25px; 71 | margin-bottom:20px; 72 | } 73 | 74 | .about .twitter-images { 75 | float:left; 76 | } 77 | 78 | .twitter-images img { 79 | display:block; 80 | width:64px; 81 | height:64px; 82 | float:left; 83 | margin-right:10px; 84 | } 85 | 86 | aside p { 87 | margin:0 20px 20px; 88 | } 89 | 90 | article { 91 | margin-right:260px; 92 | padding-bottom:40px; 93 | } 94 | 95 | .about { 96 | padding:25px; 97 | background-color:rgb(227, 232, 207); 98 | margin-right:260px; 99 | overflow:hidden; 100 | border:1px solid rgb(187, 192, 167); 101 | } 102 | 103 | .preview-wrap { 104 | width:490px; 105 | margin:0 auto; 106 | min-height:400px; 107 | } 108 | 109 | .preview-img { 110 | background-color:#fff; 111 | margin-bottom: 40px; 112 | padding:20px; 113 | box-shadow:rgba(0,0,0,0.5) 0 3px 10px; 114 | } 115 | 116 | .preview-img img { 117 | max-width:100%; 118 | max-height:100%; 119 | display:block; 120 | } -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | faceup.me 5 | 6 | 7 | 8 |
9 |
10 | 17 |
18 | 19 | Fork me on GitHub 20 | 21 |
22 |
23 | 24 |
25 |
26 |
http://faceup.me/img?overlay={hipster,clown,mustache,scumbag}&src=YOUR_IMAGE_URL
27 | 31 |
32 |
33 |

About

34 | 40 |

Faceup.me is open source software, built by a group of crazy guys. To view the source visit the github repo

41 |
42 |
43 |
44 | 45 | 48 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /lib/image.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | // For the mapping.coffee template 4 | require('coffee-script') 5 | 6 | var request = require('request') 7 | , Buffers = require('buffers') 8 | , im = require('imagemagick') 9 | , url = require('url') 10 | , async = require('async') 11 | , mapping = require('../mapping') 12 | , util = require('./util') 13 | , cache = util.cache 14 | 15 | function faceDetect(image, callback) { 16 | cache('face:' + image, function(i, callback) { 17 | var uri = url.parse('http://api.face.com/faces/detect.json', true) 18 | uri.query = { 19 | api_key: process.env.FACE_API_KEY, 20 | api_secret: process.env.FACE_API_SECRET, 21 | urls: image, 22 | } 23 | request({uri: url.format(uri), json: true}, function(err, response, data) { 24 | if (err) return callback(err) 25 | callback(null, data) 26 | }) 27 | }, callback) 28 | } 29 | 30 | function fetchImage(image, callback) { 31 | cache('image:' + image, function(i, callback) { 32 | request({uri: image, onResponse: true}, function(err, res) { 33 | if (err) return callback(err) 34 | var data = Buffers() 35 | res.on('data', function(chunk) { 36 | data.push(chunk) 37 | }) 38 | res.on('end', function() { 39 | callback(null, data.toBuffer()) 40 | }) 41 | }) 42 | }, callback) 43 | } 44 | 45 | function applyOverlay(image, options, callback) { 46 | var args = [ 47 | '-', 48 | '-virtual-pixel', 49 | 'transparent', 50 | ] 51 | 52 | var photo = options.face.photos[0] 53 | var overlays = options.overlay.split(',') 54 | 55 | if (!photo.tags) { 56 | return callback(null, null) 57 | } 58 | 59 | var w = function(x) { return x * photo.width * 0.01 } 60 | var h = function(y) { return y * photo.height * 0.01 } 61 | 62 | photo.tags.forEach(function(face) { 63 | overlays.forEach(function(overlay) { 64 | // TODO move this conversion to a better spot 65 | if (!face.converted) { 66 | face.width = w(face.width) 67 | face.height = h(face.height) 68 | Object.keys(face).forEach(function(k) { 69 | var v = face[k] 70 | if (v && v.x) v.x = w(v.x) 71 | if (v && v.y) v.y = h(v.y) 72 | }) 73 | face.converted = true 74 | } 75 | 76 | try { 77 | var transform = mapping[overlay].call(util, face, photo) 78 | } catch (err) { 79 | // TODO Probably just missing the feature (like face.nose, etc) 80 | // console.log(err.stack) 81 | return 82 | } 83 | 84 | args = args.concat([ 85 | '(', 86 | 'overlays/'+overlay+'.png', 87 | '+distort' 88 | ]) 89 | args = args.concat(transform) 90 | args.push(')') 91 | }) 92 | }) 93 | 94 | args.push('-flatten') 95 | args.push('jpg:-') 96 | 97 | // console.log(args.join(' ')) 98 | 99 | var proc = im.convert(args, function(err, stdout, stdin) { 100 | if (err) return callback(err) 101 | 102 | // THIS IS STUPID, IMAGEMAGICK Y U NO OUTPUT A BUFFER?! 103 | stdout = Buffer(stdout, 'binary') 104 | 105 | callback(err, stdout) 106 | }) 107 | proc.stdin.end(image) 108 | } 109 | 110 | exports.mash = function(data, callback){ 111 | cache(data, function(data, callback) { 112 | console.log('overlay:', data.overlay, 'image:', data.image) 113 | async.parallel( 114 | { 115 | face: async.apply(faceDetect, data.image), 116 | image: async.apply(fetchImage, data.image), 117 | }, 118 | function(err, results) { 119 | if (err) return callback(err) 120 | 121 | if (results.face.photos == null || results.face.photos[0] == null) return callback(null, null) 122 | 123 | var options = { 124 | face: results.face, 125 | overlay: data.overlay 126 | } 127 | applyOverlay(results.image, options, callback) 128 | } 129 | ) 130 | }, callback) 131 | }; 132 | --------------------------------------------------------------------------------