baiduAiFace 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72f22e710b1ca69399d2aac510693aafc64d984f
4
- data.tar.gz: 025cce54db318097aaa7b4b5717835a2f1f0f7bc
3
+ metadata.gz: 757c7d60b68d51b76a4b0aa08860d19cf3a53676
4
+ data.tar.gz: 0ee2aa6d224b3978e3535a379ced0c8a700a2172
5
5
  SHA512:
6
- metadata.gz: 6efb4adfdeeeefed3f07465df316a76bab5f7e7e9af553440b01e002fd05d91a683194c1d010798ccd017a73f02a6eb9d1064a77328985358e30989daf313506
7
- data.tar.gz: 344401c87e1f29f081ea078ecb8fec11c2611adf9230a37e7c4162b754fd6c0e628a1369c0bc700087ecba4a354ed9bc4ea01468c65b7799a3adec645ac6f6a4
6
+ metadata.gz: dd9d78a3ed2d74592311f63f7bb49019d54e2874eb0e38e32a0ca8c5e8793500aac0143114d67dfae2ef80da47002d7680de8eb7b861cf1036ae6c2dad56c13b
7
+ data.tar.gz: 5b3cf839b394e843999b0594eef28686c4a7ee9c5a6487dbc835cf82da2fc88c86bf4b05b1f569118236fa7fd520842ffa25b320741a6f3971380257084b67b6
@@ -0,0 +1,150 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'mime/types'
4
+ require 'rest-client'
5
+ require 'securerandom'
6
+ require 'uri'
7
+
8
+ unless Object.respond_to? :singleton_class
9
+ class Object
10
+ def singleton_class
11
+ class << self; self; end
12
+ end
13
+ end
14
+ end
15
+
16
+ unless Object.respond_to? :define_singleton_method
17
+ class Object
18
+ def define_singleton_method name, &block
19
+ singleton_class.send :define_method, name, &block
20
+ end
21
+ end
22
+ end
23
+
24
+ unless URI.respond_to? :encode_www_form
25
+ module URI
26
+ TBLENCWWWCOMP_ = {} # :nodoc:
27
+ 256.times do |i|
28
+ TBLENCWWWCOMP_[i.chr] = '%%%02X' % i
29
+ end
30
+ TBLENCWWWCOMP_[' '] = '+'
31
+ TBLENCWWWCOMP_.freeze
32
+
33
+ def self.encode_www_form_component str
34
+ str.to_s.gsub(/[^*\-.0-9A-Z_a-z]/) {|c| TBLENCWWWCOMP_[c] }
35
+ end
36
+
37
+ def self.encode_www_form enum
38
+ enum.map {|k,v| "#{encode_www_form_component k}=#{encode_www_form_component v}" }.join '&'
39
+ end
40
+ end
41
+ end
42
+
43
+ class FaceAi
44
+ class MultiPart
45
+ def initialize
46
+ @fields = []
47
+ @files = []
48
+ @boundary = [SecureRandom.random_bytes(15)].pack('m*').chop!
49
+ end
50
+
51
+ def add_field name, value
52
+ @fields << [name, value]
53
+ end
54
+
55
+ def add_file name, filepath
56
+ @files << [name, filepath]
57
+ end
58
+
59
+ def self.guess_mime filename
60
+ res = MIME::Types.type_for(filename)
61
+ res.empty? ? 'application/octet-stream' : res[0]
62
+ end
63
+
64
+ def has_file?
65
+ not @files.empty?
66
+ end
67
+
68
+ def content_type
69
+ "multipart/form-data; boundary=#{@boundary}"
70
+ end
71
+
72
+ def inspect
73
+ res = StringIO.new
74
+ append_boundary = lambda { res.write "--#{@boundary}\r\n" }
75
+ @fields.each do |field|
76
+ append_boundary[]
77
+ res.write "Content-Disposition: form-data; name=\"#{field[0]}\"\r\n\r\n#{field[1]}\r\n"
78
+ end
79
+ @files.each do |file|
80
+ append_boundary[]
81
+ res.write "Content-Disposition: file; name=\"#{file[0]}\"; filename=\"#{file[1]}\"\r\n"
82
+ res.write "Content-Type: #{self.class.guess_mime file[1]}\r\n"
83
+ res.write "Content-Transfer-Encoding: binary\r\n\r\n"
84
+ res.write File.open(file[1]).read
85
+ res.write "\r\n"
86
+ end
87
+ res.write "--#{@boundary}--\r\n"
88
+ res.rewind
89
+ res.read
90
+ end
91
+ end
92
+
93
+ APIS = [
94
+ 'search',
95
+
96
+ '/faceset/user/add',
97
+ '/faceset/user/update',
98
+ '/faceset/face/delete',
99
+
100
+ '/faceset/face/getlist',
101
+
102
+ '/faceset/group/add',
103
+ '/faceset/group/delete',
104
+ '/faceset/group/getlist'
105
+ ]
106
+
107
+ def initialize(access_token, options={})
108
+ decode = options.fetch :decode, true
109
+ make_hash = lambda { Hash.new {|h,k| h[k] = make_hash.call make_hash } }
110
+
111
+ APIS.each do |api|
112
+ m = self
113
+ breadcrumbs = api.split('/')[1..-1]
114
+ breadcrumbs[0..-2].each do |breadcrumb|
115
+ unless m.instance_variable_defined? "@#{breadcrumb}"
116
+ m.instance_variable_set "@#{breadcrumb}", Object.new
117
+ m.singleton_class.class_eval do
118
+ attr_reader breadcrumb
119
+ end
120
+ end
121
+ m = m.instance_variable_get "@#{breadcrumb}"
122
+ end
123
+
124
+ m.define_singleton_method breadcrumbs[-1] do |*args|
125
+ form = MultiPart.new
126
+ fields = {'access_token' => access_token}
127
+ (args[0] || {}).each do |k,v|
128
+ if k.to_s == 'img' # via POST
129
+ form.add_file k, v
130
+ else
131
+ fields[k] = v.is_a?(Enumerable) ? v.to_a.join(',') : v
132
+ end
133
+ end
134
+
135
+ if form.has_file?
136
+ req.set_content_type form.content_type
137
+ req.body = form.inspect
138
+ req['Content-Length'] = req.body.size
139
+ end
140
+ url = "https://api-cn.faceplusplus.com/facepp/v3#{api}"
141
+ begin
142
+ response = RestClient.post url, fields
143
+ rescue ExceptionName
144
+ return []
145
+ end
146
+ response
147
+ end
148
+ end
149
+ end
150
+ end
@@ -1,3 +1,3 @@
1
1
  module BaiduAiFace
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baiduAiFace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - weichengzhu
@@ -59,18 +59,11 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".rspec"
64
- - ".travis.yml"
65
- - CODE_OF_CONDUCT.md
66
- - Gemfile
67
- - LICENSE.txt
68
62
  - README.md
