Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add helpers for SRT and make things a lot easier

  • Loading branch information...
commit 1b32fd44b10308810ced0a58f993906b8076dad8 1 parent 831e439
@wadey wadey authored
Showing with 89 additions and 27 deletions.
  1. +17 −7 lib/image.js
  2. +29 −0 lib/util.js
  3. +33 −16 mapping.coffee
  4. +10 −4 transforms.js
View
24 lib/image.js
@@ -9,7 +9,8 @@ var request = require('request')
, url = require('url')
, async = require('async')
, mapping = require('../mapping')
- , cache = require('./util').cache
+ , util = require('./util')
+ , cache = util.cache
function faceDetect(image, callback) {
cache('face:' + image, function(i, callback) {
@@ -54,15 +55,24 @@ function applyOverlay(image, options, callback) {
return callback(null, null)
}
+ var w = function(x) { return x * photo.width * 0.01 }
+ var h = function(y) { return y * photo.height * 0.01 }
+
photo.tags.forEach(function(face) {
- Object.keys(face).forEach(function(k) {
- var v = face[k]
- if (v && v.x) v.rx = v.x * photo.width * 0.01
- if (v && v.y) v.ry = v.y * photo.height * 0.01
- })
+ // TODO move this conversion to a better spot
+ if (!face.converted) {
+ face.width = w(face.width)
+ face.height = h(face.height)
+ Object.keys(face).forEach(function(k) {
+ var v = face[k]
+ if (v && v.x) v.x = w(v.x)
+ if (v && v.y) v.y = h(v.y)
+ })
+ face.converted = true
+ }
try {
- var transform = mapping[options.overlay](face, photo)
+ var transform = mapping[options.overlay].call(util, face, photo)
} catch (err) {
// TODO Probably just missing the feature (like face.nose, etc)
// console.log(err.stack)
View
29 lib/util.js
@@ -5,6 +5,35 @@ var LRU = require("lru-cache")
var loading = {}
+var x = exports.x = function(e) {
+ return e.x || e[0]
+}
+
+var y = exports.y = function(e) {
+ return e.y || e[1]
+}
+
+exports.distance = function(a, b) {
+ return Math.sqrt(
+ Math.pow(Math.abs(x(a) - x(b)), 2) +
+ Math.pow(Math.abs(y(a) - y(b)), 2)
+ )
+}
+
+exports.angle = function(a, b) {
+ return Math.atan(
+ (y(b) - y(a)) /
+ (x(b) - x(a))
+ ) / Math.PI * 180
+}
+
+exports.middle = function(a, b) {
+ return [
+ (x(b) - x(a)) * 0.5 + x(a),
+ (y(b) - y(a)) * 0.5 + y(a)
+ ]
+}
+
exports.cache = function(data, loader, callback) {
var key = JSON.stringify(data)
View
49 mapping.coffee
@@ -8,37 +8,54 @@
module.exports =
hipster: affine (face) -> [
- [138, 80, face.eye_left.x, face.eye_left.y]
- [314, 80, face.eye_right.x, face.eye_right.y]
+ [138, 80, face.eye_left]
+ [314, 80, face.eye_right]
]
+ # hipster: srt (face) -> [
+ # [234.5, 72]
+ # face.width * 1.1 / 469
+ # face.roll
+ # @middle(face.eye_left, face.eye_right)
+ # ]
+
mustache: srt (face) -> [
[249, 105]
- Math.sqrt(Math.pow(face.nose.rx - face.mouth_center.rx, 2) + Math.pow(face.nose.ry - face.mouth_center.ry, 2)) / 105
- Math.atan((face.mouth_right.ry - face.mouth_left.ry) / (face.mouth_right.rx - face.mouth_left.rx)) / Math.PI * 180
- [face.mouth_center.x, face.mouth_center.y]
+ face.width / 491 # @distance(face.nose, face.mouth_center) / 105
+ face.roll # @angle(face.mouth_left, face.mouth_right)
+ face.mouth_center
+ ]
+
+ # The original mustachio algorithm
+ mustache2: srt (face) -> [
+ [249, 105]
+ @distance(face.nose, face.mouth_center) / 105
+ @angle(face.mouth_left, face.mouth_right)
+ face.mouth_center
]
- clown: affine (face) -> [
- [108, 111, face.nose.x, face.nose.y]
- [108, 202, face.mouth_center.x, face.mouth_center.y]
+ clown: srt (face, photo) -> [
+ [108, 111]
+ face.width * 0.5 / 202
+ 0
+ face.nose
]
scumbag: affine (face) -> [
- [79, 129, face.eye_left.x, face.eye_left.y]
- [137, 120, face.eye_right.x, face.eye_right.y]
+ [79, 129, face.eye_left]
+ [137, 120, face.eye_right]
]
jason: affine (face) -> [
- [95, 176, face.eye_left.x, face.eye_left.y]
- [223, 208, face.eye_right.x, face.eye_right.y]
- [131, 316, face.mouth_center.x, face.mouth_center.y]
+ [95, 176, face.eye_left]
+ [223, 208, face.eye_right]
+ [131, 316, face.mouth_center]
]
rohan: affine (face) -> [
- [49, 107, face.eye_left.x, face.eye_left.y]
- [135, 107, face.eye_right.x, face.eye_right.y]
- [88, 202, face.mouth_center.x, face.mouth_center.y]
+ [49, 107, face.eye_left]
+ [135, 107, face.eye_right]
+ [88, 202, face.mouth_center]
]
View
14 transforms.js
@@ -1,9 +1,13 @@
+var util = require('./lib/util')
+ , x = util.x
+ , y = util.y
+
exports.srt = function(func) {
return function(face, photo) {
- var coords = func(face, photo)
+ var coords = func.call(this, face, photo)
return ['SRT', coords.map(function(part, i) {
if (i == 3) {
- part = [part[0] * photo.width * 0.01, part[1] * photo.height * 0.01]
+ part = [x(part), y(part)]
}
return (Array.isArray(part) ? part.join(',') : part)
}).join(' ')]
@@ -11,10 +15,12 @@ exports.srt = function(func) {
}
exports.affine = function(func) {
return function(face, photo) {
- var result = func(face, photo)
+ var result = func.call(this, face, photo)
return ['Affine', result.map(function(part, i) {
// Convert the destination coords from percentages to pixels
- part = [part[0], part[1], part[2] * photo.width * 0.01, part[3] * photo.height * 0.01]
+ if (part.length == 3) {
+ part = [part[0], part[1], x(part[2]), y(part[2])]
+ }
return part[0]+','+part[1]+' '+part[2]+','+part[3]
}).join(' ')]
Please sign in to comment.
Something went wrong with that request. Please try again.