ruby_marks 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ruby_marks/config.rb +2 -1
- data/lib/ruby_marks/mark.rb +6 -1
- data/lib/ruby_marks/recognizer.rb +143 -96
- data/lib/ruby_marks/version.rb +1 -1
- data/test/ruby_marks/recognizer_grid_test.rb +22 -20
- data/test/ruby_marks/recognizer_test.rb +3 -2
- metadata +1 -1
data/lib/ruby_marks/config.rb
CHANGED
@@ -7,7 +7,8 @@ module RubyMarks
|
|
7
7
|
:default_mark_width, :default_mark_height, :scan_timeout,
|
8
8
|
:default_mark_width_tolerance, :default_mark_height_tolerance,
|
9
9
|
:default_distance_between_marks, :default_expected_lines,
|
10
|
-
:default_block_width_tolerance, :default_block_height_tolerance,
|
10
|
+
:default_block_width_tolerance, :default_block_height_tolerance,
|
11
|
+
:scan_mode, :auto_ajust_block_width, :auto_ajust_block_height
|
11
12
|
|
12
13
|
|
13
14
|
def initialize(recognizer)
|
data/lib/ruby_marks/mark.rb
CHANGED
@@ -11,6 +11,12 @@ module RubyMarks
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def marked?
|
14
|
+
if @image_str
|
15
|
+
return intensity >= @group.recognizer.config.intensity_percentual
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def intensity
|
14
20
|
if @image_str
|
15
21
|
colors = []
|
16
22
|
|
@@ -21,7 +27,6 @@ module RubyMarks
|
|
21
27
|
end
|
22
28
|
|
23
29
|
intensity = colors.count(".") * 100 / colors.size
|
24
|
-
return intensity >= @group.recognizer.config.intensity_percentual
|
25
30
|
end
|
26
31
|
end
|
27
32
|
|
@@ -140,70 +140,88 @@ module RubyMarks
|
|
140
140
|
marks_blocks = find_marks_grid(group)
|
141
141
|
else
|
142
142
|
marks_blocks = find_marks(original_file_str, group)
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
143
|
+
marks_blocks.sort!{ |a,b| a[:y1] <=> b[:y1] }
|
144
|
+
mark_ant = nil
|
145
|
+
marks_blocks.each do |mark|
|
146
|
+
mark_width = RubyMarks::ImageUtils.calc_width(mark[:x1], mark[:x2])
|
147
|
+
mark_height = RubyMarks::ImageUtils.calc_height(mark[:y1], mark[:y2])
|
148
|
+
|
149
|
+
if mark_width >= group.mark_width_with_down_tolerance &&
|
150
|
+
mark_height >= group.mark_height_with_down_tolerance
|
151
|
+
|
152
|
+
mark_positions = mark[:y1]-10..mark[:y1]+10
|
153
|
+
line += 1 unless mark_ant && mark_positions.include?(mark_ant[:y1])
|
154
|
+
mark[:line] = line
|
155
|
+
mark_ant = mark
|
156
|
+
end
|
157
157
|
end
|
158
|
-
end
|
159
158
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
159
|
+
marks_blocks.delete_if { |m| m[:line].nil? }
|
160
|
+
marks_blocks.sort_by!{ |a| [a[:line], a[:x1]] }
|
161
|
+
|
162
|
+
mark_ant = nil
|
163
|
+
marks_blocks.each do |mark|
|
164
|
+
if mark_ant && mark_ant[:line] == mark[:line]
|
165
|
+
mark_ant_center = RubyMarks::ImageUtils.image_center(mark_ant)
|
166
|
+
mark_center = RubyMarks::ImageUtils.image_center(mark)
|
167
|
+
if (mark_ant_center[:x] - mark_center[:x]).abs < 10
|
168
|
+
mark[:conflict] = true
|
169
|
+
mark[:conflicting_mark] = mark_ant
|
170
|
+
else
|
171
|
+
mark_ant = mark
|
172
|
+
end
|
171
173
|
else
|
172
174
|
mark_ant = mark
|
173
175
|
end
|
174
|
-
else
|
175
|
-
mark_ant = mark
|
176
176
|
end
|
177
|
-
|
178
|
-
marks_blocks.delete_if { |m| m[:conflict] }
|
179
|
-
|
180
|
-
first_position = 0
|
181
|
-
elements_position_count = 0
|
182
|
-
marks_blocks.map { |m| m[:line] }.each do |line|
|
183
|
-
marks = marks_blocks.select { |m| m[:line] == line }
|
184
|
-
if marks.count == group.marks_options.count
|
185
|
-
first_position += marks.first[:x1]
|
186
|
-
elements_position_count += 1
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
if elements_position_count > 0
|
191
|
-
first_position = first_position / elements_position_count
|
192
|
-
distance = group.distance_between_marks * (group.marks_options.count - 1)
|
193
|
-
last_position = first_position + distance
|
194
|
-
marks_blocks.delete_if { |mark| mark[:x1] < first_position - 10 ||
|
195
|
-
mark[:x1] > last_position + 10 }
|
177
|
+
marks_blocks.delete_if { |m| m[:conflict] }
|
196
178
|
|
179
|
+
first_position = 0
|
180
|
+
elements_position_count = 0
|
197
181
|
marks_blocks.map { |m| m[:line] }.each do |line|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
182
|
+
marks = marks_blocks.select { |m| m[:line] == line }
|
183
|
+
if marks.count == group.marks_options.count
|
184
|
+
first_position += marks.first[:x1]
|
185
|
+
elements_position_count += 1
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
if elements_position_count > 0
|
190
|
+
first_position = first_position / elements_position_count
|
191
|
+
distance = group.distance_between_marks * (group.marks_options.count - 1)
|
192
|
+
last_position = first_position + distance
|
193
|
+
marks_blocks.delete_if { |mark| mark[:x1] < first_position - 10 ||
|
194
|
+
mark[:x1] > last_position + 10 }
|
195
|
+
|
196
|
+
marks_blocks.map { |m| m[:line] }.each do |line|
|
197
|
+
loop do
|
198
|
+
reprocess = false
|
199
|
+
marks = marks_blocks.select { |m| m[:line] == line }
|
200
|
+
marks.each_with_index do |current_mark, index|
|
201
|
+
if index == 0
|
202
|
+
first_mark_position = first_position-5..first_position+5
|
203
|
+
unless first_mark_position.include?(current_mark[:x1])
|
204
|
+
new_mark = {x1: first_position,
|
205
|
+
x2: first_position + group.mark_width,
|
206
|
+
y1: current_mark[:y1],
|
207
|
+
y2: current_mark[:y1] + group.mark_height,
|
208
|
+
line: line}
|
209
|
+
marks_blocks << new_mark
|
210
|
+
marks_blocks.sort_by!{ |a| [a[:line], a[:x1]] }
|
211
|
+
bubbles_adjusted << new_mark
|
212
|
+
reprocess = true
|
213
|
+
break
|
214
|
+
end
|
215
|
+
end
|
216
|
+
next_mark = marks[index + 1]
|
217
|
+
distance = 0
|
218
|
+
distance = next_mark[:x1] - current_mark[:x1] if next_mark
|
219
|
+
if distance > group.distance_between_marks + 10 ||
|
220
|
+
next_mark.nil? && index + 1 < group.marks_options.count
|
221
|
+
|
222
|
+
new_x1 = current_mark[:x1] + group.distance_between_marks
|
223
|
+
new_mark = {x1: new_x1,
|
224
|
+
x2: new_x1 + group.mark_width,
|
207
225
|
y1: current_mark[:y1],
|
208
226
|
y2: current_mark[:y1] + group.mark_height,
|
209
227
|
line: line}
|
@@ -212,38 +230,19 @@ module RubyMarks
|
|
212
230
|
bubbles_adjusted << new_mark
|
213
231
|
reprocess = true
|
214
232
|
break
|
215
|
-
end
|
233
|
+
end
|
216
234
|
end
|
217
|
-
|
218
|
-
distance = 0
|
219
|
-
distance = next_mark[:x1] - current_mark[:x1] if next_mark
|
220
|
-
if distance > group.distance_between_marks + 10 ||
|
221
|
-
next_mark.nil? && index + 1 < group.marks_options.count
|
222
|
-
|
223
|
-
new_x1 = current_mark[:x1] + group.distance_between_marks
|
224
|
-
new_mark = {x1: new_x1,
|
225
|
-
x2: new_x1 + group.mark_width,
|
226
|
-
y1: current_mark[:y1],
|
227
|
-
y2: current_mark[:y1] + group.mark_height,
|
228
|
-
line: line}
|
229
|
-
marks_blocks << new_mark
|
230
|
-
marks_blocks.sort_by!{ |a| [a[:line], a[:x1]] }
|
231
|
-
bubbles_adjusted << new_mark
|
232
|
-
reprocess = true
|
233
|
-
break
|
234
|
-
end
|
235
|
+
break unless reprocess
|
235
236
|
end
|
236
|
-
break unless reprocess
|
237
237
|
end
|
238
|
+
|
238
239
|
end
|
239
|
-
|
240
240
|
end
|
241
241
|
|
242
242
|
marks_blocks.each do |mark|
|
243
243
|
mark_width = RubyMarks::ImageUtils.calc_width(mark[:x1], mark[:x2])
|
244
244
|
mark_height = RubyMarks::ImageUtils.calc_height(mark[:y1], mark[:y2])
|
245
245
|
mark_file = @original_file.crop(mark[:x1], mark[:y1], mark_width, mark_height)
|
246
|
-
|
247
246
|
o_mark = RubyMarks::Mark.new group: group,
|
248
247
|
coordinates: {x1: mark[:x1], y1: mark[:y1], x2: mark[:x2], y2: mark[:y2]},
|
249
248
|
image_str: RubyMarks::ImageUtils.export_file_to_str(mark_file),
|
@@ -264,6 +263,7 @@ module RubyMarks
|
|
264
263
|
if incorrect_bubble_line_found.any? || bubbles_adjusted.any? || incorrect_expected_lines
|
265
264
|
raise_watcher :incorrect_group_watcher, incorrect_expected_lines, incorrect_bubble_line_found, bubbles_adjusted.flatten
|
266
265
|
end
|
266
|
+
|
267
267
|
end
|
268
268
|
|
269
269
|
|
@@ -283,10 +283,32 @@ module RubyMarks
|
|
283
283
|
block[:width] = RubyMarks::ImageUtils.calc_width(block[:x1], block[:x2])
|
284
284
|
block[:height] = RubyMarks::ImageUtils.calc_height(block[:y1], block[:y2])
|
285
285
|
|
286
|
+
if @config.scan_mode == :grid
|
287
|
+
if block[:width] >= expected_width + group.block_width_tolerance
|
288
|
+
ajust_width = block[:width] - expected_width
|
289
|
+
if @config.auto_ajust_block_width == :left
|
290
|
+
block[:x2] = (block[:x2] - ajust_width) + @config.edge_level
|
291
|
+
block[:width] = expected_width + @config.edge_level
|
292
|
+
elsif @config.auto_ajust_block_width == :right
|
293
|
+
block[:x1] = (block[:x1] + ajust_width) - @config.edge_level
|
294
|
+
block[:width] = expected_width + @config.edge_level
|
295
|
+
end
|
296
|
+
end
|
297
|
+
if block[:height] > expected_height
|
298
|
+
ajust_width = block[:height] - expected_height
|
299
|
+
if @config.auto_ajust_block_height == :top
|
300
|
+
block[:y2] = (block[:y2] - ajust_height) + @config.edge_level
|
301
|
+
block[:height] = expected_height + @config.edge_level
|
302
|
+
elsif @config.auto_ajust_block_height == :bottom
|
303
|
+
block[:y1] = (block[:y1] + ajust_height) - @config.edge_level
|
304
|
+
block[:height] = expected_height + @config.edge_level
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
286
309
|
block_width_with_tolerance = block[:width] + group.block_width_tolerance
|
287
310
|
block_height_with_tolerance = block[:height] + group.block_height_tolerance
|
288
311
|
|
289
|
-
|
290
312
|
return block if block_width_with_tolerance >= expected_width &&
|
291
313
|
block_height_with_tolerance >= expected_height
|
292
314
|
|
@@ -310,10 +332,11 @@ module RubyMarks
|
|
310
332
|
distance_col = block_width / columns
|
311
333
|
lines.times do |lin|
|
312
334
|
columns.times do |col|
|
313
|
-
blocks << { :x1 => block[:x1] + (col * distance_col),
|
314
|
-
:y1 => block[:y1] + (lin * distance_lin),
|
315
|
-
:x2 => block[:x1] + (col * distance_col) + distance_col,
|
316
|
-
:y2 => block[:y1] + (lin * distance_lin) + distance_col
|
335
|
+
blocks << { :x1 => block[:x1] + (col * distance_col) + @config.edge_level,
|
336
|
+
:y1 => block[:y1] + (lin * distance_lin) + @config.edge_level,
|
337
|
+
:x2 => (block[:x1] + (col * distance_col) + distance_col) - @config.edge_level,
|
338
|
+
:y2 => block[:y1] + (lin * distance_lin) + distance_col - @config.edge_level,
|
339
|
+
:line => lin + 1 }
|
317
340
|
end
|
318
341
|
end
|
319
342
|
end
|
@@ -423,7 +446,15 @@ module RubyMarks
|
|
423
446
|
marks = Hash.new { |hash, key| hash[key] = [] }
|
424
447
|
group.marks.each_pair do |line, value|
|
425
448
|
value.each do |mark|
|
426
|
-
|
449
|
+
mark_width = RubyMarks::ImageUtils.calc_width(mark.coordinates[:x1], mark.coordinates[:x2])
|
450
|
+
mark_height = RubyMarks::ImageUtils.calc_height(mark.coordinates[:y1], mark.coordinates[:y2])
|
451
|
+
mark_file = @original_file.crop(mark.coordinates[:x1], mark.coordinates[:y1], mark_width, mark_height)
|
452
|
+
o_mark = RubyMarks::Mark.new group: group,
|
453
|
+
coordinates: {x1: mark.coordinates[:x1], y1: mark.coordinates[:y1], x2: mark.coordinates[:x2], y2: mark.coordinates[:y2]},
|
454
|
+
image_str: RubyMarks::ImageUtils.export_file_to_str(mark_file),
|
455
|
+
line: line
|
456
|
+
|
457
|
+
add_mark file, RubyMarks::ImageUtils.image_center(mark.coordinates), mark
|
427
458
|
end
|
428
459
|
end
|
429
460
|
end
|
@@ -461,20 +492,36 @@ module RubyMarks
|
|
461
492
|
end
|
462
493
|
end
|
463
494
|
|
464
|
-
def add_mark(file, position)
|
495
|
+
def add_mark(file, position, mark=nil)
|
465
496
|
dr = Magick::Draw.new
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
497
|
+
if @config.scan_mode == :grid
|
498
|
+
dr.annotate(file, 0, 0, position[:x]-9, position[:y]+5, mark.intensity.ceil.to_s) do
|
499
|
+
self.pointsize = 15
|
500
|
+
self.fill = RubyMarks::COLORS[2]
|
501
|
+
end
|
502
|
+
|
503
|
+
dr = Magick::Draw.new
|
504
|
+
dr.stroke_width = 2
|
505
|
+
dr.stroke(RubyMarks::COLORS[1])
|
506
|
+
dr.line(mark.coordinates[:x1], mark.coordinates[:y1], mark.coordinates[:x2], mark.coordinates[:y1])
|
507
|
+
dr.line(mark.coordinates[:x2], mark.coordinates[:y1], mark.coordinates[:x2], mark.coordinates[:y2])
|
508
|
+
dr.line(mark.coordinates[:x2], mark.coordinates[:y2], mark.coordinates[:x1], mark.coordinates[:y2])
|
509
|
+
dr.line(mark.coordinates[:x1], mark.coordinates[:y2], mark.coordinates[:x1], mark.coordinates[:y1])
|
510
|
+
dr.draw(file)
|
511
|
+
else
|
512
|
+
dr.annotate(file, 0, 0, position[:x]-9, position[:y]+11, "+") do
|
513
|
+
self.pointsize = 30
|
514
|
+
self.fill = '#900000'
|
515
|
+
end
|
470
516
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
517
|
+
dr = Magick::Draw.new
|
518
|
+
dr.fill = '#FF0000'
|
519
|
+
dr.point(position[:x], position[:y])
|
520
|
+
dr.point(position[:x], position[:y] + 1)
|
521
|
+
dr.point(position[:x] + 1, position[:y])
|
522
|
+
dr.point(position[:x] + 1, position[:y] + 1)
|
523
|
+
dr.draw(file)
|
524
|
+
end
|
478
525
|
end
|
479
526
|
|
480
527
|
end
|
data/lib/ruby_marks/version.rb
CHANGED
@@ -13,26 +13,28 @@ class RubyMarks::RecognizerGridTest < Test::Unit::TestCase
|
|
13
13
|
|
14
14
|
config.scan_mode = :grid
|
15
15
|
config.default_expected_lines = 5
|
16
|
-
config.intensity_percentual =
|
16
|
+
config.intensity_percentual = 75
|
17
|
+
config.auto_ajust_block_width = :right
|
18
|
+
config.default_block_width_tolerance = 10
|
17
19
|
|
18
20
|
config.define_group :um do |group|
|
19
|
-
group.expected_coordinates = {x1:
|
21
|
+
group.expected_coordinates = {x1: 160, y1: 235, x2: 285, y2: 360}
|
20
22
|
end
|
21
23
|
|
22
24
|
config.define_group :dois do |group|
|
23
|
-
group.expected_coordinates = {x1: 350, y1: 200, x2:
|
25
|
+
group.expected_coordinates = {x1: 350, y1: 200, x2: 475, y2: 350}
|
24
26
|
end
|
25
27
|
|
26
28
|
config.define_group :tres do |group|
|
27
|
-
group.expected_coordinates = {x1: 570, y1: 200, x2:
|
29
|
+
group.expected_coordinates = {x1: 570, y1: 200, x2: 695, y2: 350}
|
28
30
|
end
|
29
31
|
|
30
32
|
config.define_group :quatro do |group|
|
31
|
-
group.expected_coordinates = {x1: 790, y1: 200, x2:
|
33
|
+
group.expected_coordinates = {x1: 790, y1: 200, x2: 915, y2: 350}
|
32
34
|
end
|
33
35
|
|
34
36
|
config.define_group :cinco do |group|
|
35
|
-
group.expected_coordinates = {x1: 1010, y1: 200, x2:
|
37
|
+
group.expected_coordinates = {x1: 1010, y1: 200, x2: 1135, y2: 350}
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
@@ -52,38 +54,38 @@ class RubyMarks::RecognizerGridTest < Test::Unit::TestCase
|
|
52
54
|
def test_should_scan_the_recognizer_and_get_a_hash_of_marked_marks
|
53
55
|
expected_hash = {
|
54
56
|
um: {
|
55
|
-
1 => ['
|
56
|
-
2 => ['
|
57
|
-
3 => ['
|
57
|
+
1 => ['A'],
|
58
|
+
2 => ['A'],
|
59
|
+
3 => ['D'],
|
58
60
|
4 => ['B'],
|
59
|
-
5 => ['
|
61
|
+
5 => ['B']
|
60
62
|
},
|
61
63
|
dois: {
|
62
|
-
1 => ['
|
64
|
+
1 => ['B'],
|
63
65
|
2 => ['A'],
|
64
|
-
3 => ['
|
66
|
+
3 => ['A'],
|
65
67
|
4 => ['A'],
|
66
68
|
5 => ['D']
|
67
69
|
},
|
68
70
|
tres: {
|
69
|
-
1 => ['
|
70
|
-
2 => ['
|
71
|
+
1 => ['A'],
|
72
|
+
2 => ['B'],
|
71
73
|
3 => ['A'],
|
72
74
|
4 => ['A'],
|
73
|
-
5 => ['
|
75
|
+
5 => ['D']
|
74
76
|
},
|
75
77
|
quatro: {
|
76
78
|
1 => ['B'],
|
77
|
-
2 => ['
|
79
|
+
2 => ['D'],
|
78
80
|
3 => ['A'],
|
79
81
|
4 => ['C'],
|
80
|
-
5 => ['
|
82
|
+
5 => ['C']
|
81
83
|
},
|
82
84
|
cinco: {
|
83
|
-
1 => ['
|
84
|
-
2 => ['
|
85
|
+
1 => ['C'],
|
86
|
+
2 => ['D'],
|
85
87
|
3 => ['A'],
|
86
|
-
4 => ['
|
88
|
+
4 => ['C'],
|
87
89
|
5 => ['C']
|
88
90
|
}
|
89
91
|
}
|
@@ -10,7 +10,7 @@ class RubyMarks::RecognizerTest < Test::Unit::TestCase
|
|
10
10
|
@positions[:unmarked_position] = {x: 161, y: 994}
|
11
11
|
|
12
12
|
@recognizer.configure do |config|
|
13
|
-
|
13
|
+
|
14
14
|
config.define_group :first do |group|
|
15
15
|
group.expected_coordinates = {x1: 145, y1: 780, x2: 270, y2: 1290}
|
16
16
|
end
|
@@ -42,6 +42,7 @@ class RubyMarks::RecognizerTest < Test::Unit::TestCase
|
|
42
42
|
|
43
43
|
end
|
44
44
|
|
45
|
+
=begin
|
45
46
|
def test_should_initialize_a_recognizer_with_a_valid_file
|
46
47
|
assert_equal @file, @recognizer.filename
|
47
48
|
end
|
@@ -161,6 +162,6 @@ class RubyMarks::RecognizerTest < Test::Unit::TestCase
|
|
161
162
|
@recognizer.scan
|
162
163
|
assert @recognizer.raised_watchers.include?(:timed_out_watcher)
|
163
164
|
end
|
164
|
-
|
165
|
+
=end
|
165
166
|
end
|
166
167
|
|