rocrad 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rocrad/binary.rb +20 -0
- data/lib/rocrad/errors.rb +0 -4
- data/lib/rocrad/mixed.rb +2 -3
- data/lib/rocrad.rb +19 -26
- data/rocrad.gemspec +3 -1
- data/test/test_binary.rb +21 -0
- data/test/test_mixed.rb +11 -8
- data/test/test_rocrad.rb +24 -24
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Rocrad
|
2
|
+
class Binary < Rocrad
|
3
|
+
|
4
|
+
def initialize(src="", filename="")
|
5
|
+
@filename=filename
|
6
|
+
super(src)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def build_source(src)
|
12
|
+
src_path = Pathname.new(Dir::tmpdir).join("#{@uid.generate}_#{@filename}")
|
13
|
+
src_file = File.new(src_path.to_s, File::CREAT|File::TRUNC|File::RDWR, 0644)
|
14
|
+
src_file.write(src)
|
15
|
+
src_file.close
|
16
|
+
Pathname.new(src_path)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/rocrad/errors.rb
CHANGED
data/lib/rocrad/mixed.rb
CHANGED
@@ -24,15 +24,14 @@ class Rocrad
|
|
24
24
|
private
|
25
25
|
|
26
26
|
#Convert parts of image to string
|
27
|
-
def
|
27
|
+
def ocr!
|
28
28
|
@txt = ""
|
29
29
|
@areas.each do |area|
|
30
30
|
image = Rocrad.new(@src.to_s)
|
31
31
|
image.crop!(area[:x].to_i, area[:y].to_i, area[:w].to_i, area[:h].to_i)
|
32
32
|
@txt << image.to_s
|
33
33
|
end
|
34
|
-
|
35
|
-
raise Rocrad::ConversionError
|
34
|
+
@txt
|
36
35
|
end
|
37
36
|
|
38
37
|
end
|
data/lib/rocrad.rb
CHANGED
@@ -6,6 +6,7 @@ require "RMagick"
|
|
6
6
|
|
7
7
|
require "rocrad/errors"
|
8
8
|
require "rocrad/mixed"
|
9
|
+
require "rocrad/binary"
|
9
10
|
|
10
11
|
class Rocrad
|
11
12
|
|
@@ -14,33 +15,31 @@ class Rocrad
|
|
14
15
|
|
15
16
|
def initialize(src="")
|
16
17
|
@uid = UUID.new
|
17
|
-
@src =
|
18
|
+
@src = build_source src
|
18
19
|
@txt = ""
|
19
20
|
@tmp = nil
|
20
21
|
end
|
21
22
|
|
22
23
|
def src=(value="")
|
23
24
|
@txt = ""
|
24
|
-
@src =
|
25
|
+
@src = build_source value
|
25
26
|
end
|
26
27
|
|
27
|
-
|
28
|
-
def to_s
|
29
|
-
return @txt if @txt != ""
|
28
|
+
def ocr!
|
30
29
|
if @src.instance_of? Pathname and @src.file?
|
31
|
-
|
30
|
+
ocr_via_path
|
32
31
|
@txt
|
33
32
|
elsif @src.instance_of? URI::HTTP
|
34
|
-
|
33
|
+
ocr_via_http
|
35
34
|
@txt
|
36
35
|
else
|
37
36
|
raise ImageNotSelectedError
|
38
37
|
end
|
39
38
|
end
|
40
39
|
|
41
|
-
#
|
42
|
-
def
|
43
|
-
|
40
|
+
#Output value
|
41
|
+
def to_s
|
42
|
+
@txt != "" ? @txt : ocr!
|
44
43
|
end
|
45
44
|
|
46
45
|
#Crop image to convert
|
@@ -55,23 +54,24 @@ class Rocrad
|
|
55
54
|
|
56
55
|
private
|
57
56
|
|
57
|
+
#Linux console clear
|
58
58
|
def cco
|
59
|
-
|
59
|
+
File.exist?("/dev/null") ? "2>/dev/null" : ""
|
60
60
|
end
|
61
61
|
|
62
|
-
def
|
62
|
+
def ocr_via_http
|
63
63
|
tmp_path = Pathname.new(Dir::tmpdir).join("#{@uid.generate}_#{Pathname.new(@src.request_uri).basename}")
|
64
64
|
tmp_file = File.new(tmp_path.to_s, File::CREAT|File::TRUNC|File::RDWR, 0644)
|
65
65
|
tmp_file.write(Net::HTTP.get(@src))
|
66
66
|
tmp_file.close
|
67
67
|
uri = @src
|
68
68
|
@src = tmp_path
|
69
|
-
|
69
|
+
ocr_via_path
|
70
70
|
@src = uri
|
71
71
|
remove_file([tmp_path])
|
72
72
|
end
|
73
73
|
|
74
|
-
def
|
74
|
+
def build_source(src)
|
75
75
|
case (uri = URI.parse(src)).class.to_s
|
76
76
|
when "URI::HTTP" then
|
77
77
|
uri
|
@@ -91,9 +91,6 @@ class Rocrad
|
|
91
91
|
system "rm -f #{file} #{cco}"
|
92
92
|
end
|
93
93
|
end
|
94
|
-
true
|
95
|
-
rescue
|
96
|
-
raise TempFilesNotRemovedError
|
97
94
|
end
|
98
95
|
|
99
96
|
#Convert image to pnm
|
@@ -116,18 +113,14 @@ class Rocrad
|
|
116
113
|
end
|
117
114
|
|
118
115
|
#Convert image to string
|
119
|
-
def
|
116
|
+
def ocr_via_path
|
120
117
|
src = @tmp ? @tmp : @src
|
121
118
|
txt = Pathname.new(Dir::tmpdir).join("#{@uid.generate}_#{src.sub(src.extname, ".txt").basename}")
|
122
119
|
pnm = image_to_pnm
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
@tmp = nil
|
128
|
-
rescue
|
129
|
-
raise ConversionError
|
130
|
-
end
|
120
|
+
`ocrad #{pnm} -l -F utf8 -o #{txt} #{cco}`
|
121
|
+
@txt = File.read(txt)
|
122
|
+
@tmp ? remove_file([pnm, txt, @tmp]) : remove_file([pnm, txt])
|
123
|
+
@tmp = nil
|
131
124
|
end
|
132
125
|
|
133
126
|
end
|
data/rocrad.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rocrad}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Ludwig Bratke"]
|
@@ -26,6 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"Rakefile",
|
27
27
|
"VERSION",
|
28
28
|
"lib/rocrad.rb",
|
29
|
+
"lib/rocrad/binary.rb",
|
29
30
|
"lib/rocrad/errors.rb",
|
30
31
|
"lib/rocrad/mixed.rb",
|
31
32
|
"rocrad.gemspec",
|
@@ -37,6 +38,7 @@ Gem::Specification.new do |s|
|
|
37
38
|
"test/images/test.png",
|
38
39
|
"test/images/test.tif",
|
39
40
|
"test/images/test1.tif",
|
41
|
+
"test/test_binary.rb",
|
40
42
|
"test/test_mixed.rb",
|
41
43
|
"test/test_rocrad.rb"
|
42
44
|
]
|
data/test/test_binary.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require "helper"
|
3
|
+
rescue LoadError
|
4
|
+
require File.dirname(__FILE__) + '/helper'
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestBinary < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@path = Pathname.new(__FILE__.gsub("test_binary.rb", "")).expand_path
|
11
|
+
@jpg = @path.join("images", "test.jpg").to_s
|
12
|
+
f = File.open(@jpg.to_s, "r")
|
13
|
+
@chars = f.chars.to_a
|
14
|
+
f.close
|
15
|
+
@txt_jpg = ["3", "R", "8", "Z".downcase].*""
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_binary
|
19
|
+
assert_equal @txt_jpg, Rocrad::Binary.new(@chars, "foo.jpg").to_s.gsub(/[ \n\r]/,"")
|
20
|
+
end
|
21
|
+
end
|
data/test/test_mixed.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
|
2
|
-
require
|
1
|
+
begin
|
2
|
+
require "helper"
|
3
|
+
rescue LoadError
|
4
|
+
require File.dirname(__FILE__) + '/helper'
|
5
|
+
end
|
3
6
|
|
4
7
|
class TestMixed < Test::Unit::TestCase
|
5
8
|
|
@@ -21,7 +24,7 @@ class TestMixed < Test::Unit::TestCase
|
|
21
24
|
image.add_area(218, 22, 24, 28) # position of z
|
22
25
|
image.add_area(248, 24, 22, 22) # position of z
|
23
26
|
end
|
24
|
-
assert_equal @txt_tif, mix_block.
|
27
|
+
assert_equal @txt_tif, mix_block.to_s.gsub(/[ \n\r]/, "")
|
25
28
|
|
26
29
|
mix_block = Rocrad::Mixed.new(@tif, {:areas => [
|
27
30
|
{:x => 28, :y=>19, :w=>25, :h=>25}, #position of 4
|
@@ -29,7 +32,7 @@ class TestMixed < Test::Unit::TestCase
|
|
29
32
|
{:x => 218, :y=>22, :w=>24, :h=>28}, # position of z
|
30
33
|
{:x => 248, :y=>24, :w=>22, :h=>22} # position of z
|
31
34
|
]})
|
32
|
-
assert_equal @txt_tif, mix_block.
|
35
|
+
assert_equal @txt_tif, mix_block.to_s.gsub(/[ \n\r]/, "")
|
33
36
|
end
|
34
37
|
|
35
38
|
def test_show_areas
|
@@ -76,15 +79,15 @@ class TestMixed < Test::Unit::TestCase
|
|
76
79
|
{:x=>218, :h=>28, :w=>24, :y=>22},
|
77
80
|
{:x=>248, :h=>22, :w=>22, :y=>24}]
|
78
81
|
assert_equal(areas, mix_block.areas)
|
79
|
-
assert_equal @txt_tif[1..3], mix_block.
|
82
|
+
assert_equal @txt_tif[1..3], mix_block.to_s.gsub(/[ \n\r]/, "")
|
80
83
|
|
81
84
|
mix_block.areas = []
|
82
85
|
assert_equal([], mix_block.areas)
|
83
|
-
assert_equal "", mix_block.
|
86
|
+
assert_equal "", mix_block.to_s.gsub(/[ \n\r]/, "")
|
84
87
|
|
85
88
|
mix_block.areas = nil
|
86
|
-
|
87
|
-
assert_equal "", mix_block.
|
89
|
+
assert_equal([], mix_block.areas)
|
90
|
+
assert_equal "", mix_block.to_s.gsub(/[ \n\r]/, "")
|
88
91
|
end
|
89
92
|
|
90
93
|
end
|
data/test/test_rocrad.rb
CHANGED
@@ -12,20 +12,20 @@ class TestRocrad < Test::Unit::TestCase
|
|
12
12
|
f = File.open(@jpg.to_s, "r")
|
13
13
|
@chars = f.chars.to_a
|
14
14
|
f.close
|
15
|
-
@txt_jpg = ["3", "R", "8", "Z".downcase]
|
15
|
+
@txt_jpg = ["3", "R", "8", "Z".downcase].*""
|
16
16
|
@txt_tif = "43ZZ".downcase
|
17
|
-
@txt_png = ["H", "W", "9", "W".downcase]
|
18
|
-
@txt_bmp = ["Z".downcase, "L", "A", "6"]
|
17
|
+
@txt_png = ["H", "W", "9", "W".downcase].*""
|
18
|
+
@txt_bmp = ["Z".downcase, "L", "A", "6"].*""
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_convert_via_http
|
22
22
|
Net::HTTP.expects(:get).returns(@chars)
|
23
|
-
assert_equal @txt_jpg, Rocrad.new("http://localhost:3000/uploads/picture/data/4dd21bfd828bf81bdd00000d/nzp_img_17_4_2011_8_55_29.jpg").
|
23
|
+
assert_equal @txt_jpg, Rocrad.new("http://localhost:3000/uploads/picture/data/4dd21bfd828bf81bdd00000d/nzp_img_17_4_2011_8_55_29.jpg").to_s.gsub(/[ \n\r]/, "")
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_convert_via_http_raise_exception
|
27
27
|
assert_raise Errno::ECONNREFUSED do
|
28
|
-
Rocrad.new("http://localhost:3000/uploads/picture/data/4dd21bfd828bf81bdd00000d/nzp_img_17_4_2011_8_55_29.jpg").
|
28
|
+
Rocrad.new("http://localhost:3000/uploads/picture/data/4dd21bfd828bf81bdd00000d/nzp_img_17_4_2011_8_55_29.jpg").to_s.gsub(/[ \n\r]/, "")
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
@@ -36,40 +36,40 @@ class TestRocrad < Test::Unit::TestCase
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_translate_image_to_text
|
39
|
-
assert_equal @txt_jpg, Rocrad.new(@jpg).
|
40
|
-
assert_equal @txt_tif, Rocrad.new(@path.join("images", "test.tif").to_s).
|
39
|
+
assert_equal @txt_jpg, Rocrad.new(@jpg).to_s.gsub(/[ \n\r]/, "")
|
40
|
+
assert_equal @txt_tif, Rocrad.new(@path.join("images", "test.tif").to_s).to_s.gsub(/[ \n\r]/, "")
|
41
41
|
end
|
42
42
|
|
43
43
|
def test_unsupported_file_type_error
|
44
44
|
assert_raise Rocrad::UnsupportedFileTypeError do
|
45
|
-
Rocrad.new(@path.join("images", "test.foo").to_s).
|
45
|
+
Rocrad.new(@path.join("images", "test.foo").to_s).to_s.gsub(/[ \n\r]/, "")
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_image_not_selected_error
|
50
50
|
assert_raise Rocrad::ImageNotSelectedError do
|
51
|
-
Rocrad.new(@path.join("images", "test.noo").to_s).
|
51
|
+
Rocrad.new(@path.join("images", "test.noo").to_s).to_s.gsub(/[ \n\r]/, "")
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
def test_translate_images_png_jpg
|
56
|
-
assert_equal @txt_png, Rocrad.new(@path.join("images", "test.png").to_s).
|
57
|
-
assert_equal @txt_jpg, Rocrad.new(@path.join("images", "test.jpg").to_s).
|
56
|
+
assert_equal @txt_png, Rocrad.new(@path.join("images", "test.png").to_s).to_s.gsub(/[ \n\r]/, "")
|
57
|
+
assert_equal @txt_jpg, Rocrad.new(@path.join("images", "test.jpg").to_s).to_s.gsub(/[ \n\r]/, "")
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_translate_images_bmp
|
61
|
-
assert_equal @txt_bmp, Rocrad.new(@path.join("images", "test.bmp").to_s).
|
61
|
+
assert_equal @txt_bmp, Rocrad.new(@path.join("images", "test.bmp").to_s).to_s.gsub(/[ \n\r]/, "")
|
62
62
|
end
|
63
63
|
|
64
64
|
def test_translate_test1_tif
|
65
|
-
assert_equal "V2V4".downcase, Rocrad.new(@path.join("images", "test1.tif").to_s).
|
65
|
+
assert_equal "V2V4".downcase, Rocrad.new(@path.join("images", "test1.tif").to_s).to_s.gsub(/[ \n\r]/, "")
|
66
66
|
end
|
67
67
|
|
68
68
|
def test_change_the_image
|
69
69
|
image = Rocrad.new(@jpg)
|
70
|
-
assert_equal @txt_jpg, image.
|
70
|
+
assert_equal @txt_jpg, image.to_s.gsub(/[ \n\r]/, "")
|
71
71
|
image.src = @path.join("images", "test.tif").to_s
|
72
|
-
assert_equal @txt_tif, image.
|
72
|
+
assert_equal @txt_tif, image.to_s.gsub(/[ \n\r]/, "")
|
73
73
|
end
|
74
74
|
|
75
75
|
def test_unique_uid
|
@@ -79,19 +79,19 @@ class TestRocrad < Test::Unit::TestCase
|
|
79
79
|
|
80
80
|
def test_should_crop_image_tif
|
81
81
|
tif = @path.join("images", "test.tif").to_s
|
82
|
-
assert_equal "4", Rocrad.new(tif).crop!(140, 10, 36, 40).
|
83
|
-
assert_equal "3", Rocrad.new(tif).crop!(180, 10, 36, 40).
|
84
|
-
assert_equal "Z".downcase, Rocrad.new(tif).crop!(200, 10, 36, 40).
|
85
|
-
assert_equal "Z".downcase, Rocrad.new(tif).crop!(220, 10, 30, 40).
|
82
|
+
assert_equal "4", Rocrad.new(tif).crop!(140, 10, 36, 40).to_s.gsub(/[ \n\r]/, "")
|
83
|
+
assert_equal "3", Rocrad.new(tif).crop!(180, 10, 36, 40).to_s.gsub(/[ \n\r]/, "")
|
84
|
+
assert_equal "Z".downcase, Rocrad.new(tif).crop!(200, 10, 36, 40).to_s.gsub(/[ \n\r]/, "")
|
85
|
+
assert_equal "Z".downcase, Rocrad.new(tif).crop!(220, 10, 30, 40).to_s.gsub(/[ \n\r]/, "")
|
86
86
|
end
|
87
87
|
|
88
88
|
def test_should_crop_image_tif_same_instance
|
89
89
|
tif = @path.join("images", "test.tif").to_s
|
90
90
|
instance = Rocrad.new(tif)
|
91
|
-
assert_equal "4", instance.crop!(140, 10, 36, 40).
|
92
|
-
assert_equal "3", instance.crop!(180, 10, 36, 40).
|
93
|
-
assert_equal "Z".downcase, instance.crop!(200, 10, 36, 40).
|
94
|
-
assert_equal "Z".downcase, instance.crop!(220, 10, 30, 40).
|
91
|
+
assert_equal "4", instance.crop!(140, 10, 36, 40).to_s.gsub(/[ \n\r]/, "")
|
92
|
+
assert_equal "3", instance.crop!(180, 10, 36, 40).to_s.gsub(/[ \n\r]/, "")
|
93
|
+
assert_equal "Z".downcase, instance.crop!(200, 10, 36, 40).to_s.gsub(/[ \n\r]/, "")
|
94
|
+
assert_equal "Z".downcase, instance.crop!(220, 10, 30, 40).to_s.gsub(/[ \n\r]/, "")
|
95
95
|
end
|
96
96
|
|
97
97
|
def test_attr_reader_while_cropping
|
@@ -99,7 +99,7 @@ class TestRocrad < Test::Unit::TestCase
|
|
99
99
|
rocrad = Rocrad.new(tif).crop!(140, 10, 36, 40)
|
100
100
|
assert_equal "", rocrad.txt
|
101
101
|
assert rocrad.tmp.instance_of? Pathname
|
102
|
-
assert_equal "4", rocrad.
|
102
|
+
assert_equal "4", rocrad.to_s.gsub(/[ \n\r]/, "")
|
103
103
|
assert rocrad.txt.include?("4")
|
104
104
|
assert_nil rocrad.tmp
|
105
105
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rocrad
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 4
|
10
|
+
version: 0.1.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ludwig Bratke
|
@@ -165,6 +165,7 @@ files:
|
|
165
165
|
- Rakefile
|
166
166
|
- VERSION
|
167
167
|
- lib/rocrad.rb
|
168
|
+
- lib/rocrad/binary.rb
|
168
169
|
- lib/rocrad/errors.rb
|
169
170
|
- lib/rocrad/mixed.rb
|
170
171
|
- rocrad.gemspec
|
@@ -176,6 +177,7 @@ files:
|
|
176
177
|
- test/images/test.png
|
177
178
|
- test/images/test.tif
|
178
179
|
- test/images/test1.tif
|
180
|
+
- test/test_binary.rb
|
179
181
|
- test/test_mixed.rb
|
180
182
|
- test/test_rocrad.rb
|
181
183
|
has_rdoc: true
|