rtesseract 2.1.0 → 3.0.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.
Files changed (53) hide show
  1. checksums.yaml +5 -5
  2. data/.document +1 -2
  3. data/.gitignore +12 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +13 -11
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -16
  8. data/Gemfile.lock +42 -82
  9. data/LICENSE.txt +18 -17
  10. data/README.md +137 -0
  11. data/Rakefile +4 -48
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/lib/rtesseract/box.rb +15 -60
  15. data/lib/rtesseract/check.rb +14 -0
  16. data/lib/rtesseract/command.rb +41 -0
  17. data/lib/rtesseract/configuration.rb +15 -64
  18. data/lib/rtesseract/pdf.rb +18 -0
  19. data/lib/rtesseract/text.rb +9 -0
  20. data/lib/rtesseract/tsv.rb +18 -0
  21. data/lib/rtesseract/version.rb +3 -0
  22. data/lib/rtesseract.rb +22 -214
  23. data/rtesseract.gemspec +27 -93
  24. metadata +38 -71
  25. data/README.rdoc +0 -156
  26. data/VERSION +0 -1
  27. data/lib/processors/mini_magick.rb +0 -43
  28. data/lib/processors/none.rb +0 -34
  29. data/lib/processors/rmagick.rb +0 -46
  30. data/lib/rtesseract/blob.rb +0 -34
  31. data/lib/rtesseract/box_char.rb +0 -31
  32. data/lib/rtesseract/errors.rb +0 -21
  33. data/lib/rtesseract/mixed.rb +0 -54
  34. data/lib/rtesseract/processor.rb +0 -19
  35. data/lib/rtesseract/utils.rb +0 -44
  36. data/spec/configs/eng.user-words.txt +0 -13
  37. data/spec/images/README.pdf +0 -0
  38. data/spec/images/blank.tif +0 -0
  39. data/spec/images/mixed.tif +0 -0
  40. data/spec/images/orientation_reverse.png +0 -0
  41. data/spec/images/test with spaces.tif +0 -0
  42. data/spec/images/test-pdf.png +0 -0
  43. data/spec/images/test.bmp +0 -0
  44. data/spec/images/test.jpg +0 -0
  45. data/spec/images/test.png +0 -0
  46. data/spec/images/test.tif +0 -0
  47. data/spec/images/test1.tif +0 -0
  48. data/spec/images/test_words.png +0 -0
  49. data/spec/rtesseract_box_char_spec.rb +0 -82
  50. data/spec/rtesseract_box_spec.rb +0 -36
  51. data/spec/rtesseract_mixed_spec.rb +0 -49
  52. data/spec/rtesseract_spec.rb +0 -274
  53. data/spec/spec_helper.rb +0 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: db992168bb87c6c4f3124403f9417c4cd46aca3e
4
- data.tar.gz: b329a5ebc7316f28f63bcd7d84aa575cb661b3b9
2
+ SHA256:
3
+ metadata.gz: 00e35fcd674159d47c7252be586ba214c42f374d1e671cab2ae8a47672237d92
4
+ data.tar.gz: e6601b67346092513af4399c49bb161b99afccb84f277395fe8f27417d2f61da
5
5
  SHA512:
6
- metadata.gz: a74dc8fd03a678ecdff1425bc188173615da59b636cbe9ceb072353a1e4d3f2aa076b9787007ef9bfebb9c928e2a170d8c0149f864741c888c6d74ea04a78889
7
- data.tar.gz: 97d3bfea1ae54841ce68427893e8e69fa358a3018ba80b8f06d23759abe0d89c81e481c5775699b3c0e98234fb39dfb898a42b833c48b8ac42128c7c7c292b88
6
+ metadata.gz: 92713fe105ad0b96ca28fc61f530c4dcfb8a6905aeb9d7206dfb951afab7f482e10b1ffc397f5d2e8cf86f9bd0a69be65e8da791ba0dd32655c29341bf0a67af
7
+ data.tar.gz: 4601e51dc96489292cfd0b54653f149bfcde9a830b3c3071ed2bc55778ab393405c267172ea06aa1e2ad3f64c615b845ba0aaf9a5317288555ee50e913bc196f
data/.document CHANGED
@@ -1,5 +1,4 @@
1
1
  lib/**/*.rb
2
2
  bin/*
3
- -
4
- features/**/*.feature
3
+ -
5
4
  LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /coverage.data
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec CHANGED
@@ -1 +1,4 @@
1
+ --format documentation
1
2
  --color