69
- - Rakefile
70
- - baiduAiFace.gemspec
71
63
  - bin/console
72
64
  - bin/setup
73
65
  - lib/baiduAiFace.rb
66
+ - lib/baiduAiFace/client.rb
74
67
  - lib/baiduAiFace/version.rb
75
68
  homepage: http://rubygem.org
76
69
  licenses:
data/.gitignore DELETED
@@ -1,11 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.4.0
5
- before_install: gem install bundler -v 1.16.1
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
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 weicheng.zhu@icloud.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 [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in baiduAiFace.gemspec
6
- gemspec
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2018 weichengzhu
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/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
data/baiduAiFace.gemspec DELETED
@@ -1,31 +0,0 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "baiduAiFace/version"
5
- require "baiduAiFace/client"
6
-
7
- Gem::Specification.new do |spec|
8
- spec.name = "baiduAiFace"
9
- spec.version = BaiduAiFace::VERSION
10
- spec.authors = ["weichengzhu"]
11
- spec.email = ["weicheng.zhu@icloud.com"]
12
-
13
- spec.summary = %q{百度简单人脸识别.}
14
- spec.description = %q{百度简单人脸识别.}
15
- spec.homepage = "http://rubygem.org"
16
- spec.license = "MIT"
17
-
18
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
19
- # to allow pushing to a single host or delete this section to allow pushing to any host.
20
-
21
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
22
- f.match(%r{^(test|spec|features)/})
23
- end
24
- spec.bindir = "exe"
25
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
- spec.require_paths = ["lib"]
27
-
28
- spec.add_development_dependency "bundler", "~> 1.16"
29
- spec.add_development_dependency "rake", "~> 10.0"
30
- spec.add_development_dependency "rspec", "~> 3.0"
31
- end