ruby_marks 0.2.6 → 0.2.7

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.
@@ -91,6 +91,7 @@ module RubyMarks
91
91
  end
92
92
  rescue Timeout::Error
93
93
  raise_watcher :timed_out_watcher
94
+ return result
94
95
  end
95
96
 
96
97
  @groups.each_pair do |label, group|
@@ -124,150 +125,131 @@ module RubyMarks
124
125
  @groups.each_pair do |label, group|
125
126
  next unless group.expected_coordinates.any?
126
127
 
128
+ line = 0
127
129
  group_center = RubyMarks::ImageUtils.image_center(group.expected_coordinates)
128
130
 
129
131
  block = find_block_marks(file_str, group_center[:x], group_center[:y], group.expected_coordinates)
130
-
131
132
  if block
132
133
  group.coordinates = {x1: block[:x1], x2: block[:x2], y1: block[:y1], y2: block[:y2]}
133
-
134
+
134
135
  marks_blocks = find_marks(original_file_str, group)
135
- positions = []
136
+ marks_blocks.sort!{ |a,b| a[:y1] <=> b[:y1] }
137
+ mark_ant = nil
136
138
  marks_blocks.each do |mark|
137
- line = 0
138
139
  mark_width = RubyMarks::ImageUtils.calc_width(mark[:x1], mark[:x2])
139
140
  mark_height = RubyMarks::ImageUtils.calc_height(mark[:y1], mark[:y2])
140
-
141
+
141
142
  if mark_width >= group.mark_width_with_down_tolerance &&
142
143
  mark_height >= group.mark_height_with_down_tolerance
143
144
 
144
- group.marks.each_pair do |key, marks_array|
145
- mark_positions = mark[:y1]-group.mark_width_with_down_tolerance..mark[:y1]+group.mark_width_with_down_tolerance
146
-
147
- marks_array.each do |m|
148
- if mark_positions.include?(m.coordinates[:y1])
149
- line = key
150
- break
151
- end
152
- end
153
-
154
- break if line > 0
155
- end
145
+ mark_positions = mark[:y1]-10..mark[:y1]+10
146
+ line += 1 unless mark_ant && mark_positions.include?(mark_ant[:y1])
147
+ mark[:line] = line
148
+ mark_ant = mark
149
+ end
150
+ end
156
151
 
157
- line = group.marks.size + 1 if line == 0
152
+ marks_blocks.delete_if { |m| m[:line].nil? }
153
+ marks_blocks.sort_by!{ |a| [a[:line], a[:x1]] }
158
154
 
159
- conflict_marks = group.marks[line].select do |el|
160
- el.coordinates[:x2] >= mark[:x1] && el.coordinates[:x2] <= mark[:x2] ||
161
- el.coordinates[:x1] >= mark[:x1] && el.coordinates[:x1] <= mark[:x2]
162
- end
163
-
164
- if conflict_marks.any?
165
- conflict_marks.each do |conflict_mark|
166
- group.marks[line].delete(conflict_mark)
167
- end
155
+ mark_ant = nil
156
+ marks_blocks.each do |mark|
157
+ if mark_ant && mark_ant[:line] == mark[:line]
158
+ mark_ant_center = RubyMarks::ImageUtils.image_center(mark_ant)
159
+ mark_center = RubyMarks::ImageUtils.image_center(mark)
160
+ if (mark_ant_center[:x] - mark_center[:x]).abs < 10
161
+ mark[:conflict] = true
162
+ mark[:conflicting_mark] = mark_ant
168
163
  else
169
- mark_file = @original_file.crop(mark[:x1], mark[:y1], mark_width, mark_height)
170
-
171
- mark = RubyMarks::Mark.new group: group,
172
- coordinates: {x1: mark[:x1], y1: mark[:y1], x2: mark[:x2], y2: mark[:y2]},
173
- image_str: RubyMarks::ImageUtils.export_file_to_str(mark_file),
174
- line: line
175
-
176
- group.marks[line] << mark
177
- group.marks[line].sort! { |a, b| a.coordinates[:x1] <=> b.coordinates[:x1] }
164
+ mark_ant = mark
178
165
  end
166
+ else
167
+ mark_ant = mark
179
168
  end
180
169
  end
181
-
170
+ marks_blocks.delete_if { |m| m[:conflict] }
171
+
182
172
  first_position = 0
183
173
  elements_position_count = 0
184
- group.marks.each_pair do |line, marks|
185
-
186
- if marks.count == group.marks_options.count &&
187
- marks.first && marks.first.coordinates
188
-
189
- first_position += marks.first.coordinates[:x1]
190
-
174
+ marks_blocks.map { |m| m[:line] }.each do |line|
175
+ marks = marks_blocks.select { |m| m[:line] == line }
176
+ if marks.count == group.marks_options.count
177
+ first_position += marks.first[:x1]
191
178
  elements_position_count += 1
192
179
  end
193
180
  end
194
181
 
195
- if @config.adjust_inconsistent_bubbles && elements_position_count > 0
182
+ if elements_position_count > 0
196
183
  first_position = first_position / elements_position_count
197
184
  distance = group.distance_between_marks * (group.marks_options.count - 1)
198
185
  last_position = first_position + distance
186
+ marks_blocks.delete_if { |mark| mark[:x1] < first_position - 10 ||
187
+ mark[:x1] > last_position + 10 }
199
188
 
200
- group.marks.each_pair do |line, marks|
189
+ marks_blocks.map { |m| m[:line] }.each do |line|
201
190
  loop do
202
191
  reprocess = false
192
+ marks = marks_blocks.select { |m| m[:line] == line }
203
193
  marks.each_with_index do |current_mark, index|
204
- if current_mark.coordinates[:x1] < first_position - 10 ||
205
- current_mark.coordinates[:x1] > last_position + 10
206
-
207
- group.marks[line].delete(current_mark)
208
- reprocess = true
209
- bubbles_adjusted << current_mark.coordinates
210
- break
211
-
212
- else
213
-
214
- if index == 0
215
- first_mark_position = first_position-5..first_position+5
216
- unless first_mark_position.include?(current_mark.coordinates[:x1])
217
- new_mark_x1 = first_position
218
- new_mark_x2 = new_mark_x1 + group.mark_width
219
- new_mark_y1 = current_mark.coordinates[:y1]
220
- new_mark_y2 = new_mark_y1 + group.mark_height
221
- reprocess = true
222
- end
223
- end
224
-
225
- unless reprocess
226
- next_mark = marks[index + 1]
227
- distance = 0
228
- distance = next_mark.coordinates[:x1] - current_mark.coordinates[:x1] if next_mark
229
-
230
- if distance > group.distance_between_marks + 10 ||
231
- next_mark.nil? && index + 1 < group.marks_options.count
232
- new_mark_x1 = current_mark.coordinates[:x1] + group.distance_between_marks
233
- new_mark_x2 = new_mark_x1 + group.mark_width
234
- new_mark_y1 = current_mark.coordinates[:y1]
235
- new_mark_y2 = new_mark_y1 + group.mark_height
236
- end
237
- end
238
-
239
- if new_mark_x1 && new_mark_x2 && new_mark_y1 && new_mark_y2
240
- mark_width = RubyMarks::ImageUtils.calc_width(new_mark_x1, new_mark_x2)
241
- mark_height = RubyMarks::ImageUtils.calc_height(new_mark_y1, new_mark_y2)
242
-
243
- mark_file = @original_file.crop(new_mark_x1, new_mark_y1, mark_width, mark_height)
244
-
245
- current_mark = RubyMarks::Mark.new group: group,
246
- coordinates: {x1: new_mark_x1, y1: new_mark_y1, x2: new_mark_x2, y2: new_mark_y2},
247
- image_str: RubyMarks::ImageUtils.export_file_to_str(mark_file),
248
- line: line
249
-
250
- group.marks[line] << current_mark
251
- group.marks[line].sort! { |a, b| a.coordinates[:x1] <=> b.coordinates[:x1] }
194
+ if index == 0
195
+ first_mark_position = first_position-5..first_position+5
196
+ unless first_mark_position.include?(current_mark[:x1])
197
+ new_mark = {x1: first_position,
198
+ x2: first_position + group.mark_width,
199
+ y1: current_mark[:y1],
200
+ y2: current_mark[:y1] + group.mark_height,
201
+ line: line}
202
+ marks_blocks << new_mark
203
+ marks_blocks.sort_by!{ |a| [a[:line], a[:x1]] }
204
+ bubbles_adjusted << new_mark
252
205
  reprocess = true
253
- bubbles_adjusted << current_mark.coordinates
254
206
  break
255
207
  end
256
208
  end
257
- break if reprocess
209
+ next_mark = marks[index + 1]
210
+ distance = 0
211
+ distance = next_mark[:x1] - current_mark[:x1] if next_mark
212
+ if distance > group.distance_between_marks + 10 ||
213
+ next_mark.nil? && index + 1 < group.marks_options.count
214
+
215
+ new_x1 = current_mark[:x1] + group.distance_between_marks
216
+ new_mark = {x1: new_x1,
217
+ x2: new_x1 + group.mark_width,
218
+ y1: current_mark[:y1],
219
+ y2: current_mark[:y1] + group.mark_height,
220
+ line: line}
221
+ marks_blocks << new_mark
222
+ marks_blocks.sort_by!{ |a| [a[:line], a[:x1]] }
223
+ bubbles_adjusted << new_mark
224
+ reprocess = true
225
+ break
226
+ end
258
227
  end
259
228
  break unless reprocess
260
229
  end
261
-
262
- incorrect_expected_lines = true if group.incorrect_expected_lines
263
230
  end
231
+
264
232
  end
265
233
 
234
+ marks_blocks.each do |mark|
235
+ mark_width = RubyMarks::ImageUtils.calc_width(mark[:x1], mark[:x2])
236
+ mark_height = RubyMarks::ImageUtils.calc_height(mark[:y1], mark[:y2])
237
+ mark_file = @original_file.crop(mark[:x1], mark[:y1], mark_width, mark_height)
238
+
239
+ o_mark = RubyMarks::Mark.new group: group,
240
+ coordinates: {x1: mark[:x1], y1: mark[:y1], x2: mark[:x2], y2: mark[:y2]},
241
+ image_str: RubyMarks::ImageUtils.export_file_to_str(mark_file),
242
+ line: mark[:line]
243
+ group.marks[mark[:line]] << o_mark
244
+ end
245
+
246
+ incorrect_expected_lines = group.incorrect_expected_lines
247
+
266
248
  group.marks.each_pair do |line, marks|
267
249
  if marks.count != group.marks_options.count
268
250
  incorrect_bubble_line_found[group.label.to_sym] << line
269
251
  end
270
- end
252
+ end
271
253
  end
272
254
  end
273
255
  @groups_detected = true
@@ -333,23 +315,24 @@ module RubyMarks
333
315
  distance_x1 = x - result[:x1]
334
316
  distance_x2 = result[:x2] - x
335
317
  if distance_x1 <= distance_x2
336
- result[:x2] = result[:x1] + group.mark_width_with_up_tolerance - 2
318
+ result[:x2] = result[:x1] + group.mark_width
337
319
  else
338
- result[:x1] = result[:x2] - group.mark_width_with_up_tolerance + 2
320
+ result[:x1] = result[:x2] - group.mark_width
339
321
  end
340
322
  end
341
-
323
+
342
324
  if mark_height > group.mark_height_with_up_tolerance
343
325
  distance_y1 = y - result[:y1]
344
326
  distance_y2 = result[:y2] - y
345
327
  if distance_y1 <= distance_y2
346
- result[:y2] = result[:y1] + group.mark_height_with_up_tolerance - 2
328
+ result[:y2] = result[:y1] + group.mark_height
347
329
  else
348
- result[:y1] = result[:y2] - group.mark_height_with_up_tolerance + 2
330
+ result[:y1] = result[:y2] - group.mark_height
349
331
  end
350
332
  end
351
333
 
352
- blocks << result
334
+ blocks << result unless blocks.any? { |b| b == result }
335
+
353
336
  end
354
337
  x += 1
355
338
  end
@@ -383,6 +366,7 @@ module RubyMarks
383
366
  end
384
367
  rescue Timeout::Error
385
368
  raise_watcher :timed_out_watcher
369
+ return false
386
370
  end
387
371
 
388
372
  @groups.each_pair do |label, group|
@@ -1,3 +1,3 @@
1
1
  module RubyMarks
2
- VERSION = "0.2.6".freeze
2
+ VERSION = "0.2.7".freeze
3
3
  end
data/lib/ruby_marks.rb CHANGED
@@ -70,5 +70,4 @@ require 'ruby_marks/group'
70
70
  require 'ruby_marks/image_utils'
71
71
  require 'ruby_marks/mark'
72
72
  require 'ruby_marks/recognizer'
73
- require 'ruby_marks/scan_area'
74
73
  require 'ruby_marks/watcher'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_marks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,15 +37,14 @@ extensions: []
37
37
  extra_rdoc_files: []
38
38
  files:
39
39
  - README.md
40
- - lib/ruby_marks/config.rb
41
40
  - lib/ruby_marks/group.rb
42
- - lib/ruby_marks/image_utils.rb
43
- - lib/ruby_marks/mark.rb
41
+ - lib/ruby_marks/watcher.rb
42
+ - lib/ruby_marks/config.rb
43
+ - lib/ruby_marks/version.rb
44
44
  - lib/ruby_marks/recognizer.rb
45
- - lib/ruby_marks/scan_area.rb
46
45
  - lib/ruby_marks/support.rb
47
- - lib/ruby_marks/version.rb
48
- - lib/ruby_marks/watcher.rb
46
+ - lib/ruby_marks/image_utils.rb
47
+ - lib/ruby_marks/mark.rb
49
48
  - lib/ruby_marks.rb
50
49
  - test/ruby_marks/image_utils_test.rb
51
50
  - test/ruby_marks/recognizer_test.rb
@@ -1,11 +0,0 @@
1
- module RubyMarks
2
-
3
- class ScanArea
4
- attr_accessor :coordinates, :file
5
-
6
- def initialize(coordinates)
7
- @coordinates = coordinates
8
- end
9
- end
10
-
11
- end