3
+ --order rand
4
+ --require spec_helper
data/.travis.yml CHANGED
@@ -1,14 +1,16 @@
1
- sudo: required
2
- dist: trusty
1
+ ---
2
+
3
+ sudo: false
4
+ dist: bionic
3
5
  language: ruby
4
- addons:
5
- apt:
6
- packages:
7
- - tesseract-ocr
6
+ cache: bundler
7
+
8
+ before_install:
9
+ - sudo add-apt-repository ppa:alex-p/tesseract-ocr -y
10
+ - sudo apt-get update -q
11
+ - sudo apt-get install tesseract-ocr tesseract-ocr-eng ghostscript -y
12
+ - gem install bundler -v 1.17.1
8
13
 
9
14
  rvm:
10
- - 1.9.3
11
- - 2.0.0
12
- - 2.1.5
13
- - 2.2.2
14
- - 2.3.0
15
+ - 2.4.5
16
+ - 2.5.3
@@ -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 daniloj.dasilva@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 [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 CHANGED
@@ -1,18 +1,6 @@
1
- source 'http://rubygems.org'
2
- # Add dependencies to develop your gem here.
3
- # Include everything needed to run rake, tests, features, etc.
4
- gem 'nokogiri'
1
+ source "https://rubygems.org"
5
2
 
6
- group :development do
7
- gem 'rspec'
8
- gem 'rdoc'
9
- gem 'bundler'
10
- gem 'jeweler', '~> 2.1.1'
11
- gem 'simplecov'
12
- gem 'coveralls', require: false
13
- end
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
14
4
 
15
- group :test do
16
- gem 'rmagick'
17
- gem 'mini_magick'
18
- end
5
+ # Specify your gem's dependencies in rtesseract.gemspec
6
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,98 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rtesseract (3.0.0)
5
+ nokogiri
6
+
1
7
  GEM
2
- remote: http://rubygems.org/
8
+ remote: https://rubygems.org/
3
9
  specs:
4
- addressable (2.4.0)
5
- builder (3.2.2)
6
- coveralls (0.8.13)
7
- json (~> 1.8)
8
- simplecov (~> 0.11.0)
10
+ coveralls (0.8.22)
11
+ json (>= 1.8, < 3)
12
+ simplecov (~> 0.16.1)
9
13
  term-ansicolor (~> 1.3)
10
- thor (~> 0.19.1)
11
- tins (~> 1.6.0)
12
- descendants_tracker (0.0.4)
13
- thread_safe (~> 0.3, >= 0.3.1)
14
- diff-lcs (1.2.5)
15
- docile (1.1.5)
16
- faraday (0.9.2)
17
- multipart-post (>= 1.2, < 3)
18
- git (1.3.0)
19
- github_api (0.13.1)
20
- addressable (~> 2.4.0)
21
- descendants_tracker (~> 0.0.4)
22
- faraday (~> 0.8, < 0.10)
23
- hashie (>= 3.4)
24
- multi_json (>= 1.7.5, < 2.0)
25
- oauth2
26
- hashie (3.4.3)
27
- highline (1.7.8)
28
- jeweler (2.1.1)
29
- builder
30
- bundler (>= 1.0)
31
- git (>= 1.2.5)
32
- github_api
33
- highline (>= 1.6.15)
34
- nokogiri (>= 1.5.10)
35
- rake
36
- rdoc
37
- semver
38
- json (1.8.3)
39
- jwt (1.5.1)
40
- mini_magick (4.5.1)
41
- mini_portile2 (2.0.0)
42
- multi_json (1.11.2)
43
- multi_xml (0.5.5)
44
- multipart-post (2.0.0)
45
- nokogiri (1.6.7.2)
46
- mini_portile2 (~> 2.0.0.rc2)
47
- oauth2 (1.1.0)
48
- faraday (>= 0.8, < 0.10)
49
- jwt (~> 1.0, < 1.5.2)
50
- multi_json (~> 1.3)
51
- multi_xml (~> 0.5)
52
- rack (>= 1.2, < 3)
53
- rack (1.6.4)
54
- rake (11.1.2)
55
- rdoc (4.2.2)
56
- json (~> 1.4)
57
- rmagick (2.15.4)
58
- rspec (3.4.0)
59
- rspec-core (~> 3.4.0)
60
- rspec-expectations (~> 3.4.0)
61
- rspec-mocks (~> 3.4.0)
62
- rspec-core (3.4.4)
63
- rspec-support (~> 3.4.0)
64
- rspec-expectations (3.4.0)
14
+ thor (~> 0.19.4)
15
+ tins (~> 1.6)
16
+ diff-lcs (1.3)
17
+ docile (1.3.1)
18
+ json (2.1.0)
19
+ mini_portile2 (2.4.0)
20
+ nokogiri (1.9.1)
21
+ mini_portile2 (~> 2.4.0)
22
+ rake (10.5.0)
23
+ rspec (3.8.0)
24
+ rspec-core (~> 3.8.0)
25
+ rspec-expectations (~> 3.8.0)
26
+ rspec-mocks (~> 3.8.0)
27
+ rspec-core (3.8.0)
28
+ rspec-support (~> 3.8.0)
29
+ rspec-expectations (3.8.2)
65
30
  diff-lcs (>= 1.2.0, < 2.0)
66
- rspec-support (~> 3.4.0)
67
- rspec-mocks (3.4.1)
31
+ rspec-support (~> 3.8.0)
32
+ rspec-mocks (3.8.0)
68
33
  diff-lcs (>= 1.2.0, < 2.0)
69
- rspec-support (~> 3.4.0)
70
- rspec-support (3.4.1)
71
- semver (1.0.1)
72
- simplecov (0.11.2)
73
- docile (~> 1.1.0)
74
- json (~> 1.8)
34
+ rspec-support (~> 3.8.0)
35
+ rspec-support (3.8.0)
36
+ simplecov (0.16.1)
37
+ docile (~> 1.1)
38
+ json (>= 1.8, < 3)
75
39
  simplecov-html (~> 0.10.0)
76
- simplecov-html (0.10.0)
77
- term-ansicolor (1.3.2)
40
+ simplecov-html (0.10.2)
41
+ term-ansicolor (1.7.0)
78
42
  tins (~> 1.0)
79
- thor (0.19.1)
80
- thread_safe (0.3.5)
81
- tins (1.6.0)
43
+ thor (0.19.4)
44
+ tins (1.20.2)
82
45
 
83
46
  PLATFORMS
84
47
  ruby
85
48
 
86
49
  DEPENDENCIES
87
- bundler
50
+ bundler (~> 1.17)
88
51
  coveralls
89
- jeweler (~> 2.1.1)
90
- mini_magick
91
- nokogiri
92
- rdoc
93
- rmagick
94
- rspec
52
+ rake (~> 10.0)
53
+ rspec (~> 3.0)
54
+ rtesseract!
95
55
  simplecov
96
56
 
97
57
  BUNDLED WITH
98
- 1.11.2
58
+ 1.17.2
data/LICENSE.txt CHANGED
@@ -1,20 +1,21 @@
1
- Copyright (c) 2014 Danilo Jeremias da Silva
1
+ The MIT License (MIT)
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
3
+ Copyright (c) 2018 Danilo Jeremias da Silva
10
4
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
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:
13
11
 
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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,137 @@
1
+ # RTesseract
2
+
3
+ <a href='http://badge.fury.io/rb/rtesseract'>
4
+ <img src="https://badge.fury.io/rb/rtesseract.png" alt="Gem Version" />
5
+ </a>
6
+ <a href='https://travis-ci.org/dannnylo/rtesseract'>
7
+ <img src="https://travis-ci.org/dannnylo/rtesseract.png?branch=master" alt="Build Status" />
8
+ </a>
9
+ <a href='https://coveralls.io/r/dannnylo/rtesseract?branch=master'>
10
+ <img src="https://coveralls.io/repos/dannnylo/rtesseract/badge.png?branch=master" alt="Coverage Status" />
11
+ </a>
12
+ <a href='https://codeclimate.com/github/dannnylo/rtesseract'>
13
+ <img src="https://codeclimate.com/github/dannnylo/rtesseract.png" />
14
+ </a>
15
+
16
+ Ruby library for working with the Tesseract OCR.
17
+
18
+ ## Installation
19
+
20
+ Check if tesseract ocr programs is installed:
21
+
22
+ $ tesseract --version
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'rtesseract'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install rtesseract
37
+
38
+ ## Usage
39
+
40
+ It's very simple to use rtesseract.
41
+
42
+ ### Convert image to string
43
+
44
+ ```ruby
45
+ image = RTesseract.new("my_image.jpg")
46
+ image.to_s # Getting the value
47
+ ```
48
+
49
+ ### Convert image to searchable PDF
50
+
51
+ ```ruby
52
+ image = RTesseract.new("my_image.jpg")
53
+ image.to_pdf # Getting open file of pdf
54
+ ```
55
+
56
+ ### Convert image to TSV
57
+
58
+ ```ruby
59
+ image = RTesseract.new("my_image.jpg")
60
+ image.to_tsv # Getting open file of pdf
61
+ ```
62
+
63
+ This will preserve the image colors, pictures and structure in the generated pdf.
64
+
65
+ ## Options
66
+
67
+ ### Language
68
+
69
+ ```ruby
70
+ RTesseract.new('test.jpg', lang: 'deu')
71
+ ```
72
+
73
+ * eng - English
74
+ * deu - German
75
+ * deu-f - German fraktur
76
+ * fra - French
77
+ * ita - Italian
78
+ * nld - Dutch
79
+ * por - Portuguese
80
+ * spa - Spanish
81
+ * vie - Vietnamese
82
+ * or any other supported by tesseract.
83
+
84
+ Note: Make sure you have installed the language to tesseract
85
+
86
+ ### Other options
87
+
88
+ ```ruby
89
+ RTesseract.new('test.jpg', config_file: :digits) # Only digit recognition
90
+ ```
91
+
92
+ OR
93
+
94
+ ```ruby
95
+ RTesseract.new('test.jpg', config_file: 'digits quiet')
96
+ ```
97
+
98
+ ### BOUNDING BOX: TO GET WORDS WITH THEIR POSITIONS
99
+ ```ruby
100
+ RTesseract.new('test_words.png').to_box
101
+ ```
102
+
103
+ # => [
104
+ # {:word => 'If', :x_start=>52, :y_start=>13, :x_end=>63, :y_end=>27},
105
+ # {:word => 'you', :x_start=>69, :y_start=>17, :x_end=>100, :y_end=>31},
106
+ # {:word => 'are', :x_start=>108, :y_start=>17, :x_end=>136, :y_end=>27},
107
+ # {:word => 'a', :x_start=>143, :y_start=>17, :x_end=>151, :y_end=>27},
108
+ # {:word => 'friend,', :x_start=>158, :y_start=>13, :x_end=>214, :y_end=>29},
109
+ # {:word => 'you', :x_start=>51, :y_start=>39, :x_end=>82, :y_end=>53},
110
+ # {:word => 'speak', :x_start=>90, :y_start=>35, :x_end=>140, :y_end=>53},
111
+ # {:word => 'the', :x_start=>146, :y_start=>35, :x_end=>174, :y_end=>49},
112
+ # {:word => 'password,', :x_start=>182, :y_start=>35, :x_end=>267, :y_end=>53},
113
+ # {:word => 'and', :x_start=>51, :y_start=>57, :x_end=>81, :y_end=>71},
114
+ # {:word => 'the', :x_start=>89, :y_start=>57, :x_end=>117, :y_end=>71},
115
+ # {:word => 'doors', :x_start=>124, :y_start=>57, :x_end=>172, :y_end=>71},
116
+ # {:word => 'will', :x_start=>180, :y_start=>57, :x_end=>208, :y_end=>71},
117
+ # {:word => 'open.', :x_start=>216, :y_start=>61, :x_end=>263, :y_end=>75}
118
+ # ]
119
+
120
+
121
+ ## Development
122
+
123
+ 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.
124
+
125
+ 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).
126
+
127
+ ## Contributing
128
+
129
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dannnylo/rtesseract. 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.
130
+
131
+ ## License
132
+
133
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
134
+
135
+ ## Code of Conduct
136
+
137
+ Everyone interacting in the Rtesseract project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dannnylo/rtesseract/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,50 +1,6 @@
1
- # encoding: utf-8
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
3
- require 'rubygems'
4
- require 'bundler'
5
- begin
6
- Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts 'Run `bundle install` to install missing gems'
10
- exit e.status_code
11
- end
12
- require 'rake'
4
+ RSpec::Core::RakeTask.new(:spec)
13
5
 
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
- gem.name = 'rtesseract'
18
- gem.homepage = 'http://github.com/dannnylo/rtesseract'
19
- gem.license = 'MIT'
20
- gem.summary = 'Ruby library for working with the Tesseract OCR.'
21
- gem.description = 'Ruby library for working with the Tesseract OCR.'
22
- gem.email = 'dannnylo@gmail.com'
23
- gem.authors = ['Danilo Jeremias da Silva']
24
- # dependencies defined in Gemfile
25
- end
26
- Jeweler::RubygemsDotOrgTasks.new
27
-
28
- require 'rspec/core'
29
- require 'rspec/core/rake_task'
30
- RSpec::Core::RakeTask.new(:spec) do |spec|
31
- spec.pattern = FileList['spec/**/*_spec.rb']
32
- end
33
-
34
- desc 'Code coverage detail'
35
- task :simplecov do
36
- ENV['COVERAGE'] = 'true'
37
- Rake::Task['spec'].execute
38
- end
39
-
40
- task default: :spec
41
-
42
- require 'rdoc/task'
43
- Rake::RDocTask.new do |rdoc|
44
- version = File.exist?('VERSION') ? File.read('VERSION') : ''
45
-
46
- rdoc.rdoc_dir = 'rdoc'
47
- rdoc.title = "rtesseract #{version}"
48
- rdoc.rdoc_files.include('README*')
49
- rdoc.rdoc_files.include('lib/**/*.rb')
50
- end
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rtesseract"
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
@@ -1,73 +1,28 @@
1
- # encoding: UTF-8
2
1
  require 'nokogiri'
