ruby_face_recognition 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b899c91149dec1631859fab73f3020d15360724e74c804902b4a0e30f45e9c2
4
+ data.tar.gz: 0de3145d5c735ca74db4a0f33d915b8c20c1c7b252522a89fa9b91904b76c860
5
+ SHA512:
6
+ metadata.gz: 466bbce79a3416be655661da5c8e06099f79abd35f5214b9d4041ebccac1707d1499cb3d959264bc74907a1fc485114ec691ec4b211d9d20b07a2092d70d17ed
7
+ data.tar.gz: 8b5ec85e4f3719e4205d05ce4432ec829c5160f00800ee3a45e91643297cb4d06ec4fc97efe668b43de2d03496d2e5d5f41b18220767675130a17430d5b6f67e
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .DS_Store
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rogerluo410@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_face_recognition.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~> 12.0'
7
+ gem 'rspec', '~> 3.0'
8
+
9
+ gem 'pycall', '~> 1.4.1'
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_face_recognition (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ pycall (1.4.1)
11
+ rake (12.3.3)
12
+ rspec (3.11.0)
13
+ rspec-core (~> 3.11.0)
14
+ rspec-expectations (~> 3.11.0)
15
+ rspec-mocks (~> 3.11.0)
16
+ rspec-core (3.11.0)
17
+ rspec-support (~> 3.11.0)
18
+ rspec-expectations (3.11.1)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.11.0)
21
+ rspec-mocks (3.11.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-support (3.11.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ pycall (~> 1.4.1)
31
+ rake (~> 12.0)
32
+ rspec (~> 3.0)
33
+ ruby_face_recognition!
34
+
35
+ BUNDLED WITH
36
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 rogerluo410
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.
data/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # RubyFaceRecognition
2
+
3
+ A Ruby wrapper for recognize and manipulate faces.
4
+
5
+ ## Enables
6
+
7
+ * Fetch face encoding.
8
+ * Get distance between two face encodings.
9
+ * Compare two faces are same person.
10
+
11
+ ## Dependencies
12
+
13
+ - pycall >= 1.4.1, see https://github.com/mrkn/pycall.rb for more.
14
+
15
+ - python 3
16
+
17
+ * Install python 3.7 and face recognition
18
+
19
+ 1) If use `pyenv`, install python with command to load libpython in pycall:
20
+
21
+ envPYTHON_CONFIGURE_OPTS="--enable-framework" CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.7.0
22
+
23
+
24
+ 2) Export LIBPYTHON environment variable:
25
+
26
+ export LIBPYTHON="/Users/xxx/.pyenv/versions/3.7.0/lib/libpython3.7m.a"
27
+
28
+ 3) Install face recognition tool, see https://github.com/ageitgey/face_recognition:
29
+
30
+ pip3 install face_recognition
31
+
32
+ * Another way for ubuntu
33
+
34
+ 1) Make install python3 and pip3
35
+
36
+ 2) Install dlib
37
+
38
+ git clone https://github.com/davisking/dlib.git
39
+ cd dlib
40
+ mkdir build; cd build; cmake ..; cmake --build .
41
+ cd ..
42
+ python3 setup.py install
43
+
44
+ 3) Install plugin
45
+
46
+ pip3 install face_recognition
47
+
48
+ - face_recognition, see https://github.com/ageitgey/face_recognition for more.
49
+
50
+ ## Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ ```ruby
55
+ gem 'ruby_face_recognition'
56
+ ```
57
+
58
+ And then execute:
59
+
60
+ $ bundle install
61
+
62
+ Or install it yourself as:
63
+
64
+ $ gem install ruby_face_recognition
65
+
66
+ ## Usage
67
+
68
+ ```ruby
69
+ require 'ruby_face_recognition'
70
+
71
+ # See ./examples folder for images
72
+ wdz1_doubles = FaceRecognition.fetch_face_encoding('./wdz1.jpeg')
73
+ wdz2_doubles = FaceRecognition.fetch_face_encoding('./wdz2.jpeg')
74
+ ycg1_doubles = FaceRecognition.fetch_face_encoding('./ycg1.jpeg')
75
+ ycg2_doubles = FaceRecognition.fetch_face_encoding('./ycg2.jpeg')
76
+
77
+ pp "wdz1 and wdz2 is same: #{FaceRecognition.same_person?(wdz1_doubles,
78
+ wdz2_doubles)}, tolerant is #{FaceRecognition.get_tolerant(
79
+ wdz1_doubles, wdz2_doubles
80
+ )}"
81
+ pp "wdz1 and ycg1 is same: #{FaceRecognition.same_person?(wdz1_doubles,
82
+ ycg1_doubles)}, tolerant is #{FaceRecognition.get_tolerant(
83
+ wdz1_doubles, ycg1_doubles
84
+ )}"
85
+ pp "ycg1 and ycg2 is same: #{FaceRecognition.same_person?(ycg1_doubles,
86
+ ycg2_doubles)}, tolerant is #{FaceRecognition.get_tolerant(
87
+ ycg1_doubles, ycg2_doubles
88
+ )}"
89
+
90
+ #Output:
91
+ "wdz1 and wdz2 is same: true, tolerant is 0.31643992115339953"
92
+ "wdz1 and ycg1 is same: false, tolerant is 1.1141872408732252"
93
+ "ycg1 and ycg2 is same: true, tolerant is 0.338220706580959"
94
+
95
+ ```
96
+
97
+ ### Use in Rails
98
+
99
+ Assuming Database is postgresql:
100
+
101
+ - Create field to save face encoding.
102
+
103
+ `add_column :table_name, :column_name, :float, array: true, comment: 'face encoding'`
104
+
105
+ - Create distance function script to Databse.
106
+
107
+ ```ruby
108
+ class AddDistanceFunctionToDb < ActiveRecord::Migration[6.1]
109
+ def self.up
110
+ execute "CREATE OR REPLACE FUNCTION distance(l double precision[], r double precision[]) RETURNS double precision AS $$
111
+ DECLARE
112
+ s double precision;
113
+ BEGIN
114
+ s := 0;
115
+ FOR i IN 1..128 LOOP
116
+ s := s + power(l[i] - r[i], 2);
117
+ END LOOP;
118
+ RETURN sqrt(s);
119
+ END;
120
+ $$ LANGUAGE plpgsql;"
121
+ end
122
+ def self.down
123
+ execute "drop function distance(l double precision[], r double precision[]) cascade"
124
+ end
125
+ end
126
+
127
+ ```
128
+
129
+ - Use distance function in SQL
130
+
131
+ ```sql
132
+ tolerant = ActiveRecord::Base.connection.exec_query("select distance(ARRAY#{face_encoding1}, ARRAY#{face_encoding2})").rows[0][0]
133
+ ```
134
+
135
+ ## Development
136
+
137
+ 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.
138
+
139
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby_face_recognition. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ruby_face_recognition/blob/master/CODE_OF_CONDUCT.md).
144
+
145
+
146
+ ## License
147
+
148
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
149
+
150
+ ## Code of Conduct
151
+
152
+ Everyone interacting in the RubyFaceRecognition project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ruby_face_recognition/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby_face_recognition"
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(__FILE__)
data/bin/setup ADDED
@@ -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,19 @@
1
+ require 'ruby_face_recognition'
2
+
3
+ wdz1_doubles = FaceRecognition.fetch_face_encoding('./wdz1.jpeg')
4
+ wdz2_doubles = FaceRecognition.fetch_face_encoding('./wdz2.jpeg')
5
+ ycg1_doubles = FaceRecognition.fetch_face_encoding('./ycg1.jpeg')
6
+ ycg2_doubles = FaceRecognition.fetch_face_encoding('./ycg2.jpeg')
7
+
8
+ pp "wdz1 and wdz2 is same: #{FaceRecognition.same_person?(wdz1_doubles,
9
+ wdz2_doubles)}, tolerant is #{FaceRecognition.get_tolerant(
10
+ wdz1_doubles, wdz2_doubles
11
+ )}"
12
+ pp "wdz1 and ycg1 is same: #{FaceRecognition.same_person?(wdz1_doubles,
13
+ ycg1_doubles)}, tolerant is #{FaceRecognition.get_tolerant(
14
+ wdz1_doubles, ycg1_doubles
15
+ )}"
16
+ pp "ycg1 and ycg2 is same: #{FaceRecognition.same_person?(ycg1_doubles,
17
+ ycg2_doubles)}, tolerant is #{FaceRecognition.get_tolerant(
18
+ ycg1_doubles, ycg2_doubles
19
+ )}"
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,46 @@
1
+ require 'pycall'
2
+ require 'pycall/import'
3
+ require 'logger'
4
+
5
+ PyCall.init('python3')
6
+
7
+ LOGGER = Logger.new(STDOUT)
8
+
9
+ # wrapper for face_recognition
10
+ class FaceRecognition
11
+ class << self
12
+ def fetch_face_encoding(image_full_path)
13
+ begin # rubocop:disable Style/RedundantBegin
14
+ face_recognition = PyCall.import_module('face_recognition') # use python wrap in ruby class domain
15
+ known_image = face_recognition.load_image_file(image_full_path)
16
+ face_encoding = face_recognition.face_encodings(known_image)[0]
17
+ list = face_encoding.tolist() # rubocop:disable Style/MethodCallWithoutArgsParentheses
18
+ list.to_a
19
+ rescue PyCall::PyError => e
20
+ LOGGER.fatal e
21
+ end
22
+ end
23
+
24
+ def same_person?(l_doubles, r_doubles)
25
+ raise 'l_doubles must be Array' unless l_doubles.is_a? Array
26
+ raise 'r_doubles must be Array' unless r_doubles.is_a? Array
27
+
28
+ # If the tolerant value less than 0.55, judge they are same person.
29
+ get_tolerant(l_doubles, r_doubles) < 0.55
30
+ end
31
+
32
+ def get_tolerant(l_doubles, r_doubles)
33
+ raise 'l_doubles must be Array' unless l_doubles.is_a? Array
34
+ raise 'r_doubles must be Array' unless r_doubles.is_a? Array
35
+ raise 'l_doubles must have 128 doubles' unless l_doubles.length == 128
36
+ raise 'r_doubles must have 128 doubles' unless r_doubles.length == 128
37
+
38
+ s = 0
39
+ (0..127).to_a.each do |i|
40
+ s += (l_doubles[i] - r_doubles[i]) * 2
41
+ end
42
+
43
+ Math.sqrt(s)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module RubyFaceRecognition
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'ruby_face_recognition/version'
2
+ require_relative 'ruby_face_recognition/ruby_face_recognition'
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/ruby_face_recognition/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'ruby_face_recognition'
5
+ spec.version = RubyFaceRecognition::VERSION
6
+ spec.authors = ['rogerluo410']
7
+ spec.email = ['rogerluo410@gmail.com']
8
+
9
+ spec.summary = 'A Ruby wrapper for face recognition.'
10
+ spec.description = 'A Ruby wrapper for recognize and manipulate faces.'
11
+ spec.homepage = 'https://github.com/rogerluo410/ruby_face_recognition'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://github.com/rogerluo410/ruby_face_recognition'
19
+ spec.metadata['changelog_uri'] = 'https://github.com/rogerluo410/ruby_face_recognition'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_face_recognition
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rogerluo410
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby wrapper for recognize and manipulate faces.
14
+ email:
15
+ - rogerluo410@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - examples/example.rb
32
+ - examples/wdz1.jpeg
33
+ - examples/wdz2.jpeg
34
+ - examples/ycg1.jpeg
35
+ - examples/ycg2.jpeg
36
+ - lib/ruby_face_recognition.rb
37
+ - lib/ruby_face_recognition/ruby_face_recognition.rb
38
+ - lib/ruby_face_recognition/version.rb
39
+ - ruby_face_recognition.gemspec
40
+ homepage: https://github.com/rogerluo410/ruby_face_recognition
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ allowed_push_host: https://rubygems.org/
45
+ homepage_uri: https://github.com/rogerluo410/ruby_face_recognition
46
+ source_code_uri: https://github.com/rogerluo410/ruby_face_recognition
47
+ changelog_uri: https://github.com/rogerluo410/ruby_face_recognition
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.6
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A Ruby wrapper for face recognition.
67
+ test_files: []