betaface 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ccac298f929a2636847ae39b9984c108836767fe
4
+ data.tar.gz: 35049c0416dbaba090849e2daf1dc844b2373604
5
+ SHA512:
6
+ metadata.gz: c831f5d042539c1560a9f4df85affd140c9b7d1aef5743c687cbf9cd360e5debdca776002adff78d68b735f9c71277956528e3bbe85a436d05f49240e40e088d
7
+ data.tar.gz: 8e8191e0f1eb4892639fbdb0e4dcd7d1d2dcb30c95c038c7b09de9cccbde91bf50bec5cab36348eaf9326c7731f1eb626eb3e83263326efb21a6a8ef7657e0b8
@@ -0,0 +1,69 @@
1
+ # betaface
2
+ Betaface is the unofficial ruby Gem for the Betaface API. You can find all needed documentation about the official API at [Betaface docs](https://betafaceapi.com/wpa/index.php/documentation)
3
+
4
+ ## Installation
5
+ To install using [Bundler][bundler] grab the latest stable version:
6
+
7
+ ```ruby
8
+ gem 'betaface'
9
+ ```
10
+
11
+ To manually install `betaface` via [Rubygems][rubygems] simply gem install:
12
+
13
+ ```bash
14
+ gem install betaface
15
+ ```
16
+
17
+
18
+ ## Configuration
19
+
20
+ ### Put your own credentials here
21
+ ``` ruby
22
+ require 'betaface'
23
+ Betaface.key = "d45fd466-51e2-4701-8da8-04351c872236"
24
+ Betaface.secret = "171e8465-f548-401d-b63b-caf0dc28df5f"
25
+ ```
26
+
27
+ ### alternatively, you can preconfigure the client like so
28
+ ``` ruby
29
+ require 'betaface'
30
+ api = Betaface::Api.new("d45fd466-51e2-4701-8da8-04351c872236","171e8465-f548-401d-b63b-caf0dc28df5f")
31
+ ```
32
+
33
+ ## Upload a picture
34
+
35
+ ```ruby
36
+ api = Betaface::Api.new
37
+ api.upload_image("propoints,classifiers",{url:"http://img.wennermedia.com/480-width/1444256733_tom-cruise-zoom.jpg"})
38
+ ```
39
+
40
+ ## Get image info
41
+
42
+ ```ruby
43
+ api = Betaface::Api.new
44
+ api.get_image_info("2bdcd1ad-47a6-45f8-ba74-86c765272422")
45
+ ```
46
+
47
+ ## Supported Ruby Versions
48
+
49
+ This library supports and is [tested against][travis] the following Ruby
50
+ implementations:
51
+
52
+ - Ruby 2.2.0
53
+ - Ruby 2.1.0
54
+ - Ruby 2.0.0
55
+ - Ruby 1.9.3
56
+ - [JRuby][jruby]
57
+ - [Rubinius][rubinius]
58
+
59
+ ## More Information
60
+
61
+ There are more detailed examples in the included [examples][examples]
62
+
63
+ [bundler]: http://bundler.io
64
+ [rubygems]: http://rubygems.org
65
+ [examples]: https://github.com/jrm2194/betaface/tree/master/examples
66
+
67
+ ## License
68
+
69
+ (The MIT License.)
@@ -0,0 +1,20 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'betaface/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'betaface'
7
+ s.version = Betaface::VERSION
8
+ s.date = '2016-06-02'
9
+ s.files = `git ls-files -z`.split("\x0")
10
+ s.require_paths = ['lib']
11
+ s.summary = "Use the betaface API with Ruby!"
12
+ s.description = "A ruby Gem to easily use the betaface API without having to deal with all this XLM"
13
+ s.authors = ["Jean Meyer"]
14
+ s.email = 'jean@getonce.com'
15
+ s.homepage = 'http://rubygems.org/gems/betaface'
16
+ s.license = 'MIT'
17
+ s.required_ruby_version = '>= 1.9.3'
18
+ s.add_dependency('multi_json', '>= 1.3.0')
19
+ s.add_dependency('rubysl') if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
20
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/ruby
2
+ require 'betaface'
3
+ Betaface.key = "d45fd466-51e2-4701-8da8-04351c872236"
4
+ Betaface.secret = "171e8465-f548-401d-b63b-caf0dc28df5f"
5
+ api = Betaface::Api.new
6
+ p api.get_image_info("2bdcd1ad-47a6-45f8-ba74-86c765272422")
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/ruby
2
+ require 'active_support/all'
3
+ require 'betaface'
4
+ Betaface.key = "d45fd466-51e2-4701-8da8-04351c872236"
5
+ Betaface.secret = "171e8465-f548-401d-b63b-caf0dc28df5f"
6
+ api = Betaface::Api.new
7
+ p api.upload_image("propoints,classifiers",{url:"http://img.wennermedia.com/480-width/1444256733_tom-cruise-zoom.jpg"})
@@ -0,0 +1,40 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+ require 'multi_json'
5
+
6
+ require 'betaface/version'
7
+ require 'betaface/util'
8
+ require 'betaface/util/client_config'
9
+ require 'betaface/rest/client'
10
+ require 'betaface/rest/errors'
11
+ require 'betaface/api'
12
+
13
+
14
+ module Betaface
15
+
16
+ def self.key
17
+ @key
18
+ end
19
+
20
+ def self.secret
21
+ @secret
22
+ end
23
+
24
+ def self.host
25
+ @host
26
+ end
27
+
28
+ def self.key=(key)
29
+ @key = key
30
+ end
31
+
32
+ def self.secret=(secret)
33
+ @secret = secret
34
+ end
35
+
36
+ def self.host=(host)
37
+ @host = host
38
+ end
39
+
40
+ end
@@ -0,0 +1,158 @@
1
+ module Betaface
2
+ class Api
3
+ include Betaface::REST
4
+
5
+ attr_reader :client
6
+
7
+ ##
8
+ # Do nothing for now
9
+ def initialize(*args)
10
+ @client ||= Betaface::REST::Client.new(*args)
11
+ end
12
+
13
+ #############################
14
+ # #
15
+ # RESOURCES #
16
+ # #
17
+ #############################
18
+
19
+
20
+ ##
21
+ # https://www.betafaceapi.com/service_json_ssl.svc/FaceInfo_Delete
22
+ # Delete image and faces information
23
+ # Note to the API dev: a REST DELETE instead of a POST is recommended
24
+ def face_info_delete(face_uid)
25
+ @client.post "/service_json_ssl.svc/FaceInfo_Delete", {face_uid:face_uid}
26
+ end
27
+
28
+ ##
29
+ # https://www.betafaceapi.com/service_json_ssl.svc/FaceInfo_New
30
+ # Tag new info about a face in an picture/image
31
+ def face_info_new(img_uid, faceinfo = {})
32
+ @client.post "/service_json_ssl.svc/FaceInfo_New", {img_uid:img_uid,faceinfo:faceinfo}
33
+ end
34
+
35
+ ##
36
+ # https://www.betafaceapi.com/service_json_ssl.svc/FaceInfo_SetFaceImagePoints
37
+ def face_info_set_face_image_points(face_uid, points = [])
38
+ @client.post "/service_json_ssl.svc/FaceInfo_SetFaceImagePoints", {face_uid:face_uid,points:points}
39
+ end
40
+
41
+ ##
42
+ # https://www.betafaceapi.com/service_json_ssl.svc/FaceInfo_SetPoints
43
+ def face_info_set_points(face_uid, points = [])
44
+ @client.post "/service_json_ssl.svc/FaceInfo_SetPoints", {face_uid:face_uid,points:points}
45
+ end
46
+
47
+ ##
48
+ # https://www.betafaceapi.com/service_json_ssl.svc/FaceInfo_SetTags
49
+ def face_info_set_tags(face_uid, tags = [])
50
+ @client.post "/service_json_ssl.svc/FaceInfo_SetTags", {face_uid:face_uid,tags:tags}
51
+ end
52
+
53
+ ##
54
+ # https://www.betafaceapi.com/service_json_ssl.svc/FaceInfo_Update
55
+ # Update info about a face in an picture/image
56
+ def face_info_update(face_uid, faceinfo = {})
57
+ @client.post "/service_json_ssl.svc/FaceInfo_Update", {face_uid:face_uid,faceinfo:faceinfo}
58
+ end
59
+
60
+ ##
61
+ # https://www.betafaceapi.com/service_json_ssl.svc/GetFaceImage
62
+ # Retrieve cropped face image and points coordinates in cropped face image coordinates
63
+ def get_face_image(face_uid)
64
+ @client.post "/service_json_ssl.svc/GetFaceImage", {face_uid:face_uid}
65
+ end
66
+
67
+ ##
68
+ # https://www.betafaceapi.com/service_json_ssl.svc/GetImageFileInfo
69
+ # Retrieve image and faces information or processing status using image file SHA-256 hash code
70
+ def get_image_file_info(img_checksum)
71
+ @client.post "/service_json_ssl.svc/GetImageFileInfo", {img_checksum:img_checksum}
72
+ end
73
+
74
+ ##
75
+ # https://www.betafaceapi.com/service_json_ssl.svc/GetImageInfo
76
+ # Retrieve image and faces information or processing status
77
+ def get_image_info(img_uid)
78
+ @client.post "/service_json_ssl.svc/GetImageInfo", {img_uid:img_uid}
79
+ end
80
+
81
+ ##
82
+ # https://www.betafaceapi.com/service_json_ssl.svc/GetRecognizeResult
83
+ # Get results of recognition request or processing status
84
+ def get_recognize_result(recognize_uid)
85
+ @client.post "/service_json_ssl.svc/GetRecognizeResult", {recognize_uid:recognize_uid}
86
+ end
87
+
88
+ ##
89
+ # https://www.betafaceapi.com/service_json_ssl.svc/GetTransformResult
90
+ # Get results of transform request or processing status
91
+ def get_transform_result(transform_uid)
92
+ @client.post "/service_json_ssl.svc/GetTransformResult", {transform_uid:transform_uid}
93
+ end
94
+
95
+ ##
96
+ # https://www.betafaceapi.com/service_json_ssl.svc/GetVerifyResult
97
+ def get_verify_result(verify_string)
98
+ @client.post "/service_json_ssl.svc/GetVerifyResult", {verify_string:verify_string}
99
+ end
100
+
101
+ ##
102
+ # https://www.betafaceapi.com/service_json_ssl.svc/RecognizeFaces
103
+ # Execute recognition request for each of face ids and specified list of targets
104
+ # (comma separated list either of face ids, or persons and namespaces as name@namespace.com or all@namespace.com)
105
+ def recognize_faces(faces_uids, targets, params)
106
+ @client.post "/service_json_ssl.svc/RecognizeFaces", {faces_uids:faces_uids,targets:targets,parameters:params}
107
+ end
108
+
109
+ ##
110
+ # https://www.betafaceapi.com/service_json_ssl.svc/SetPerson
111
+ # Assign person name (John Doe@mynamespace.com) to one or more face ids
112
+ def set_person(faces_uid, person_id)
113
+ @client.post "/service_json_ssl.svc/SetPerson", {faces_uids:faces_uids,person_id:person_id}
114
+ end
115
+
116
+ ##
117
+ # Assign person name (John Doe@mynamespace.com) to one or more face ids
118
+ # https://www.betafaceapi.com/service_json_ssl.svc/TransformFaces
119
+ def transform_faces(action, faces_points, faces_uids, params)
120
+ @client.post "/service_json_ssl.svc/TransformFaces", {faces_uids:faces_uids,action:action,faces_points:faces_points,parameters:params}
121
+ end
122
+
123
+ ##
124
+ # https://www.betafaceapi.com/service_json_ssl.svc/UploadImage
125
+ # Upload image url or base64 encoded string
126
+ # detection flags: propoints,classifiers,extended
127
+ # TODO Upload image file
128
+ def upload_image(detection_flags, image = {})
129
+ params = {detection_flags:detection_flags}
130
+ params[:url] = image[:url] if image[:url].present?
131
+ params[:original_filename] = image[:url].split("/")[-1] if image[:url].present? and image[:original_filename].blank?
132
+ params[:image_base64] = image[:base64] if image[:base64].present?
133
+ @client.post "/service_json_ssl.svc/UploadImage", params
134
+ end
135
+
136
+ ##
137
+ # https://www.betafaceapi.com/service_json_ssl.svc/UploadNewImage_File
138
+ # detection flags: propoints,classifiers,extended
139
+ # Upload image file as Base64Binary encoded string
140
+ # TODO Need to be fixed on the API Server
141
+ def upload_new_image_file(detection_flags, image = {})
142
+ @client.post "/service_json_ssl.svc/UploadNewImage_File", {detection_flags:detection_flags,imagefile_data:image[:data],original_filename:image[:original_filename]}
143
+ end
144
+
145
+ ##
146
+ # https://www.betafaceapi.com/service_json_ssl.svc/UploadNewImage_Url
147
+ # detection flags: propoints,classifiers,extended
148
+ # Upload image url
149
+ # TODO Need to be fixed on the API Server
150
+ def upload_new_image_url(detection_flags, image = {})
151
+ params = {detection_flags:detection_flags}
152
+ params[:url] = image[:url] if image[:url].present?
153
+ params[:original_filename] = image[:url].split("/")[-1] if image[:url].present? and image[:original_filename].blank?
154
+ @client.post "/service_json_ssl.svc/UploadNewImage_Url", params
155
+ end
156
+
157
+ end
158
+ end
@@ -0,0 +1,86 @@
1
+ module Betaface
2
+ module REST
3
+ class Client
4
+ include Betaface::Util
5
+
6
+ HTTP_HEADERS = {
7
+ 'Content-Type' => 'application/json',
8
+ 'Accept-Charset' => 'utf-8',
9
+ 'User-Agent' => "betaface/#{Betaface::VERSION}" \
10
+ " (#{RUBY_ENGINE}/#{RUBY_PLATFORM}" \
11
+ " #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})"
12
+ }
13
+
14
+ attr_reader :last_request, :last_response
15
+
16
+ ##
17
+ # Instantiate the Client with a key/secret if not set before
18
+ # Just call new if Betaface.key and Betaface.secret have been init
19
+ # Last argument is a hash of options
20
+ def initialize(*args)
21
+ @config = Betaface::Util::ClientConfig.new
22
+ @key = args[0] || Betaface.key
23
+ @secret = args[1] || Betaface.secret
24
+ if @key.nil? || @secret.nil?
25
+ raise ArgumentError, 'Key and Secret are required'
26
+ end
27
+ end
28
+
29
+ # let's define all rest calls even though betaface currently supports only POST
30
+ # Maybe they'll adopt some RESTful best practice soon :)
31
+ [:get, :put, :post, :delete].each do |method|
32
+ method_class = Net::HTTP.const_get method.to_s.capitalize
33
+ define_method method do |path, *args|
34
+ payload = args[0]
35
+ payload ||= {}
36
+ @config.host.chomp!("/")
37
+ path = ((path[0] == "/") ? path : ("/" + path).to_s)
38
+ uri = URI(@config.host + path)
39
+ request = method_class.new(uri, HTTP_HEADERS)
40
+ payload[:api_secret] = @secret
41
+ payload[:api_key] = @key
42
+ request.body = payload.to_json if [:post, :put].include?(method)
43
+ connect_and_send(request)
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ ##
50
+ # Send an HTTP request using the cached <tt>@connection</tt> object and
51
+ # return the JSON response body parsed into a hash. Also save the raw
52
+ # Net::HTTP::Request and Net::HTTP::Response objects as
53
+ # <tt>@last_request</tt> and <tt>@last_response</tt> to allow for
54
+ # inspection later.
55
+ def connect_and_send(request)
56
+ @last_request = request
57
+ retries_left = @config.retry_limit
58
+ begin
59
+ http = Net::HTTP.new(request.uri.host, request.uri.port)
60
+ http.use_ssl = true if request.uri.scheme == "https"
61
+ response = http.start do |h|
62
+ h.request(request)
63
+ end
64
+ @last_response = response
65
+ if response.kind_of? Net::HTTPServerError
66
+ raise Betaface::REST::ServerError
67
+ end
68
+ rescue
69
+ raise if request.class == Net::HTTP::Post
70
+ if retries_left > 0 then retries_left -= 1; retry else raise end
71
+ end
72
+ if response.body and !response.body.empty?
73
+ object = MultiJson.load response.body
74
+ elsif response.kind_of? Net::HTTPBadRequest
75
+ object = { message: 'Bad request', code: 400 }
76
+ end
77
+
78
+ if response.kind_of? Net::HTTPClientError
79
+ raise Betaface::REST::RequestError.new object['message'], object['code']
80
+ end
81
+ object
82
+ end
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,14 @@
1
+ module Betaface
2
+ module REST
3
+ class ServerError < StandardError; end
4
+
5
+ class RequestError < StandardError
6
+ attr_reader :code
7
+
8
+ def initialize(message, code=nil);
9
+ super message
10
+ @code = code
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Betaface
2
+ module Util
3
+ def url_encode(hash)
4
+ hash.to_a.map {|p| p.map {|e| CGI.escape get_string(e)}.join '='}.join '&'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module Betaface
2
+ module Util
3
+ class ClientConfig
4
+ DEFAULTS = {
5
+ host: 'https://www.betafaceapi.com/',
6
+ port: 443,
7
+ use_ssl: true,
8
+ timeout: 30,
9
+ retry_limit: 1
10
+ }
11
+
12
+ DEFAULTS.each_key do |attribute|
13
+ attr_accessor attribute
14
+ end
15
+
16
+ def initialize(opts={})
17
+ DEFAULTS.each do |attribute, value|
18
+ send("#{attribute}=".to_sym, opts.fetch(attribute, value))
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Betaface
2
+ VERSION = '0.3.1'
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: betaface
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Jean Meyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.0
27
+ description: A ruby Gem to easily use the betaface API without having to deal with
28
+ all this XLM
29
+ email: jean@getonce.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - README.md
35
+ - betaface.gemspec
36
+ - examples/get_image_info.rb
37
+ - examples/upload_from_url.rb
38
+ - lib/betaface.rb
39
+ - lib/betaface/api.rb
40
+ - lib/betaface/rest/client.rb
41
+ - lib/betaface/rest/errors.rb
42
+ - lib/betaface/util.rb
43
+ - lib/betaface/util/client_config.rb
44
+ - lib/betaface/version.rb
45
+ homepage: http://rubygems.org/gems/betaface
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: 1.9.3
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.14
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Use the betaface API with Ruby!
69
+ test_files: []
70
+ has_rdoc: