face_detect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa441dc7c2d9525533a9b65f43813791871a4c9d
4
+ data.tar.gz: 862176e4951cb4ceee392d0c2480da02a202f714
5
+ SHA512:
6
+ metadata.gz: 16b5de33d46972b9cb274f76f4c5c8538c6b9abbb4c367be5f4d65b08bd86ad0e0cc9f3af68099bce936bb047cf37fa05b2b894e5b8af39f30fe7bc57e5b1112
7
+ data.tar.gz: 5771a878d9bc011a2a5b25c706c94f9cc22a2fa1da23016d5537b6720a49e898ac8f10b3b3108783433be7edb15a642b3caea706318ec6588c5c9ab2955cd815
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /mustachio-*.json
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.1
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at aidan.feldman@gsa.gov. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
@@ -0,0 +1,7 @@
1
+ ## Development
2
+
3
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
4
+
5
+ ## Contributing
6
+
7
+ Bug reports and pull requests are welcome on GitHub at https://github.com/afeld/face_detect. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Aidan Feldman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Face Detect
2
+
3
+ A Ruby gem for face detection in images. Also includes detection of facial features/landmarks. The backends are swappable.
4
+
5
+ While the intention is to support multiple adapters, the only one available for now is the [**Google Cloud Vision API**](https://cloud.google.com/vision/).
6
+
7
+ ## Installation
8
+
9
+ Requires Ruby 2.x.
10
+
11
+ 1. Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'face_detect'
15
+ gem 'googleauth'
16
+ gem 'google-api-client'
17
+ ```
18
+
19
+ 1. Execute:
20
+
21
+ ```bash
22
+ bundle
23
+ ```
24
+
25
+ 1. [Create a JSON service account key.](https://console.cloud.google.com/apis/credentials/serviceaccountkey)
26
+
27
+ ## Usage
28
+
29
+ 1. The JSON file that you downloaded contains your credentials. Set the full contents as an environment variable.
30
+
31
+ ```bash
32
+ export GOOGLE_CREDENTIALS_JSON=$(cat <credentials>.json)
33
+ ```
34
+
35
+ 1. Add the following to your code:
36
+
37
+ ```ruby
38
+ require 'face_detect'
39
+ require 'face_detect/adapter/google'
40
+
41
+ input = File.new('image.png')
42
+ detector = FaceDetect.new(
43
+ file: input,
44
+ adapter: FaceDetect::Adapter::Google
45
+ )
46
+ results = detector.run
47
+ results #=> [<FaceDetect::Face>, ...]
48
+ face = results.first
49
+ face.mouth_left.x #=> 225.06
50
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "face_detect"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'face_detect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "face_detect"
8
+ spec.version = FaceDetect::VERSION
9
+ spec.authors = ["Aidan Feldman"]
10
+ spec.email = ["aidan.feldman@gmail.com"]
11
+
12
+ spec.summary = %q{Simple face detection}
13
+ spec.homepage = "https://github.com/afeld/face_detect"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.required_ruby_version = '~> 2.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.12"
24
+ spec.add_development_dependency "google-api-client", "~> 0.9.6"
25
+ spec.add_development_dependency "googleauth", "~> 0.5.1"
26
+ spec.add_development_dependency "pry-byebug", "~> 3.3.0"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "webmock", "~> 2.0"
30
+ end
@@ -0,0 +1,15 @@
1
+ Dir[File.expand_path('face_detect/*.rb', __dir__)].each { |file| require file }
2
+
3
+ class FaceDetect
4
+ attr_reader :file, :adapter_instance
5
+
6
+ # TODO accept a File or a URL
7
+ def initialize(file:, adapter:)
8
+ @file = file
9
+ @adapter_instance = adapter.new(file)
10
+ end
11
+
12
+ def run
13
+ adapter_instance.run
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ require_relative 'google/auth_helper'
2
+ require_relative 'google/service'
3
+
4
+ class FaceDetect
5
+ module Adapter
6
+ class Google
7
+ attr_reader :file
8
+
9
+ def initialize(file)
10
+ @file = file
11
+ end
12
+
13
+ def run
14
+ batch_response = execute
15
+ response = batch_response.responses.first
16
+ response.face_annotations.map do |annotation|
17
+ convert_face(annotation)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def authorization
24
+ AuthHelper.authorization
25
+ end
26
+
27
+ def execute
28
+ service = Service.new(authorization, file)
29
+ batch_response = service.run
30
+ end
31
+
32
+ def landmarks_by_name(annotation)
33
+ results = {}
34
+ annotation.landmarks.each do |google_landmark|
35
+ name = google_landmark.type.downcase
36
+ results[name] = convert_landmark(google_landmark)
37
+ end
38
+ results
39
+ end
40
+
41
+ def convert_face(annotation)
42
+ FaceDetect::Face.new(landmarks_by_name(annotation))
43
+ end
44
+
45
+ def convert_landmark(google_landmark)
46
+ pos = google_landmark.position
47
+ FaceDetect::Landmark.new(pos.x, pos.y)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ require 'googleauth'
2
+ require 'stringio'
3
+
4
+ class FaceDetect
5
+ module Adapter
6
+ class Google
7
+ module AuthHelper
8
+ class << self
9
+ def scopes
10
+ ['https://www.googleapis.com/auth/cloud-platform']
11
+ end
12
+
13
+ def credentials
14
+ ENV.fetch('GOOGLE_CREDENTIALS_JSON')
15
+ end
16
+
17
+ def authorization
18
+ key_io = StringIO.new(credentials)
19
+ ::Google::Auth::ServiceAccountCredentials.make_creds(
20
+ json_key_io: key_io,
21
+ scope: scopes
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,58 @@
1
+ require 'google/apis/vision_v1'
2
+
3
+ class FaceDetect
4
+ module Adapter
5
+ class Google
6
+ class Service
7
+ # alias the module
8
+ # http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/VisionV1/VisionService
9
+ Vision = ::Google::Apis::VisionV1
10
+
11
+ attr_reader :authorization, :file
12
+
13
+ def initialize(authorization, file)
14
+ @authorization = authorization
15
+ @file = file
16
+ end
17
+
18
+ def run
19
+ service.authorization = authorization
20
+ service.annotate_image(batch_request, fields: fields)
21
+ end
22
+
23
+ private
24
+
25
+ def service
26
+ @service ||= Vision::VisionService.new
27
+ end
28
+
29
+ def image_contents
30
+ File.read(file)
31
+ end
32
+
33
+ def image
34
+ Vision::Image.new(content: image_contents)
35
+ end
36
+
37
+ def features
38
+ [Vision::Feature.new(type: 'FACE_DETECTION')]
39
+ end
40
+
41
+ def batch_request
42
+ @batch_request ||= Vision::BatchAnnotateImagesRequest.new(
43
+ requests: [
44
+ {
45
+ image: image,
46
+ features: features
47
+ }
48
+ ]
49
+ )
50
+ end
51
+
52
+ def fields
53
+ 'responses(faceAnnotations(landmarks))'
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,54 @@
1
+ require 'set'
2
+
3
+ class FaceDetect
4
+ class Face
5
+ # https://cloud.google.com/vision/reference/rest/v1/images/annotate#Landmark
6
+ LANDMARK_NAMES = %i(
7
+ left_eye
8
+ right_eye
9
+ left_of_left_eyebrow
10
+ right_of_left_eyebrow
11
+ left_of_right_eyebrow
12
+ right_of_right_eyebrow
13
+ midpoint_between_eyes
14
+ nose_tip
15
+ upper_lip
16
+ lower_lip
17
+ mouth_left
18
+ mouth_right
19
+ mouth_center
20
+ nose_bottom_right
21
+ nose_bottom_left
22
+ nose_bottom_center
23
+ left_eye_top_boundary
24
+ left_eye_right_corner
25
+ left_eye_bottom_boundary
26
+ left_eye_left_corner
27
+ right_eye_top_boundary
28
+ right_eye_right_corner
29
+ right_eye_bottom_boundary
30
+ right_eye_left_corner
31
+ left_eyebrow_upper_midpoint
32
+ right_eyebrow_upper_midpoint
33
+ left_ear_tragion
34
+ right_ear_tragion
35
+ left_eye_pupil
36
+ right_eye_pupil
37
+ forehead_glabella
38
+ chin_gnathion
39
+ chin_left_gonion
40
+ chin_right_gonion
41
+ ).to_set.freeze
42
+
43
+ attr_reader *LANDMARK_NAMES
44
+
45
+ def initialize(landmarks_by_name={})
46
+ landmarks_by_name.each do |name, landmark|
47
+ unless LANDMARK_NAMES.include?(name.to_sym)
48
+ raise "'#{name}' not a valid landmark name."
49
+ end
50
+ instance_variable_set("@#{name}", landmark)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ class FaceDetect
2
+ class Landmark
3
+ attr_reader :x, :y
4
+
5
+ def initialize(x, y)
6
+ @x = x.to_f
7
+ @y = y.to_f
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ class FaceDetect
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ # After following the installation steps in the README, run
2
+ #
3
+ # bundle exec ruby script/test_google_full.rb
4
+ #
5
+ # You can also specify the IMG environment variable as a path to an image if you want to test with something else.
6
+ #
7
+ require 'pp'
8
+
9
+ require_relative '../lib/face_detect'
10
+ require_relative '../lib/face_detect/adapter/google'
11
+
12
+ path = ENV['IMG'] || File.expand_path('../spec/fixtures/obama.jpg', __dir__)
13
+ input = File.new(path)
14
+ detector = FaceDetect.new(
15
+ file: input,
16
+ adapter: FaceDetect::Adapter::Google
17
+ )
18
+ result = detector.run
19
+ pp result
@@ -0,0 +1,18 @@
1
+ # After following the installation steps in the README, run
2
+ #
3
+ # bundle exec ruby script/test_google_adatper.rb
4
+ #
5
+ # You can also specify the IMG environment variable as a path to an image if you want to test with something else.
6
+ #
7
+ require 'json'
8
+
9
+ require_relative '../lib/face_detect'
10
+ require_relative '../lib/face_detect/adapter/google'
11
+
12
+ path = ENV['IMG'] || File.expand_path('../spec/fixtures/obama.jpg', __dir__)
13
+ input = File.new(path)
14
+
15
+ authorization = FaceDetect::Adapter::Google::AuthHelper.authorization
16
+ service = FaceDetect::Adapter::Google::Service.new(authorization, input)
17
+ result = service.run
18
+ puts result.to_json
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: face_detect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aidan Feldman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-api-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.6
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: googleauth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '2.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ description:
112
+ email:
113
+ - aidan.feldman@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - CONTRIBUTING.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - face_detect.gemspec
130
+ - lib/face_detect.rb
131
+ - lib/face_detect/adapter/google.rb
132
+ - lib/face_detect/adapter/google/auth_helper.rb
133
+ - lib/face_detect/adapter/google/service.rb
134
+ - lib/face_detect/face.rb
135
+ - lib/face_detect/landmark.rb
136
+ - lib/face_detect/version.rb
137
+ - script/test_google_full.rb
138
+ - script/test_google_service.rb
139
+ homepage: https://github.com/afeld/face_detect
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '2.0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.6.3
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Simple face detection
163
+ test_files: []