3
- require 'fileutils'
2
+ require 'tmpdir'
4
3
 
5
- # RTesseract
6
4
  class RTesseract
7
- # Class to read char positions from an image
8
- class Box < RTesseract
9
- # Setting value as blank array
10
- def initialize_hook
11
- @value = []
5
+ module Box
6
+ def self.temp_dir
7
+ @file_path = Pathname.new(Dir.tmpdir)
12
8
  end
13
9
 
14
- # Aditional options to config file
15
- def config_hook
16
- @options['tessedit_create_hocr'] = 1 # Split Words configuration
17
- end
18
-
19
- # Words converted
20
- def words
21
- convert if @value == []
22
- @value
23
- end
24
-
25
- # Extension of file
26
- def file_ext
27
- '.hocr'
28
- end
29
-
30
- # Read the result file
31
- def parse_file
32
- html = Nokogiri::HTML(File.read(file_with_ext))
33
- html.css('span.ocrx_word, span.ocr_word')
34
- end
35
-
36
- # Return words to value
37
- def convert_text
38
- text_objects = []
39
- parse_file.each { |word| text_objects << BoxParser.new(word).to_h }
40
- @value = text_objects
41
- end
10
+ def self.run(source, options)
11
+ name = "rtesseract_#{SecureRandom.uuid}"
12
+ options.tessedit_create_hocr = 1
42
13
 
