cardreader 0.2.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6f58f99cdfec66688cc0244dd0353f2e868a1ba8f17679e74e16b6b06321c875
4
+ data.tar.gz: 076b5860352606397e03cd1c5367b262b250d0072602d6a4382b25241fa683b4
5
+ SHA512:
6
+ metadata.gz: 668a2ea279fa105561a710819f464a79759c2dbf1557c4bb8fc9568999c720d2a8032506186c8f0be0b05c7d61d45dc724b2221e25d700331bb1e1cbf25df391
7
+ data.tar.gz: 63f1ba72059b275268643b8fef8ec2393357d4c9e176921f060b5553477dae0b20b73697f73559fcc2144fda0bbdcfa8db9e72f58da59869876c3141acc4ffe6
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cardreader.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 netzilla
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,55 @@
1
+ # Cardreader
2
+ Detect a business card information using Google Cloud Vison API.
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'cardreader'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install cardreader
19
+
20
+ ## Usage
21
+ Prepare a business card.
22
+ ![A Business Card Sample](https://raw.githubusercontent.com/alpha-netzilla/cardreader/master/example/card.png "card")
23
+
24
+
25
+ Set you API key for Google Cloud and use detect method with an argument of a picture.
26
+ ```ruby
27
+ manage = Cardreader::Manage.new (
28
+ key = "******" # Google Cloud Vison API key
29
+ )
30
+
31
+ manage.detect("example/card.jpg")
32
+ ```
33
+
34
+ Returned JSON
35
+ ```
36
+ {
37
+ "person": "Shinya Funaki",
38
+ "organization": " Technology Planning Department",
39
+ "location": " Higashi-Shimbashi Minato-Ku Tokyo",
40
+ "mail": "alpha.netzilla@gmail.com",
41
+ "url": "http://example.com/",
42
+ "phone": "+81-80-xxxx-xxxx",
43
+ "company": "Example"
44
+ }
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alpha-netzilla/cardreader. 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.
50
+
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
55
+
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 "cardreader"
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
Binary file
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cardreader/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cardreader"
8
+ spec.version = Cardreader::VERSION
9
+ spec.authors = ["netzilla"]
10
+ spec.email = ["alpha.netzilla@gmail.com"]
11
+
12
+ spec.summary = %q{Detect a business card information}
13
+ spec.description = %q{Detect a business card information using Google Cloud Vison API}
14
+ spec.homepage = "https://github.com/netzilla/cardreader"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/example/card.jpg ADDED
Binary file
data/example/card.png ADDED
Binary file
data/lib/cardreader.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "cardreader/version"
2
+ require "cardreader/manage"
3
+ require "cardreader/lang"
4
+ require "cardreader/vision"
5
+ require "base64"
6
+ require "json"
7
+ require "net/http"
@@ -0,0 +1,33 @@
1
+ module Cardreader
2
+
3
+ class Lang
4
+ def initialize(key)
5
+ @key = key
6
+ end
7
+
8
+ def post(text)
9
+ uri = URI.parse("https://language.googleapis.com/v1beta1/documents:analyzeEntities?key=#{@key}")
10
+
11
+ request = Net::HTTP::Post.new(uri)
12
+ request.content_type = "application/json"
13
+ request.body = ""
14
+ request.body = "{
15
+ 'document':{
16
+ 'type':'PLAIN_TEXT',
17
+ 'content': '#{text}'
18
+ }
19
+ }"
20
+
21
+ req_options = {
22
+ use_ssl: uri.scheme == "https",
23
+ }
24
+
25
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
26
+ http.request(request)
27
+ end
28
+
29
+ JSON.parse(response.body)["entities"]
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,58 @@
1
+ module Cardreader
2
+
3
+ class Manage
4
+ def initialize(key)
5
+ @vision = Vision.new(key)
6
+ @lang = Lang.new(key)
7
+ @results = {person: "", organization: "", location: "",
8
+ mail: "", url: "", phone: ""}
9
+ end
10
+
11
+ def distill(keyword, reg)
12
+ matched = @detected_text.match(reg)
13
+
14
+ if matched
15
+ matched = matched[0]
16
+ @detected_text.gsub!(/#{matched.gsub(/\+/,'')}/, "")
17
+ @results[keyword] = matched
18
+ end
19
+ end
20
+
21
+ def preprocess
22
+ reg= /[^\s]+[\s]*(Corp|Ltd|Inc|Co|株式会社)/
23
+ distill(:company, reg)
24
+
25
+ reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
26
+ distill(:mail, reg)
27
+
28
+ reg = /(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?/
29
+ distill(:url, reg)
30
+
31
+ reg = /(\+[\dO]{2}[\-])?[\dO]{2,5}[\-(][\dO]{1,4}[\-)][\dO]{4}/
32
+ distill(:phone, reg)
33
+ end
34
+
35
+ def detect(image)
36
+ @detected_text = @vision.post(image)
37
+ preprocess
38
+ text = @lang.post(@detected_text)
39
+ make(text)
40
+ end
41
+
42
+ def make(text)
43
+ text.each do |t|
44
+ case t["type"]
45
+ when "PERSON"
46
+ @results[:person] = t["name"]
47
+ when "ORGANIZATION"
48
+ @results[:organization] = " " + t["name"] + @results[:organization]
49
+ when "LOCATION"
50
+ @results[:location] = " " + t["name"] + @results[:location]
51
+ else
52
+ end
53
+ end
54
+ @results.to_json
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ module Cardreader
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,53 @@
1
+ module Cardreader
2
+
3
+ class Vision
4
+ def initialize(key)
5
+ @key = key
6
+ end
7
+
8
+ def post(image)
9
+ uri = "https://vision.googleapis.com/v1/images:annotate?key=#{@key}"
10
+ base64_image = Base64.strict_encode64(File.new(image, 'rb').read)
11
+ body = {
12
+ requests: [{
13
+ image: {
14
+ content: base64_image
15
+ },
16
+ features: [
17
+ {
18
+ type: 'TEXT_DETECTION',
19
+ maxResults: 1
20
+ }
21
+ ]
22
+ }]
23
+ }.to_json
24
+
25
+ uri = URI.parse(uri)
26
+ https = Net::HTTP.new(uri.host, uri.port)
27
+ https.use_ssl = true
28
+ request = Net::HTTP::Post.new(uri.request_uri)
29
+ request["Content-Type"] = "application/json"
30
+ response = https.request(request, body)
31
+
32
+ #text = JSON.parse(response.body)["responses"][0]["fullTextAnnotation"]["text"]
33
+
34
+
35
+ blocks = JSON.parse(response.body)["responses"][0]["fullTextAnnotation"]["pages"][0]["blocks"]
36
+
37
+ page = []
38
+ blocks.each do |block|
39
+ text = ""
40
+ block["paragraphs"][0]["words"].each do |word|
41
+ text += " "
42
+ word["symbols"].each do |symbol|
43
+ text += symbol["text"]
44
+ end
45
+ end
46
+ page << text
47
+ end
48
+ return page.join("\n")
49
+ end
50
+ end
51
+
52
+ end
53
+ #cat vision.json | jq .fullTextAnnotation.pages[0].blocks[4].paragraphs[0].words[5].symbols[0]
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cardreader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - netzilla
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-31 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Detect a business card information using Google Cloud Vison API
56
+ email:
57
+ - alpha.netzilla@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - cardreader-0.1.0.gem
72
+ - cardreader.gemspec
73
+ - example/card.jpg
74
+ - example/card.png
75
+ - lib/cardreader.rb
76
+ - lib/cardreader/lang.rb
77
+ - lib/cardreader/manage.rb
78
+ - lib/cardreader/version.rb
79
+ - lib/cardreader/vision.rb
80
+ homepage: https://github.com/netzilla/cardreader
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.6
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Detect a business card information
104
+ test_files: []