From 109bed1e1aef14656d529530c3884366e26c0526 Mon Sep 17 00:00:00 2001 From: Matthias Wagner Date: Sun, 30 Oct 2011 22:29:22 -0700 Subject: [PATCH 01/18] set to aggressive recognition --- lib/detectors/face_com.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/detectors/face_com.rb b/lib/detectors/face_com.rb index d70ba8a..8281f40 100755 --- a/lib/detectors/face_com.rb +++ b/lib/detectors/face_com.rb @@ -4,7 +4,7 @@ class FaceCrop::Detector::FaceCom < FaceCrop::Detector::Base URL = "http://api.face.com/faces/detect.json" def detect_faces(file) - query = @options.to_query + query = @options.to_query + "&detector=Aggressive" url = "#{URL}?#{query}" response = RestClient.post url, :file => File.new(file) From a74e7a55164010d150c593b042c9d47d949aa66c Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 30 Oct 2011 22:47:43 -0700 Subject: [PATCH 02/18] Update paperclip-facecrop.gemspec --- paperclip-facecrop.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paperclip-facecrop.gemspec b/paperclip-facecrop.gemspec index bef30b0..70d5f66 100755 --- a/paperclip-facecrop.gemspec +++ b/paperclip-facecrop.gemspec @@ -1,4 +1,4 @@ -# encoding: utf-8 +# encoding: utf-8 $:.push File.expand_path("../lib", __FILE__) Gem::Specification.new do |s| From 94ee59724dbfbfd7449b6d983c2cd17b079c13c4 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 30 Oct 2011 22:47:55 -0700 Subject: [PATCH 03/18] Update paperclip-facecrop.gemspec --- paperclip-facecrop.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paperclip-facecrop.gemspec b/paperclip-facecrop.gemspec index 70d5f66..bef30b0 100755 --- a/paperclip-facecrop.gemspec +++ b/paperclip-facecrop.gemspec @@ -1,4 +1,4 @@ -# encoding: utf-8 +# encoding: utf-8 $:.push File.expand_path("../lib", __FILE__) Gem::Specification.new do |s| From 32b20d79e051e3df166f62b238c35a52273e7e39 Mon Sep 17 00:00:00 2001 From: matthias Date: Thu, 13 Sep 2012 17:10:17 -0700 Subject: [PATCH 04/18] added Lambda labs detector --- .idea/.name | 1 + .idea/.rakeTasks | 7 + .idea/encodings.xml | 5 + .idea/misc.xml | 25 + .idea/modules.xml | 9 + .idea/paperclip-facecrop.iml | 21 + .idea/scopes/scope_settings.xml | 5 + .idea/vcs.xml | 7 + .idea/workspace.xml | 449 ++++++++++++++++++ lib/detectors/lambda_labs.rb | 25 + lib/detectors/lambda_libs/FaceRecognition.rb | 61 +++ .../mashape/authentication/authentication.rb | 18 + .../authentication/authentication_utils.rb | 24 + .../authentication/basic_authentication.rb | 14 + .../custom_header_authentication.rb | 12 + .../authentication/header_authentication.rb | 11 + .../authentication/mashape_authentication.rb | 15 + .../authentication/oauth10a_authentication.rb | 9 + .../authentication/oauth2_authentication.rb | 9 + .../authentication/oauth_authentication.rb | 13 + .../authentication/query_authentication.rb | 11 + .../lambda_libs/mashape/http_client.rb | 96 ++++ .../lambda_libs/mashape/http_utils.rb | 47 ++ lib/detectors/lambda_libs/mashape/mashape.rb | 1 + .../lambda_libs/mashape/mashape_exception.rb | 9 + lib/face_crop.rb | 1 + 26 files changed, 905 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/.rakeTasks create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/paperclip-facecrop.iml create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 lib/detectors/lambda_labs.rb create mode 100755 lib/detectors/lambda_libs/FaceRecognition.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb create mode 100755 lib/detectors/lambda_libs/mashape/http_client.rb create mode 100755 lib/detectors/lambda_libs/mashape/http_utils.rb create mode 100755 lib/detectors/lambda_libs/mashape/mashape.rb create mode 100755 lib/detectors/lambda_libs/mashape/mashape_exception.rb diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..81f4ea0 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +paperclip-facecrop \ No newline at end of file diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks new file mode 100644 index 0000000..336edcd --- /dev/null +++ b/.idea/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..f415bd5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,25 @@ + + + + http://www.w3.org/1999/xhtml + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b7cd248 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/paperclip-facecrop.iml b/.idea/paperclip-facecrop.iml new file mode 100644 index 0000000..4a1c20a --- /dev/null +++ b/.idea/paperclip-facecrop.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c80f219 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..70fd102 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,449 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1347506012506 + 1347506012506 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb new file mode 100644 index 0000000..6b119b0 --- /dev/null +++ b/lib/detectors/lambda_labs.rb @@ -0,0 +1,25 @@ +require 'rest_client' + +class FaceCrop::Detector::Lambda_Labs < FaceCrop::Detector::Base + + def detect_faces(file) + + require File.join(File.dirname(__FILE__), "FaceRecognition.rb") + lambda_face = FaceRecognition.new(@options[:api_key], @options[:api_secret]) + + response = lambda_face.detect(File.open(file)) + + photo = response['photos'].first + photo['tags'].map do |tag| + # values are returned as percentual values + x = (photo['width'] * (tag['center']['x'] / 100.0)).to_i + y = (photo['height'] * (tag['center']['y'] / 100.0)).to_i + w = (photo['width'] * (tag['width'] / 100)).to_i + h = (photo['height'] * (tag['height'] / 100)).to_i + + region = FaceCrop::Detector::Region.new(x, y, w, h) + region.color = "green" + region + end + end +end diff --git a/lib/detectors/lambda_libs/FaceRecognition.rb b/lib/detectors/lambda_libs/FaceRecognition.rb new file mode 100755 index 0000000..b4772d8 --- /dev/null +++ b/lib/detectors/lambda_libs/FaceRecognition.rb @@ -0,0 +1,61 @@ +require File.join(File.dirname(__FILE__), "/mashape/mashape") + +class FaceRecognition + PUBLIC_DNS = "lambda-face-recognition.p.mashape.com" + + def initialize(public_key, private_key) + @authentication_handlers = Array.new + @authentication_handlers << Mashape::MashapeAuthentication.new(public_key, private_key) + end + + + def createAlbum(album,&callback) + parameters = { + "album" => album + } + return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/album", parameters, :form, :json, @authentication_handlers, &callback) + end + + def detect(files=nil,urls=nil,&callback) + parameters = { + "files" => files,"urls" => urls + } + return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/detect", parameters, :binary, :json, @authentication_handlers, &callback) + end + + def rebuildAlbum(album,albumkey,&callback) + parameters = { + "album" => album,"albumkey" => albumkey + } + return Mashape::HttpClient.do_request(:get, "https://" + PUBLIC_DNS + "/album_rebuild", parameters, :form, :json, @authentication_handlers, &callback) + end + + def recognize(album,albumkey,files=nil,urls=nil,&callback) + parameters = { + "album" => album,"albumkey" => albumkey,"files" => files,"urls" => urls + } + return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/recognize", parameters, :binary, :json, @authentication_handlers, &callback) + end + + def trainAlbum(album,albumkey,entryid,files=nil,rebuild=nil,urls=nil,&callback) + parameters = { + "album" => album,"albumkey" => albumkey,"entryid" => entryid,"files" => files,"rebuild" => rebuild,"urls" => urls + } + return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/album_train", parameters, :binary, :json, @authentication_handlers, &callback) + end + + def viewAlbum(album,albumkey,&callback) + parameters = { + "album" => album,"albumkey" => albumkey + } + return Mashape::HttpClient.do_request(:get, "https://" + PUBLIC_DNS + "/album", parameters, :form, :json, @authentication_handlers, &callback) + end + + def viewEntry(album,albumkey,entryid,&callback) + parameters = { + "album" => album,"albumkey" => albumkey,"entryid" => entryid + } + return Mashape::HttpClient.do_request(:get, "https://" + PUBLIC_DNS + "/album_train", parameters, :form, :json, @authentication_handlers, &callback) + end + +end diff --git a/lib/detectors/lambda_libs/mashape/authentication/authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/authentication.rb new file mode 100755 index 0000000..7ccb477 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/authentication.rb @@ -0,0 +1,18 @@ +module Mashape + class Authentication + + def initialize() + @header = Hash.new + @params = Hash.new + end + + def handleHeader() + return @header + end + + def handleParams() + return @params + end + + end +end diff --git a/lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb b/lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb new file mode 100755 index 0000000..b213548 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb @@ -0,0 +1,24 @@ +require 'rubygems' +require 'base64' +require 'hmac-sha1' + +module Mashape + class AuthenticationUtils + + def AuthenticationUtils.generateMashapeAuthHeader(public_key, private_key) + unless public_key.empty? || private_key.empty? + hash = HMAC::SHA1.hexdigest(private_key, public_key) + auth = {"X-Mashape-Authorization" => Base64.encode64(public_key + ":" + hash).chomp.gsub(/\n/,'')} + end + return auth + end + + def AuthenticationUtils.generateBasicAuthHeader(username, password) + unless username.empty? || password.empty? + auth = {"Authorization" => "Basic " + Base64.encode64(username + ":" + password).chomp.gsub(/\n/,'')} + end + return auth + end + + end +end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb new file mode 100755 index 0000000..b33babf --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb @@ -0,0 +1,14 @@ +require File.join(File.dirname(__FILE__), "/header_authentication.rb") +require File.join(File.dirname(__FILE__), "/authentication_utils.rb") + +module Mashape + class BasicAuthentication < Mashape::HeaderAuthentication + + def initialize(username, password) + super() + @header = @header.merge(Mashape::AuthenticationUtils.generateBasicAuthHeader(username, password)) + end + + end +end + diff --git a/lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb new file mode 100755 index 0000000..5f8518a --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb @@ -0,0 +1,12 @@ +require File.join(File.dirname(__FILE__), "/header_authentication.rb") + +module Mashape + class CustomHeaderAuthentication < HeaderAuthentication + + def initialize(header_name, header_value) + super() + @header[header_name] = header_value + end + + end +end diff --git a/lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb new file mode 100755 index 0000000..8b6c12c --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb @@ -0,0 +1,11 @@ +require File.join(File.dirname(__FILE__), "/authentication.rb") + +module Mashape + class HeaderAuthentication < Mashape::Authentication + + def initialize() + super() + end + + end +end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb new file mode 100755 index 0000000..46cbc87 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb @@ -0,0 +1,15 @@ +require File.join(File.dirname(__FILE__), "/header_authentication.rb") +require File.join(File.dirname(__FILE__), "/authentication_utils.rb") + +module Mashape + class MashapeAuthentication < HeaderAuthentication + + def initialize(public_key, private_key) + super() + @header = @header.merge(Mashape::AuthenticationUtils.generateMashapeAuthHeader(public_key, private_key)) + end + + end +end + + diff --git a/lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb new file mode 100755 index 0000000..cff11dd --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb @@ -0,0 +1,9 @@ +require File.join(File.dirname(__FILE__), "/oauth_authentication.rb") + +module Mashape + class OAuth10aAuthentication < OAuthAuthentication + def initialize(consumer_key, consumer_secret, redirect_url) + super(consumer_key, consumer_secret, redirect_url) + end + end +end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb new file mode 100755 index 0000000..5928397 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb @@ -0,0 +1,9 @@ +require File.join(File.dirname(__FILE__), "/oauth_authentication.rb") + +module Mashape + class OAuth2Authentication < OAuthAuthentication + def initialize(consumer_key, consumer_secret, redirect_url) + super(consumer_key, consumer_secret, redirect_url) + end + end +end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb new file mode 100755 index 0000000..bfc79f8 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb @@ -0,0 +1,13 @@ +require File.join(File.dirname(__FILE__), "/authentication.rb") + +module Mashape + class OAuthAuthentication < Authentication + + def initialize(consumer_key, consumer_secret, redirect_url) + super() + @params[:consumer_key] = consumer_key + @params[:consumer_secret] = consumer_secret + @params[:redirect_url] = redirect_url + end + end +end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb new file mode 100755 index 0000000..5a7bb52 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb @@ -0,0 +1,11 @@ +require File.join(File.dirname(__FILE__), "/authentication.rb") + +module Mashape + class QueryAuthentication < Authentication + def initialize(query_key, query_value) + super() + @params[query_key] = query_value + end + end +end + diff --git a/lib/detectors/lambda_libs/mashape/http_client.rb b/lib/detectors/lambda_libs/mashape/http_client.rb new file mode 100755 index 0000000..45928f5 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/http_client.rb @@ -0,0 +1,96 @@ +require 'rubygems' +require 'addressable/uri' +require 'rest-client' +require File.join(File.dirname(__FILE__), "/authentication/mashape_authentication.rb") +require File.join(File.dirname(__FILE__), "/authentication/custom_header_authentication.rb") +require File.join(File.dirname(__FILE__), "/authentication/query_authentication.rb") +require File.join(File.dirname(__FILE__), "/authentication/basic_authentication.rb") +require File.join(File.dirname(__FILE__), "/http_utils.rb") +require File.join(File.dirname(__FILE__), "/mashape_exception.rb") + +module Mashape + class HttpResponse + attr :code, true + attr :raw_body, true + attr :body, true + attr :headers, true + end + + class HttpClient + + def HttpClient.do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil, &callback) + if callback + return Thread.new do + callback.call(internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers)) + end + else + return internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers) + end + end + + def HttpClient.internal_do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil) + httpResponse = nil; + + headers = {} + if parameters == nil + case content_type + when :form || :binary + parameters = {} + end + end + + # figure out what kind of auth we have and where to put it + authentication_handlers.each do |handler| + if handler.kind_of? Mashape::HeaderAuthentication + headers = headers.merge(handler.handleHeader) + elsif handler.kind_of? Mashape::QueryAuthentication + parameters = parameters.merge(handler.handleParams) +# elsif handler.kind_of? Mashape::OAuth10aAuthentication +# if handler.handleParams[:access_token] == nil || handler.handleParams[:access_secret] == nil +# raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authenticate_oauth('access_token','access_secret') with not null values") +# end + # These headers will be processed by the proxy to sign the request +# headers["X-Mashape-OAuth-ConsumerKey"] = handler.handleParams[:consumer_key] +# headers["X-Mashape-OAuth-ConsumerSecret"] = handler.handleParams[:consumer_secret] +# headers["X-Mashape-OAuth-AccessToken"] = handler.handleParams[:access_token] +# headers["X-Mashape-OAuth-AccessSecret"] = handler.handleParams[:access_secret] +# elsif handler.kind_of? Mashape::OAuth2Authentication +# if handler.handleParams[:access_token] == nil +# raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authenticate_oauth('access_token') with a not null value") +# end +# parameters = parameters.merge({"access_token" => handler.handleParams[:access_token]}) + end + end + + Mashape::HttpUtils.setRequestHeaders(content_type, response_type, headers) + + begin + case method + when :get + uri = Addressable::URI.new + uri.query_values = parameters + httpResponse = RestClient.get url + "?" + uri.query, headers + when :post + httpResponse = RestClient.post url, parameters, headers + when :put + httpResponse = RestClient.put url, parameters, headers + when :delete + httpResponse = RestClient.delete url, parameters, headers + when :patch + httpResponse = RestClient.patch url, parameters, headers + end + rescue => e + httpResponse = e.response + end + + response = HttpResponse.new + response.code = httpResponse.code + response.headers = httpResponse.headers + response.raw_body = httpResponse + + Mashape::HttpUtils.setResponse(response_type, response) + + return response + end + end +end diff --git a/lib/detectors/lambda_libs/mashape/http_utils.rb b/lib/detectors/lambda_libs/mashape/http_utils.rb new file mode 100755 index 0000000..32644c6 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/http_utils.rb @@ -0,0 +1,47 @@ +require File.join(File.dirname(__FILE__), "/mashape_exception.rb") +require 'rubygems' +require 'json' +require 'uri' + +module Mashape + + class HttpUtils + + def HttpUtils.uriEncode(value) + return URI.escape(value) + end + + def HttpUtils.setRequestHeaders(content_type, response_type, headers) + headers["User-Agent"] = "mashape-ruby/2.0" + + case content_type + when :json + headers["Content-Type"] = "application/json" + when :form + headers["Content-Type"] = "application/x-www-form-urlencoded" + when :binary + headers["Content-Type"] = "multipart/form-data" + end + + case response_type + when :json + headers["Accept"] = "application/json" + end + end + + def HttpUtils.setResponse(response_type, output_response) + case response_type + when :json + begin + output_response.body = JSON.parse(output_response.raw_body) + rescue StandardError + raise Mashape::JsonException.new("Can't parse the following response into JSON: " + output_response.raw_body) + end + else + output_response.body = output_response.raw_body + end + end + + end + +end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/mashape.rb b/lib/detectors/lambda_libs/mashape/mashape.rb new file mode 100755 index 0000000..03ac309 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/mashape.rb @@ -0,0 +1 @@ +Dir[File.dirname(__FILE__) + '/**/*.rb'].each {|file| require file } \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/mashape_exception.rb b/lib/detectors/lambda_libs/mashape/mashape_exception.rb new file mode 100755 index 0000000..2307774 --- /dev/null +++ b/lib/detectors/lambda_libs/mashape/mashape_exception.rb @@ -0,0 +1,9 @@ +module Mashape + class JsonException < StandardError + + def initialize(message) + super(message) + end + + end +end diff --git a/lib/face_crop.rb b/lib/face_crop.rb index dff20d9..4012357 100755 --- a/lib/face_crop.rb +++ b/lib/face_crop.rb @@ -5,6 +5,7 @@ module FaceCrop module Detector autoload :FaceCom, File.expand_path('../detectors/face_com', __FILE__) + autoload :LambdaLabs, File.expand_path('../detectors/lambda_labs', __FILE__) autoload :OpenCV, File.expand_path('../detectors/opencv', __FILE__) # Base From 090f040e134203916f0c004d6ffb870d3a142ac1 Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 17:46:51 -0700 Subject: [PATCH 05/18] works :) --- .idea/workspace.xml | 52 ++++++++++++++++++------------------ lib/detectors/lambda_labs.rb | 2 +- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 70fd102..d924fd2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,29 +2,8 @@ - - - - - - - - - - - - - - - - - - - - - - - + + @@ -46,7 +25,7 @@ - + @@ -307,7 +286,7 @@ - + @@ -437,9 +416,30 @@ + + + + + + + + + + + + + + + + + + + + + - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index 6b119b0..91a7a6f 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -1,6 +1,6 @@ require 'rest_client' -class FaceCrop::Detector::Lambda_Labs < FaceCrop::Detector::Base +class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base def detect_faces(file) From c9b9b1cf1ab03886fae159b26af6709c2680e059 Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 17:47:31 -0700 Subject: [PATCH 06/18] opss worksnow --- .idea/workspace.xml | 7 +++---- lib/detectors/lambda_labs.rb | 18 +++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d924fd2..4c5177a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -25,7 +24,7 @@ - + @@ -286,7 +285,7 @@ - + @@ -439,7 +438,7 @@ - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index 91a7a6f..a2330dd 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -1,24 +1,24 @@ -require 'rest_client' +require File.join(File.dirname(__FILE__), "lambda_libs/FaceRecognition.rb") class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base def detect_faces(file) - - require File.join(File.dirname(__FILE__), "FaceRecognition.rb") lambda_face = FaceRecognition.new(@options[:api_key], @options[:api_secret]) + response = lambda_face.detect(File.open(file)).body - response = lambda_face.detect(File.open(file)) + puts response photo = response['photos'].first photo['tags'].map do |tag| # values are returned as percentual values - x = (photo['width'] * (tag['center']['x'] / 100.0)).to_i - y = (photo['height'] * (tag['center']['y'] / 100.0)).to_i - w = (photo['width'] * (tag['width'] / 100)).to_i - h = (photo['height'] * (tag['height'] / 100)).to_i + + x = tag['center']['x'].to_i + y = tag['center']['y'].to_i + w = tag['width'].to_i + h = tag['height'].to_i region = FaceCrop::Detector::Region.new(x, y, w, h) - region.color = "green" + region.color = "blue" region end end From 1ff7a495c77dbbb3b4231f276c4c006c1ba5cf32 Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 18:03:26 -0700 Subject: [PATCH 07/18] fix --- .idea/workspace.xml | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4c5177a..a2af6e4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -21,10 +21,19 @@ - + - + + + + + + + + + + @@ -44,8 +53,8 @@ @@ -341,7 +350,7 @@ - - - - - - - @@ -438,7 +440,14 @@ - + + + + + + + + From 30cbd38482d1edbc867a04050d78f4c6dea133fc Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 18:05:41 -0700 Subject: [PATCH 08/18] fix --- .idea/workspace.xml | 20 ++++++++++---------- lib/detectors/lambda_labs.rb | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a2af6e4..5bbfd14 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + @@ -21,19 +21,19 @@ - + - + - + - + @@ -53,8 +53,8 @@ @@ -438,16 +438,16 @@ - + - + - + - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index a2330dd..fc8afa2 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), "lambda_libs/FaceRecognition.rb") +require File.expand_path('../lambda_libs/FaceRecognition', __FILE__) class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base From 3b77d9291bf71595a28e0b67faafc7310269e33a Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 18:09:42 -0700 Subject: [PATCH 09/18] fix --- .idea/workspace.xml | 4 ++-- lib/detectors/lambda_labs.rb | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5bbfd14..4e65add 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -24,7 +24,7 @@ - + @@ -447,7 +447,7 @@ - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index fc8afa2..71ad0f5 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -1,8 +1,9 @@ -require File.expand_path('../lambda_libs/FaceRecognition', __FILE__) + class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base def detect_faces(file) + require File.expand_path('../lambda_libs/FaceRecognition', __FILE__) lambda_face = FaceRecognition.new(@options[:api_key], @options[:api_secret]) response = lambda_face.detect(File.open(file)).body From 19b4550035508e89b2367be9f0ecc18d090a0268 Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 18:34:33 -0700 Subject: [PATCH 10/18] update --- .idea/workspace.xml | 5 +++-- lib/detectors/lambda_labs.rb | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4e65add..c042040 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -24,7 +25,7 @@ - + @@ -447,7 +448,7 @@ - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index 71ad0f5..afb30e0 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -1,11 +1,15 @@ - +require 'rest_client' class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base + URL = "https://lambda-face-detection-and-recognition.p.mashape.com/detect" def detect_faces(file) - require File.expand_path('../lambda_libs/FaceRecognition', __FILE__) - lambda_face = FaceRecognition.new(@options[:api_key], @options[:api_secret]) - response = lambda_face.detect(File.open(file)).body + + response = RestClient.post(URL, File.new(file), + {:content_type => 'json', + "X-Mashape-Authorization" => "azJmc3Bmc2l4OWNjcGtnZ3NibGVheDRlbzlrYm92OjAyYWM2NmU1NTBhMWYwYzUyNmY1NzFlYWRhMTc2M2RjODJmN2I1Mzk=" }) + + response = JSON.parse(response) puts response From 5f26eb5bc4da375303a46ac2cedcf4a1fe5ad0a3 Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 19:30:01 -0700 Subject: [PATCH 11/18] update config options --- .idea/workspace.xml | 5 ++--- lib/detectors/lambda_labs.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index c042040..d4f2ddc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -25,7 +24,7 @@ - + @@ -448,7 +447,7 @@ - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index afb30e0..7d0f775 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -7,7 +7,7 @@ def detect_faces(file) response = RestClient.post(URL, File.new(file), {:content_type => 'json', - "X-Mashape-Authorization" => "azJmc3Bmc2l4OWNjcGtnZ3NibGVheDRlbzlrYm92OjAyYWM2NmU1NTBhMWYwYzUyNmY1NzFlYWRhMTc2M2RjODJmN2I1Mzk=" }) + "X-Mashape-Authorization" => @options[:mashape_authorization] }) response = JSON.parse(response) From 16a2669864496c5f59e64b98d2194cb33226aece Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 19:36:59 -0700 Subject: [PATCH 12/18] fix --- .idea/workspace.xml | 4 ++-- lib/detectors/lambda_labs.rb | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d4f2ddc..34a1890 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -24,7 +24,7 @@ - + @@ -447,7 +447,7 @@ - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index 7d0f775..18570b8 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -4,18 +4,22 @@ class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base URL = "https://lambda-face-detection-and-recognition.p.mashape.com/detect" def detect_faces(file) - + puts "boom" + puts @options + puts @options[:mashape_authorization] response = RestClient.post(URL, File.new(file), {:content_type => 'json', "X-Mashape-Authorization" => @options[:mashape_authorization] }) + + response = JSON.parse(response) puts response photo = response['photos'].first photo['tags'].map do |tag| - # values are returned as percentual values + # values are returned as pixel values x = tag['center']['x'].to_i y = tag['center']['y'].to_i From ebd8c5b277af64cad140f86c46259829fb6edd8e Mon Sep 17 00:00:00 2001 From: matthias Date: Fri, 14 Sep 2012 19:39:32 -0700 Subject: [PATCH 13/18] remote old libs --- .idea/workspace.xml | 63 ++++-------- lib/detectors/lambda_libs/FaceRecognition.rb | 61 ------------ .../mashape/authentication/authentication.rb | 18 ---- .../authentication/authentication_utils.rb | 24 ----- .../authentication/basic_authentication.rb | 14 --- .../custom_header_authentication.rb | 12 --- .../authentication/header_authentication.rb | 11 --- .../authentication/mashape_authentication.rb | 15 --- .../authentication/oauth10a_authentication.rb | 9 -- .../authentication/oauth2_authentication.rb | 9 -- .../authentication/oauth_authentication.rb | 13 --- .../authentication/query_authentication.rb | 11 --- .../lambda_libs/mashape/http_client.rb | 96 ------------------- .../lambda_libs/mashape/http_utils.rb | 47 --------- lib/detectors/lambda_libs/mashape/mashape.rb | 1 - .../lambda_libs/mashape/mashape_exception.rb | 9 -- 16 files changed, 17 insertions(+), 396 deletions(-) delete mode 100755 lib/detectors/lambda_libs/FaceRecognition.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb delete mode 100755 lib/detectors/lambda_libs/mashape/http_client.rb delete mode 100755 lib/detectors/lambda_libs/mashape/http_utils.rb delete mode 100755 lib/detectors/lambda_libs/mashape/mashape.rb delete mode 100755 lib/detectors/lambda_libs/mashape/mashape_exception.rb diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 34a1890..4fedc7a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,21 @@ - + + + + + + + + + + + + + + + @@ -140,28 +154,6 @@ - + - + @@ -368,27 +360,6 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/lib/detectors/lambda_libs/FaceRecognition.rb b/lib/detectors/lambda_libs/FaceRecognition.rb deleted file mode 100755 index b4772d8..0000000 --- a/lib/detectors/lambda_libs/FaceRecognition.rb +++ /dev/null @@ -1,61 +0,0 @@ -require File.join(File.dirname(__FILE__), "/mashape/mashape") - -class FaceRecognition - PUBLIC_DNS = "lambda-face-recognition.p.mashape.com" - - def initialize(public_key, private_key) - @authentication_handlers = Array.new - @authentication_handlers << Mashape::MashapeAuthentication.new(public_key, private_key) - end - - - def createAlbum(album,&callback) - parameters = { - "album" => album - } - return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/album", parameters, :form, :json, @authentication_handlers, &callback) - end - - def detect(files=nil,urls=nil,&callback) - parameters = { - "files" => files,"urls" => urls - } - return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/detect", parameters, :binary, :json, @authentication_handlers, &callback) - end - - def rebuildAlbum(album,albumkey,&callback) - parameters = { - "album" => album,"albumkey" => albumkey - } - return Mashape::HttpClient.do_request(:get, "https://" + PUBLIC_DNS + "/album_rebuild", parameters, :form, :json, @authentication_handlers, &callback) - end - - def recognize(album,albumkey,files=nil,urls=nil,&callback) - parameters = { - "album" => album,"albumkey" => albumkey,"files" => files,"urls" => urls - } - return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/recognize", parameters, :binary, :json, @authentication_handlers, &callback) - end - - def trainAlbum(album,albumkey,entryid,files=nil,rebuild=nil,urls=nil,&callback) - parameters = { - "album" => album,"albumkey" => albumkey,"entryid" => entryid,"files" => files,"rebuild" => rebuild,"urls" => urls - } - return Mashape::HttpClient.do_request(:post, "https://" + PUBLIC_DNS + "/album_train", parameters, :binary, :json, @authentication_handlers, &callback) - end - - def viewAlbum(album,albumkey,&callback) - parameters = { - "album" => album,"albumkey" => albumkey - } - return Mashape::HttpClient.do_request(:get, "https://" + PUBLIC_DNS + "/album", parameters, :form, :json, @authentication_handlers, &callback) - end - - def viewEntry(album,albumkey,entryid,&callback) - parameters = { - "album" => album,"albumkey" => albumkey,"entryid" => entryid - } - return Mashape::HttpClient.do_request(:get, "https://" + PUBLIC_DNS + "/album_train", parameters, :form, :json, @authentication_handlers, &callback) - end - -end diff --git a/lib/detectors/lambda_libs/mashape/authentication/authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/authentication.rb deleted file mode 100755 index 7ccb477..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/authentication.rb +++ /dev/null @@ -1,18 +0,0 @@ -module Mashape - class Authentication - - def initialize() - @header = Hash.new - @params = Hash.new - end - - def handleHeader() - return @header - end - - def handleParams() - return @params - end - - end -end diff --git a/lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb b/lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb deleted file mode 100755 index b213548..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/authentication_utils.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'rubygems' -require 'base64' -require 'hmac-sha1' - -module Mashape - class AuthenticationUtils - - def AuthenticationUtils.generateMashapeAuthHeader(public_key, private_key) - unless public_key.empty? || private_key.empty? - hash = HMAC::SHA1.hexdigest(private_key, public_key) - auth = {"X-Mashape-Authorization" => Base64.encode64(public_key + ":" + hash).chomp.gsub(/\n/,'')} - end - return auth - end - - def AuthenticationUtils.generateBasicAuthHeader(username, password) - unless username.empty? || password.empty? - auth = {"Authorization" => "Basic " + Base64.encode64(username + ":" + password).chomp.gsub(/\n/,'')} - end - return auth - end - - end -end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb deleted file mode 100755 index b33babf..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/basic_authentication.rb +++ /dev/null @@ -1,14 +0,0 @@ -require File.join(File.dirname(__FILE__), "/header_authentication.rb") -require File.join(File.dirname(__FILE__), "/authentication_utils.rb") - -module Mashape - class BasicAuthentication < Mashape::HeaderAuthentication - - def initialize(username, password) - super() - @header = @header.merge(Mashape::AuthenticationUtils.generateBasicAuthHeader(username, password)) - end - - end -end - diff --git a/lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb deleted file mode 100755 index 5f8518a..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/custom_header_authentication.rb +++ /dev/null @@ -1,12 +0,0 @@ -require File.join(File.dirname(__FILE__), "/header_authentication.rb") - -module Mashape - class CustomHeaderAuthentication < HeaderAuthentication - - def initialize(header_name, header_value) - super() - @header[header_name] = header_value - end - - end -end diff --git a/lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb deleted file mode 100755 index 8b6c12c..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/header_authentication.rb +++ /dev/null @@ -1,11 +0,0 @@ -require File.join(File.dirname(__FILE__), "/authentication.rb") - -module Mashape - class HeaderAuthentication < Mashape::Authentication - - def initialize() - super() - end - - end -end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb deleted file mode 100755 index 46cbc87..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/mashape_authentication.rb +++ /dev/null @@ -1,15 +0,0 @@ -require File.join(File.dirname(__FILE__), "/header_authentication.rb") -require File.join(File.dirname(__FILE__), "/authentication_utils.rb") - -module Mashape - class MashapeAuthentication < HeaderAuthentication - - def initialize(public_key, private_key) - super() - @header = @header.merge(Mashape::AuthenticationUtils.generateMashapeAuthHeader(public_key, private_key)) - end - - end -end - - diff --git a/lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb deleted file mode 100755 index cff11dd..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/oauth10a_authentication.rb +++ /dev/null @@ -1,9 +0,0 @@ -require File.join(File.dirname(__FILE__), "/oauth_authentication.rb") - -module Mashape - class OAuth10aAuthentication < OAuthAuthentication - def initialize(consumer_key, consumer_secret, redirect_url) - super(consumer_key, consumer_secret, redirect_url) - end - end -end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb deleted file mode 100755 index 5928397..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/oauth2_authentication.rb +++ /dev/null @@ -1,9 +0,0 @@ -require File.join(File.dirname(__FILE__), "/oauth_authentication.rb") - -module Mashape - class OAuth2Authentication < OAuthAuthentication - def initialize(consumer_key, consumer_secret, redirect_url) - super(consumer_key, consumer_secret, redirect_url) - end - end -end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb deleted file mode 100755 index bfc79f8..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/oauth_authentication.rb +++ /dev/null @@ -1,13 +0,0 @@ -require File.join(File.dirname(__FILE__), "/authentication.rb") - -module Mashape - class OAuthAuthentication < Authentication - - def initialize(consumer_key, consumer_secret, redirect_url) - super() - @params[:consumer_key] = consumer_key - @params[:consumer_secret] = consumer_secret - @params[:redirect_url] = redirect_url - end - end -end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb b/lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb deleted file mode 100755 index 5a7bb52..0000000 --- a/lib/detectors/lambda_libs/mashape/authentication/query_authentication.rb +++ /dev/null @@ -1,11 +0,0 @@ -require File.join(File.dirname(__FILE__), "/authentication.rb") - -module Mashape - class QueryAuthentication < Authentication - def initialize(query_key, query_value) - super() - @params[query_key] = query_value - end - end -end - diff --git a/lib/detectors/lambda_libs/mashape/http_client.rb b/lib/detectors/lambda_libs/mashape/http_client.rb deleted file mode 100755 index 45928f5..0000000 --- a/lib/detectors/lambda_libs/mashape/http_client.rb +++ /dev/null @@ -1,96 +0,0 @@ -require 'rubygems' -require 'addressable/uri' -require 'rest-client' -require File.join(File.dirname(__FILE__), "/authentication/mashape_authentication.rb") -require File.join(File.dirname(__FILE__), "/authentication/custom_header_authentication.rb") -require File.join(File.dirname(__FILE__), "/authentication/query_authentication.rb") -require File.join(File.dirname(__FILE__), "/authentication/basic_authentication.rb") -require File.join(File.dirname(__FILE__), "/http_utils.rb") -require File.join(File.dirname(__FILE__), "/mashape_exception.rb") - -module Mashape - class HttpResponse - attr :code, true - attr :raw_body, true - attr :body, true - attr :headers, true - end - - class HttpClient - - def HttpClient.do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil, &callback) - if callback - return Thread.new do - callback.call(internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers)) - end - else - return internal_do_request(method, url, parameters, content_type, response_type, authentication_handlers) - end - end - - def HttpClient.internal_do_request(method, url, parameters = nil, content_type = nil, response_type = nil, authentication_handlers = nil) - httpResponse = nil; - - headers = {} - if parameters == nil - case content_type - when :form || :binary - parameters = {} - end - end - - # figure out what kind of auth we have and where to put it - authentication_handlers.each do |handler| - if handler.kind_of? Mashape::HeaderAuthentication - headers = headers.merge(handler.handleHeader) - elsif handler.kind_of? Mashape::QueryAuthentication - parameters = parameters.merge(handler.handleParams) -# elsif handler.kind_of? Mashape::OAuth10aAuthentication -# if handler.handleParams[:access_token] == nil || handler.handleParams[:access_secret] == nil -# raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authenticate_oauth('access_token','access_secret') with not null values") -# end - # These headers will be processed by the proxy to sign the request -# headers["X-Mashape-OAuth-ConsumerKey"] = handler.handleParams[:consumer_key] -# headers["X-Mashape-OAuth-ConsumerSecret"] = handler.handleParams[:consumer_secret] -# headers["X-Mashape-OAuth-AccessToken"] = handler.handleParams[:access_token] -# headers["X-Mashape-OAuth-AccessSecret"] = handler.handleParams[:access_secret] -# elsif handler.kind_of? Mashape::OAuth2Authentication -# if handler.handleParams[:access_token] == nil -# raise Mashape::JsonException.new("Before consuming OAuth endpoint, invoke authenticate_oauth('access_token') with a not null value") -# end -# parameters = parameters.merge({"access_token" => handler.handleParams[:access_token]}) - end - end - - Mashape::HttpUtils.setRequestHeaders(content_type, response_type, headers) - - begin - case method - when :get - uri = Addressable::URI.new - uri.query_values = parameters - httpResponse = RestClient.get url + "?" + uri.query, headers - when :post - httpResponse = RestClient.post url, parameters, headers - when :put - httpResponse = RestClient.put url, parameters, headers - when :delete - httpResponse = RestClient.delete url, parameters, headers - when :patch - httpResponse = RestClient.patch url, parameters, headers - end - rescue => e - httpResponse = e.response - end - - response = HttpResponse.new - response.code = httpResponse.code - response.headers = httpResponse.headers - response.raw_body = httpResponse - - Mashape::HttpUtils.setResponse(response_type, response) - - return response - end - end -end diff --git a/lib/detectors/lambda_libs/mashape/http_utils.rb b/lib/detectors/lambda_libs/mashape/http_utils.rb deleted file mode 100755 index 32644c6..0000000 --- a/lib/detectors/lambda_libs/mashape/http_utils.rb +++ /dev/null @@ -1,47 +0,0 @@ -require File.join(File.dirname(__FILE__), "/mashape_exception.rb") -require 'rubygems' -require 'json' -require 'uri' - -module Mashape - - class HttpUtils - - def HttpUtils.uriEncode(value) - return URI.escape(value) - end - - def HttpUtils.setRequestHeaders(content_type, response_type, headers) - headers["User-Agent"] = "mashape-ruby/2.0" - - case content_type - when :json - headers["Content-Type"] = "application/json" - when :form - headers["Content-Type"] = "application/x-www-form-urlencoded" - when :binary - headers["Content-Type"] = "multipart/form-data" - end - - case response_type - when :json - headers["Accept"] = "application/json" - end - end - - def HttpUtils.setResponse(response_type, output_response) - case response_type - when :json - begin - output_response.body = JSON.parse(output_response.raw_body) - rescue StandardError - raise Mashape::JsonException.new("Can't parse the following response into JSON: " + output_response.raw_body) - end - else - output_response.body = output_response.raw_body - end - end - - end - -end \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/mashape.rb b/lib/detectors/lambda_libs/mashape/mashape.rb deleted file mode 100755 index 03ac309..0000000 --- a/lib/detectors/lambda_libs/mashape/mashape.rb +++ /dev/null @@ -1 +0,0 @@ -Dir[File.dirname(__FILE__) + '/**/*.rb'].each {|file| require file } \ No newline at end of file diff --git a/lib/detectors/lambda_libs/mashape/mashape_exception.rb b/lib/detectors/lambda_libs/mashape/mashape_exception.rb deleted file mode 100755 index 2307774..0000000 --- a/lib/detectors/lambda_libs/mashape/mashape_exception.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Mashape - class JsonException < StandardError - - def initialize(message) - super(message) - end - - end -end From d64adbbab050338650fe3b301fd4baef8f1c13bd Mon Sep 17 00:00:00 2001 From: matthias Date: Sat, 15 Sep 2012 17:03:30 -0700 Subject: [PATCH 14/18] maybe it works now :/ --- .idea/paperclip-facecrop.iml | 2 +- .idea/workspace.xml | 39 +++++++++--------------------------- lib/detectors/lambda_labs.rb | 6 +----- 3 files changed, 11 insertions(+), 36 deletions(-) diff --git a/.idea/paperclip-facecrop.iml b/.idea/paperclip-facecrop.iml index 4a1c20a..82133fd 100644 --- a/.idea/paperclip-facecrop.iml +++ b/.idea/paperclip-facecrop.iml @@ -14,7 +14,7 @@ - + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4fedc7a..0748beb 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,21 +2,9 @@ - - - - - - - - - - - - - - - + + + @@ -38,16 +26,7 @@ - - - - - - - - - - + @@ -286,13 +265,14 @@ - + + - + @@ -300,7 +280,6 @@ - @@ -411,14 +390,14 @@ - + - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index 18570b8..4b2cbee 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -4,15 +4,11 @@ class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base URL = "https://lambda-face-detection-and-recognition.p.mashape.com/detect" def detect_faces(file) - puts "boom" - puts @options - puts @options[:mashape_authorization] + response = RestClient.post(URL, File.new(file), {:content_type => 'json', "X-Mashape-Authorization" => @options[:mashape_authorization] }) - - response = JSON.parse(response) puts response From f375286bb025f4cf3aff78bc37a0df07574caa30 Mon Sep 17 00:00:00 2001 From: matthias Date: Sat, 15 Sep 2012 17:45:21 -0700 Subject: [PATCH 15/18] works --- .idea/workspace.xml | 5 ++--- lib/detectors/lambda_labs.rb | 13 ++++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0748beb..f667eda 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -26,7 +25,7 @@ - + @@ -397,7 +396,7 @@ - + diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index 4b2cbee..fb0bc26 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -1,13 +1,12 @@ require 'rest_client' class FaceCrop::Detector::LambdaLabs < FaceCrop::Detector::Base - URL = "https://lambda-face-detection-and-recognition.p.mashape.com/detect" + URL = "https://lambda-face-recognition.p.mashape.com/detect" def detect_faces(file) - - response = RestClient.post(URL, File.new(file), - {:content_type => 'json', - "X-Mashape-Authorization" => @options[:mashape_authorization] }) + response = RestClient.post(URL, + {:files => File.new(file)}, + {:content_type => 'json', "X-Mashape-Authorization" => @options[:mashape_authorization]}) response = JSON.parse(response) @@ -15,7 +14,7 @@ def detect_faces(file) photo = response['photos'].first photo['tags'].map do |tag| - # values are returned as pixel values + # values are returned as percentual values x = tag['center']['x'].to_i y = tag['center']['y'].to_i @@ -27,4 +26,4 @@ def detect_faces(file) region end end -end +end \ No newline at end of file From 45c2952e820cf0d998817f518837a694d5af5dc5 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 15 Sep 2012 17:58:40 -0700 Subject: [PATCH 16/18] Update lib/detectors/lambda_labs.rb removed logging --- lib/detectors/lambda_labs.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/detectors/lambda_labs.rb b/lib/detectors/lambda_labs.rb index fb0bc26..e12a8b1 100644 --- a/lib/detectors/lambda_labs.rb +++ b/lib/detectors/lambda_labs.rb @@ -10,8 +10,6 @@ def detect_faces(file) response = JSON.parse(response) - puts response - photo = response['photos'].first photo['tags'].map do |tag| # values are returned as percentual values From 5bac9d38195cb3d3c2ed9ad728edfc6df92c02a7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 16 Sep 2012 16:45:06 -0700 Subject: [PATCH 17/18] Update README.md --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fb2bbc4..576e1d2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Paperclip::FaceCrop ==================== `Paperclip::FaceCrop` is a [Paperclip][paperclip] processor that is aware of the faces found on the image so that they aren't cropped or aren't shown too small while generating the thumbnails. -It can use the [OpenCV][opencv] library or the [Face.com][face_com] web service(or both at the same time) for the facial recognition. +It can use the [OpenCV][opencv] library, the [Face.com][face_com] or [LambdaLabs][lambda_labs] web service(or all three at the same time) for the facial recognition. ![](https://github.com/dagi3d/paperclip-facecrop/raw/master/README_example.jpg) @@ -25,6 +25,12 @@ checkout the OpenCV_2.2 branch or just remove the line 143 from `/ext/opencv/cve In order to use the Face.com service, you will also need to register in order to get your api key and api secret for your application. +### lambdal.com +- [rest-client][rest-client] + +In order to use the lambdal.com service, you will also need to register in order to get your api key for your application. + + Installation: ------------- @@ -85,6 +91,13 @@ only the found areas that contain parts like a mouth, an eye or a nose will be c 'FaceCom' => { :api_key => "", :api_secret => ""} } +### lambdal.com + + Paperclip::FaceCrop.detectors = { + 'LambdaLabs' => {:mashape_authorization => ""} + } + + Usage: ------ @@ -114,6 +127,7 @@ Copyright (c) 2011 Borja Martín Sánchez de Vivar The photo used as example belongs to [Jesper Rønn-Jensen](http://www.flickr.com/photos/jesper/) [face_com]: http://face.com +[lambda_labs]: http://lambdal.com [rest-client]: https://rubygems.org/gems/rest-client [paperclip]: https://github.com/thoughtbot/paperclip [opencv]: http://opencv.willowgarage.com/ From 04e1eb900ae772b1f875de4a472658a92713b801 Mon Sep 17 00:00:00 2001 From: matthias Date: Tue, 2 Oct 2012 17:10:53 -0700 Subject: [PATCH 18/18] fix positioning for extreme aspect ratios --- .idea/workspace.xml | 71 ++++++++++++++---------------- lib/processor.rb | 104 ++++++++++++++++++++++++-------------------- 2 files changed, 90 insertions(+), 85 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f667eda..31b7098 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,7 @@ - + @@ -22,10 +22,10 @@ - - + + - + @@ -47,6 +47,7 @@ @@ -81,7 +82,6 @@ - @@ -134,14 +134,15 @@ + - + @@ -257,6 +258,7 @@ + 1347506012506 1347506012506 @@ -266,23 +268,23 @@ - + - - + - + - - + + - + + - + @@ -338,60 +340,46 @@ - + - + - + - - - + - - - + - - - + - - - + - - - + - - - + - - - + @@ -401,6 +389,13 @@ + + + + + + + diff --git a/lib/processor.rb b/lib/processor.rb index 5816c40..16d56eb 100755 --- a/lib/processor.rb +++ b/lib/processor.rb @@ -4,10 +4,10 @@ module Paperclip class FaceCrop < Paperclip::Thumbnail @@debug = false - + #cattr_accessor :classifiers cattr_accessor :debug - + def self.detectors=(detectors) @@detectors = detectors.map do |name, options| #require File.expand_path("../detectors/#{name}", __FILE__) @@ -15,16 +15,16 @@ def self.detectors=(detectors) detector = detector_class.new(options) end end - + def initialize(file, options = {}, attachment = nil) super(file, options, attachment) - - + + raise "No detectors were defined" if @@detectors.nil? - + faces_regions = [] faces_parts_regions = [] - + @@detectors.each do |detector| begin faces_regions += detector.detect(file.path) @@ -33,82 +33,92 @@ def initialize(file, options = {}, attachment = nil) Rails.logger.error(e) end end - - + + x_coords, y_coords, widths, heights = [], [], [], [] - + faces_regions.each do |region| x_coords << region.top_left.x << region.bottom_right.x y_coords << region.top_left.y << region.bottom_right.y widths << region.width heights << region.height end - - + + @has_faces = faces_regions.size > 0 - + if @has_faces @top_left_x = x_coords.min @top_left_y = y_coords.min @bottom_right_x = x_coords.max @bottom_right_y = y_coords.max - - - + + + #puts @top_left_x.to_s - + # average faces areas average_face_width = widths.sum / widths.size average_face_height = heights.sum / heights.size - + # calculating the surrounding margin of the area that covers all the found faces # - + # new width @top_left_x -= average_face_width / 1.2 @bottom_right_x += average_face_width / 1.2 - - - + + # new height #puts ":::#{@top_left_x}---#{average_face_width}" #return - + @top_left_y -= average_face_height / 1.2 - @bottom_right_y += average_face_height / 1.6 + @bottom_right_y += average_face_height / 1.2 + calculate_bounds - - - + + + # if the new area is smaller than the target geometry, it's scaled so the final image isn't resampled # - if @faces_width < @target_geometry.width + if @faces_width < @target_geometry.width delta_width = (@target_geometry.width - @faces_width) / 2 @top_left_x -= delta_width @bottom_right_x += delta_width calculate_bounds end - + # scale the image so the cropped image still displays the faces if @faces_width > @target_geometry.width && crop? ratio = @faces_width / @target_geometry.width @faces_height = @target_geometry.height * ratio end - + #raise (@target_geometry.height > 0 and @faces_height < @target_geometry.height).to_s - + if (@target_geometry.height > 0 and @faces_height < @target_geometry.height) delta_height = (@target_geometry.height - @faces_height) / 2 @top_left_y -= delta_height @bottom_right_y += delta_height calculate_bounds end - + + + #fix image position for extrem aspect ratios + if @target_geometry.width / @target_geometry.height < 0.6 + @top_left_x = x_coords.min * 1.2 + end + + if @target_geometry.width / @target_geometry.height > 1.4 + @top_left_y = y_coords.min * 1.2 + end + @faces_height = @faces_width if @target_geometry.height == 0 - + @current_geometry = Paperclip::Geometry.new(@faces_width, @faces_height) - + if @@debug parameters = [] parameters << "-stroke" << "green" @@ -117,43 +127,43 @@ def initialize(file, options = {}, attachment = nil) parameters << ":source" parameters << ":dest" parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ") - + Paperclip.run("convert", parameters, :source => "#{File.expand_path(file.path)}", :dest => "#{File.expand_path(file.path)}") end - - + + end end - - + + def transformation_command return super unless @has_faces - + scale, crop = @current_geometry.transformation_to(@target_geometry, crop?) faces_crop = "%dx%d+%d+%d" % [@faces_width, @faces_height, @top_left_x, @top_left_y] - + trans = [] trans << "-crop" << %["#{faces_crop}"] << "+repage" trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty? trans << "-crop" << %["#{crop}"] << "+repage" if crop - + trans end - + private - + # calculate_bounds # def calculate_bounds - @top_left_x = 0 if @top_left_x < 0 + @top_left_x = 0 if @top_left_x < 0 @bottom_right_x = @current_geometry.width if @bottom_right_x > @current_geometry.width @top_left_y = 0 if @top_left_y < 0 @bottom_right_y = @current_geometry.height if @bottom_right_y > @current_geometry.height - + @faces_width = @bottom_right_x - @top_left_x @faces_height = @bottom_right_y - @top_left_y end - + end end \ No newline at end of file