43
- # Move file html to hocr
44
- def after_convert_hook
45
- FileUtils.mv(file_with_ext('.html'), file_with_ext) rescue nil
46
- end
14
+ RTesseract::Command.new(source, temp_dir.join(name).to_s, options).run
47
15
 
48
- # Output value
49
- def to_s
50
- return @value.map { |word| word[:word] } if @value != []
51
- if @processor.image?(@source) || @source.file?
52
- convert
53
- @value.map { |word| word[:word] }.join(' ')
54
- else
55
- fail RTesseract::ImageNotSelectedError.new(@source)
56
- end
16
+ parse(temp_dir.join("#{name}.hocr").read)
57
17
  end
58
18
 
59
- # Parse word data from html.
60
- class BoxParser
61
- def initialize(word_html)
62
- @word = word_html
63
- title = @word.attributes['title'].value.to_s
64
- @attributes = title.gsub(';', '').split(' ')
65
- end
19
+ def self.parse(content)
20
+ html = Nokogiri::HTML(content)
21
+ html.css('span.ocrx_word, span.ocr_word').map do |word|
22
+ @attributes = word.attributes['title'].value.to_s.gsub(';', '').split(' ')
66
23
 
67
- # Hash of word and position
68
- def to_h
69
24
  {
70
- word: @word.text,
25
+ word: word.text,
71
26
  x_start: @attributes[1].to_i,
72
27
  y_start: @attributes[2].to_i,
73
28
  x_end: @attributes[3].to_i,
@@ -76,4 +31,4 @@ class RTesseract
76
31
  end
77
32
  end
78
33
  end
79
- end
34
+ end
@@ -0,0 +1,14 @@
1
+
2
+ class RTesseract
3
+ class << self
4
+ def tesseract_version
5
+ Open3.capture2e(RTesseract.config.command, "--version").first.to_s.match(/\d+.\d+/)[0].to_f
6
+ rescue Errno::ENOENT
7
+ 0
8
+ end
9
+
10
+ def check_version!
11
+ raise RTesseract::Error.new('Tesseract OCR 3.5 or later not installed') if RTesseract.tesseract_version < 3.05
12
+ end
13
+ end
14
+ end