mork 0.16.1 → 0.17.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 +4 -4
- data/README.md +23 -0
- data/lib/mork/grid_omr.rb +7 -0
- data/lib/mork/magicko.rb +33 -5
- data/lib/mork/mimage.rb +5 -1
- data/lib/mork/sheet_omr.rb +88 -0
- data/lib/mork/version.rb +1 -1
- data/spec/mork/grid_omr_spec.rb +14 -0
- data/spec/mork/magicko_spec.rb +12 -0
- data/spec/mork/sheet_omr_spec.rb +52 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a0c88481311d1eeaf37c2df7f308dbde8e72968a23cdd8a649012d1591898412
|
|
4
|
+
data.tar.gz: 90ff649618ec8c71c75e2c1b6fa09a36d0bd9016a1bc529628ab5f4027b4bf97
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 16bbe20136a27742f2247b7ac00856e3dd1bb68349149c65ba94ab5bb06656ecf92e2400a509953251af5074bfbc1bbbd7db63b74a299e9c785db751750e4d10
|
|
7
|
+
data.tar.gz: 30b783689b3bac8dc7785fcaeac7681eb3da5f1d9f6a679a7a08be267094b918e71fc103c3ab7d69d4ac90a4de77a164e39aa4717fa047d42fa46eddc6b3c8d4
|
data/README.md
CHANGED
|
@@ -296,6 +296,29 @@ s.save 'marked_choices_and_outlines.jpg'
|
|
|
296
296
|
system 'open marked_choices_and_outlines.jpg' # this works in macOS
|
|
297
297
|
```
|
|
298
298
|
|
|
299
|
+
For single-answer tests, Mork also provides a higher-level method that compares the detected responses against an answer key and applies the overlays automatically:
|
|
300
|
+
|
|
301
|
+
```ruby
|
|
302
|
+
correct = [3, 0, 2, 1, 2] # one zero-based correct choice per question
|
|
303
|
+
s.overlay_corrections correct
|
|
304
|
+
s.save 'corrections.jpg'
|
|
305
|
+
system 'open corrections.jpg' # this works in macOS
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
`overlay_corrections` applies:
|
|
309
|
+
|
|
310
|
+
- a green outline on the marked cell when the response is correct
|
|
311
|
+
- a red outline on the expected cell and a red cross on the marked cell when the response is incorrect
|
|
312
|
+
- a red outline on the expected cell when the response is blank or invalid
|
|
313
|
+
|
|
314
|
+
By default, `overlay_corrections` uses the unique marked response for each question, so if a responder marks more than one choice, the response is treated as invalid and no red cross is applied. To cross all marked cells for incorrect or multi-marked responses:
|
|
315
|
+
|
|
316
|
+
```ruby
|
|
317
|
+
s.overlay_corrections correct, marked: :all
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
The answer key must contain one entry per configured question, and each entry must be either a zero-based choice index, a one-element array containing that index, or `nil` to skip overlaying a question.
|
|
321
|
+
|
|
299
322
|
Scoring can only be performed if the sheet gets properly registered, which in turn depends on the quality of the scanned image.
|
|
300
323
|
|
|
301
324
|
### Improving sheet registration and marking
|
data/lib/mork/grid_omr.rb
CHANGED
|
@@ -5,6 +5,8 @@ require 'deep_merge/rails_compat'
|
|
|
5
5
|
module Mork
|
|
6
6
|
# @private
|
|
7
7
|
class GridOMR < Grid
|
|
8
|
+
OVERLAY_STROKE_WIDTH_MM = 0.5
|
|
9
|
+
|
|
8
10
|
def initialize(options=nil)
|
|
9
11
|
super options
|
|
10
12
|
end
|
|
@@ -15,6 +17,10 @@ module Mork
|
|
|
15
17
|
self
|
|
16
18
|
end
|
|
17
19
|
|
|
20
|
+
def overlay_stroke_width_px
|
|
21
|
+
[(pixels_per_unit * OVERLAY_STROKE_WIDTH_MM).round, 1].max
|
|
22
|
+
end
|
|
23
|
+
|
|
18
24
|
def barcode_areas(bits)
|
|
19
25
|
[].tap do |areas|
|
|
20
26
|
bits.each_with_index do |b, i|
|
|
@@ -61,6 +67,7 @@ module Mork
|
|
|
61
67
|
|
|
62
68
|
def cx() @px / reg_frame_width end
|
|
63
69
|
def cy() @py / reg_frame_height end
|
|
70
|
+
def pixels_per_unit() (ppu_x + ppu_y) / 2.0 end
|
|
64
71
|
def ppu_x() @px / page_width end
|
|
65
72
|
def ppu_y() @py / page_height end
|
|
66
73
|
|
data/lib/mork/magicko.rb
CHANGED
|
@@ -8,10 +8,12 @@ module Mork
|
|
|
8
8
|
class Magicko
|
|
9
9
|
attr_reader :width
|
|
10
10
|
attr_reader :height
|
|
11
|
+
attr_reader :overlay_stroke_width
|
|
11
12
|
|
|
12
13
|
def initialize(path)
|
|
13
14
|
@path = path
|
|
14
15
|
@cmd = []
|
|
16
|
+
@overlay_stroke_width = 3
|
|
15
17
|
# a density is required for processing PDF or other vector-based images;
|
|
16
18
|
# a default of 150 dpi seems sensible. It should not affect bitmaps.
|
|
17
19
|
density = 150
|
|
@@ -39,6 +41,10 @@ module Mork
|
|
|
39
41
|
end
|
|
40
42
|
end
|
|
41
43
|
|
|
44
|
+
def overlay_stroke_width=(width)
|
|
45
|
+
@overlay_stroke_width = [width.to_i, 1].max
|
|
46
|
+
end
|
|
47
|
+
|
|
42
48
|
def valid?
|
|
43
49
|
@valid
|
|
44
50
|
end
|
|
@@ -71,14 +77,36 @@ module Mork
|
|
|
71
77
|
end
|
|
72
78
|
|
|
73
79
|
def outline(coords, rounded)
|
|
74
|
-
|
|
75
|
-
|
|
80
|
+
outline_with_color(coords, rounded, 'green')
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def outline_green(coords, rounded)
|
|
84
|
+
outline_with_color(coords, rounded, 'green')
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def outline_red(coords, rounded)
|
|
88
|
+
outline_with_color(coords, rounded, 'red')
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def check(coords, rounded)
|
|
92
|
+
check_with_color(coords, 'red')
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def check_red(coords, rounded)
|
|
96
|
+
check_with_color(coords, 'red')
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def outline_with_color(coords, rounded, color)
|
|
100
|
+
return if coords.empty?
|
|
101
|
+
@cmd << [:stroke, color]
|
|
102
|
+
@cmd << [:strokewidth, overlay_stroke_width]
|
|
76
103
|
@cmd << [:fill, 'none']
|
|
77
104
|
coords.each { |c| @cmd << [:draw, shape(c, rounded)] }
|
|
78
105
|
end
|
|
79
106
|
|
|
80
|
-
def
|
|
81
|
-
|
|
107
|
+
def check_with_color(coords, color)
|
|
108
|
+
return if coords.empty?
|
|
109
|
+
@cmd << [:stroke, color]
|
|
82
110
|
@cmd << [:strokewidth, '3']
|
|
83
111
|
coords.each do |c|
|
|
84
112
|
@cmd << [:draw, "line #{c.cross1}"]
|
|
@@ -115,7 +143,7 @@ module Mork
|
|
|
115
143
|
def join(p)
|
|
116
144
|
@cmd << [:fill, 'none']
|
|
117
145
|
@cmd << [:stroke, 'green']
|
|
118
|
-
@cmd << [:strokewidth,
|
|
146
|
+
@cmd << [:strokewidth, overlay_stroke_width]
|
|
119
147
|
pts = [
|
|
120
148
|
p[0][:x], p[0][:y],
|
|
121
149
|
p[1][:x], p[1][:y],
|
data/lib/mork/mimage.rb
CHANGED
|
@@ -14,6 +14,7 @@ module Mork
|
|
|
14
14
|
@mack = Magicko.new path
|
|
15
15
|
|
|
16
16
|
@grom = grom.set_page_size @mack.width, @mack.height
|
|
17
|
+
@mack.overlay_stroke_width = @grom.overlay_stroke_width_px
|
|
17
18
|
@choxq = [(0...@grom.max_choices_per_question).to_a] * grom.max_questions
|
|
18
19
|
@rm = {} # registration mark centers
|
|
19
20
|
@valid = register
|
|
@@ -86,7 +87,10 @@ module Mork
|
|
|
86
87
|
fail ArgumentError, 'Invalid overlay argument “where”'
|
|
87
88
|
end
|
|
88
89
|
round = where != :barcode
|
|
89
|
-
@mack.
|
|
90
|
+
unless @mack.respond_to?(what)
|
|
91
|
+
fail ArgumentError, 'Invalid overlay argument “what”'
|
|
92
|
+
end
|
|
93
|
+
@mack.public_send what, areas, round
|
|
90
94
|
end
|
|
91
95
|
|
|
92
96
|
# write the underlying MiniMagick::Image to disk;
|
data/lib/mork/sheet_omr.rb
CHANGED
|
@@ -163,6 +163,47 @@ module Mork
|
|
|
163
163
|
@mim.overlay what, where
|
|
164
164
|
end
|
|
165
165
|
|
|
166
|
+
# Applies correctness-aware overlays to the image using an answer key.
|
|
167
|
+
#
|
|
168
|
+
# Correct responses receive a green outline on the marked cell. Incorrect
|
|
169
|
+
# responses receive a red outline on the expected cell and a red cross on
|
|
170
|
+
# the given cell. Blank or invalid responses receive only the red outline
|
|
171
|
+
# on the expected cell.
|
|
172
|
+
#
|
|
173
|
+
# @param correct_choices [Array<Integer, Array<Integer>, nil>] one entry per
|
|
174
|
+
# question. Each entry can be an integer choice index, a 1-element array
|
|
175
|
+
# containing that index, or nil to skip that question.
|
|
176
|
+
# @param marked [Symbol] `:unique` treats multi-marked responses as invalid
|
|
177
|
+
# and does not cross them out; `:all` crosses all marked cells for
|
|
178
|
+
# incorrect responses.
|
|
179
|
+
def overlay_corrections(correct_choices, marked: :unique)
|
|
180
|
+
return if not_registered
|
|
181
|
+
|
|
182
|
+
expected = normalize_correct_choices(correct_choices)
|
|
183
|
+
actual = marked_responses(marked)
|
|
184
|
+
|
|
185
|
+
green_outlines = Array.new(expected.length) { [] }
|
|
186
|
+
red_outlines = Array.new(expected.length) { [] }
|
|
187
|
+
red_crosses = Array.new(expected.length) { [] }
|
|
188
|
+
|
|
189
|
+
expected.each_with_index do |choice, question|
|
|
190
|
+
next if choice.nil?
|
|
191
|
+
|
|
192
|
+
marked_cells = actual[question]
|
|
193
|
+
if marked_cells == [choice]
|
|
194
|
+
green_outlines[question] = [choice]
|
|
195
|
+
next
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
red_outlines[question] = [choice]
|
|
199
|
+
red_crosses[question] = marked_cells if marked_cells.any?
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
@mim.overlay :outline_green, green_outlines
|
|
203
|
+
@mim.overlay :outline_red, red_outlines
|
|
204
|
+
@mim.overlay :check_red, red_crosses
|
|
205
|
+
end
|
|
206
|
+
|
|
166
207
|
# Saves a copy of the source image after registration;
|
|
167
208
|
# the output image will also contain any previously applied overlays.
|
|
168
209
|
#
|
|
@@ -187,6 +228,53 @@ module Mork
|
|
|
187
228
|
private #
|
|
188
229
|
# ============================================================#
|
|
189
230
|
|
|
231
|
+
def normalize_correct_choices(correct_choices)
|
|
232
|
+
unless correct_choices.is_a?(Array)
|
|
233
|
+
fail ArgumentError, 'Correct choices must be an array'
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
if correct_choices.length != @mim.choxq.length
|
|
237
|
+
fail ArgumentError, 'Correct choices must match the configured number of questions'
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
correct_choices.map.with_index do |choice, question|
|
|
241
|
+
normalized =
|
|
242
|
+
case choice
|
|
243
|
+
when nil
|
|
244
|
+
nil
|
|
245
|
+
when Integer
|
|
246
|
+
choice
|
|
247
|
+
when Array
|
|
248
|
+
if choice.length > 1
|
|
249
|
+
fail ArgumentError, 'Each question supports a single correct choice'
|
|
250
|
+
end
|
|
251
|
+
choice.first
|
|
252
|
+
else
|
|
253
|
+
fail ArgumentError, 'Correct choices must contain integers, 1-element arrays, or nil'
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
next if normalized.nil?
|
|
257
|
+
|
|
258
|
+
max_choice = @mim.choxq[question].length
|
|
259
|
+
if normalized.negative? || normalized >= max_choice
|
|
260
|
+
fail ArgumentError, 'Correct choice exceeds the configured number of choices'
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
normalized
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def marked_responses(mode)
|
|
268
|
+
case mode
|
|
269
|
+
when :unique
|
|
270
|
+
marked_choices.map { |choices| choices.length == 1 ? choices : [] }
|
|
271
|
+
when :all
|
|
272
|
+
marked_choices
|
|
273
|
+
else
|
|
274
|
+
fail ArgumentError, 'Invalid marked argument'
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
190
278
|
def not_registered
|
|
191
279
|
unless valid?
|
|
192
280
|
puts "---=={ Unregistered image. Reason: '#{@mim.status.inspect}' }==---"
|
data/lib/mork/version.rb
CHANGED
data/spec/mork/grid_omr_spec.rb
CHANGED
|
@@ -56,6 +56,20 @@ module Mork
|
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
describe '#overlay_stroke_width_px' do
|
|
60
|
+
it 'keeps the default visual width on the reference scan' do
|
|
61
|
+
grom = GridOMR.new 'spec/samples/jdoe/layout.yml'
|
|
62
|
+
grom.set_page_size 1240, 1754
|
|
63
|
+
expect(grom.overlay_stroke_width_px).to eq 3
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'scales with scan resolution' do
|
|
67
|
+
grom = GridOMR.new 'spec/samples/jdoe/layout.yml'
|
|
68
|
+
grom.set_page_size 2480, 3508
|
|
69
|
+
expect(grom.overlay_stroke_width_px).to eq 6
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
59
73
|
describe '#paper_white_area' do
|
|
60
74
|
it 'returns the coordinates of the white area used for barcode calibration' do
|
|
61
75
|
expect(@grom.paper_white_area).to have_coords(93, 2260, 25, 21)
|
data/spec/mork/magicko_spec.rb
CHANGED
|
@@ -38,5 +38,17 @@ module Mork
|
|
|
38
38
|
expect(ma.registered_bytes(pp).length).to eq sh.height*sh.width
|
|
39
39
|
end
|
|
40
40
|
end
|
|
41
|
+
|
|
42
|
+
describe 'overlay stroke width' do
|
|
43
|
+
it 'defaults to the legacy 3-pixel width' do
|
|
44
|
+
expect(ma.overlay_stroke_width).to eq 3
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'uses the configured width for green outlines' do
|
|
48
|
+
ma.overlay_stroke_width = 6
|
|
49
|
+
ma.outline [co], false
|
|
50
|
+
expect(ma.instance_variable_get(:@cmd)).to include([:strokewidth, 6])
|
|
51
|
+
end
|
|
52
|
+
end
|
|
41
53
|
end
|
|
42
54
|
end
|
data/spec/mork/sheet_omr_spec.rb
CHANGED
|
@@ -148,6 +148,58 @@ module Mork
|
|
|
148
148
|
omr.overlay :outline
|
|
149
149
|
omr.save "spec/out/JD-outline-marked.jpeg"
|
|
150
150
|
end
|
|
151
|
+
|
|
152
|
+
it 'applies correctness-aware overlays and saves the result' do
|
|
153
|
+
omr.set_choices [5] * 5
|
|
154
|
+
omr.overlay_corrections [0, 0, 2, 3, 1]
|
|
155
|
+
omr.save "spec/out/JD-corrections.jpeg"
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
context 'applying correctness-aware overlays' do
|
|
160
|
+
let(:mim) { omr.instance_variable_get(:@mim) }
|
|
161
|
+
|
|
162
|
+
before do
|
|
163
|
+
omr.set_choices [5] * 4
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it 'batches green outlines, red outlines, and red crosses by correctness' do
|
|
167
|
+
allow(omr).to receive(:marked_choices).and_return([[1], [2], [], [3, 4]])
|
|
168
|
+
|
|
169
|
+
expect(mim).to receive(:overlay).with(:outline_green, [[1], [], [], []]).ordered
|
|
170
|
+
expect(mim).to receive(:overlay).with(:outline_red, [[], [0], [2], [3]]).ordered
|
|
171
|
+
expect(mim).to receive(:overlay).with(:check_red, [[], [2], [], []]).ordered
|
|
172
|
+
|
|
173
|
+
omr.overlay_corrections [1, 0, 2, 3]
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it 'can cross all marked cells for incorrect responses' do
|
|
177
|
+
allow(omr).to receive(:marked_choices).and_return([[1, 2], [0]])
|
|
178
|
+
|
|
179
|
+
omr.set_choices [5] * 2
|
|
180
|
+
|
|
181
|
+
expect(mim).to receive(:overlay).with(:outline_green, [[], []]).ordered
|
|
182
|
+
expect(mim).to receive(:overlay).with(:outline_red, [[3], [1]]).ordered
|
|
183
|
+
expect(mim).to receive(:overlay).with(:check_red, [[1, 2], [0]]).ordered
|
|
184
|
+
|
|
185
|
+
omr.overlay_corrections [3, 1], marked: :all
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it 'raises an ArgumentError if the answer key length does not match the questions' do
|
|
189
|
+
expect { omr.overlay_corrections([0, 1, 2]) }.to raise_error(ArgumentError)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
it 'raises an ArgumentError if a correct choice exceeds the configured choices' do
|
|
193
|
+
expect { omr.overlay_corrections([0, 1, 5, 2]) }.to raise_error(ArgumentError)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
it 'raises an ArgumentError if a question has multiple correct choices' do
|
|
197
|
+
expect { omr.overlay_corrections([0, [1, 2], 2, 3]) }.to raise_error(ArgumentError)
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
it 'raises an ArgumentError if the marked mode is invalid' do
|
|
201
|
+
expect { omr.overlay_corrections([0, 1, 2, 3], marked: :invalid) }.to raise_error(ArgumentError)
|
|
202
|
+
end
|
|
151
203
|
end
|
|
152
204
|
|
|
153
205
|
context 'requesting invalid responses and choices' do
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mork
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.17.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Giuseppe Bertini
|
|
@@ -283,7 +283,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
283
283
|
- !ruby/object:Gem::Version
|
|
284
284
|
version: '0'
|
|
285
285
|
requirements: []
|
|
286
|
-
rubygems_version:
|
|
286
|
+
rubygems_version: 4.0.4
|
|
287
287
|
specification_version: 4
|
|
288
288
|
summary: Optical mark recognition of multiple-choice tests and surveys
|
|
289
289
|
test_files: []
|