Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
zesje
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Works on my machine
zesje
Commits
adc2a1f3
Commit
adc2a1f3
authored
5 years ago
by
RABijl
Browse files
Options
Downloads
Plain Diff
merges latest develop
parents
4b349575
6f5fdfdd
Branches
mc-checkbox-exam-api
Branches containing commit
No related tags found
1 merge request
!28
combine precise positioning with pregrading
Pipeline
#18359
passed
5 years ago
Stage: build
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
tests/test_scans.py
+1
-1
1 addition, 1 deletion
tests/test_scans.py
zesje/scans.py
+1
-99
1 addition, 99 deletions
zesje/scans.py
with
2 additions
and
100 deletions
tests/test_scans.py
+
1
−
1
View file @
adc2a1f3
...
...
@@ -183,7 +183,7 @@ def apply_scan(img, rotation=0, scale=1, skew=(0, 0)):
def
upscale_image
(
image
,
scale
=
2
):
width
,
height
=
image
.
size
# standard
ly, the image has a
dpi
of
72 so 72*2=144 dpi w
hen using
the standard scale
#
the
standard dpi
is
72 so 72*2=144 dpi w
ith
the standard scale
.
new_size
=
(
int
(
scale
*
width
),
int
(
scale
*
height
))
image
=
image
.
resize
(
new_size
,
resample
=
1
)
return
image
...
...
This diff is collapsed.
Click to expand it.
zesje/scans.py
+
1
−
99
View file @
adc2a1f3
...
...
@@ -468,104 +468,6 @@ def decode_barcode(image, exam_config):
raise
RuntimeError
(
"
No barcode found.
"
)
def
rotate_image
(
image_array
,
corner_keypoints
):
"""
Rotate an image according to the rotation of the corner markers.
"""
# Find two corner markers which lie in the same horizontal half.
# Same horizontal half is chosen as the line from one keypoint to
# the other shoud be 0. To get corner markers in the same horizontal half,
# the pair with the smallest distance is chosen.
distances
=
[(
a
,
b
,
math
.
hypot
(
a
[
0
]
-
b
[
0
],
a
[
1
]
-
b
[
1
]))
for
(
a
,
b
)
in
list
(
itertools
.
combinations
(
corner_keypoints
,
2
))]
distances
.
sort
(
key
=
lambda
tup
:
tup
[
2
],
reverse
=
True
)
best_keypoint_combination
=
distances
.
pop
()
(
coords1
,
coords2
,
dist
)
=
best_keypoint_combination
# If the angle is downward, we have a negative angle and
# we want to rotate it counterclockwise
# However warpaffine needs a positve angle if
# you want to rotate it counterclockwise
# So we invert the angle retrieved from calc_angle
angle_deg
=
-
1
*
calc_angle
(
coords1
,
coords2
)
angle_rad
=
math
.
radians
(
angle_deg
)
h
,
w
,
*
_
=
image_array
.
shape
rot_origin
=
(
w
/
2
,
h
/
2
)
keyp_from_rot_origin
=
[(
coord_x
-
rot_origin
[
0
],
coord_y
-
rot_origin
[
1
])
for
(
coord_x
,
coord_y
)
in
corner_keypoints
]
after_rot_keypoints
=
[((
coord_y
*
math
.
sin
(
angle_rad
)
+
coord_x
*
math
.
cos
(
angle_rad
)
+
rot_origin
[
0
]),
(
coord_y
*
math
.
cos
(
angle_rad
)
-
coord_x
*
math
.
sin
(
angle_rad
))
+
rot_origin
[
1
])
for
(
coord_x
,
coord_y
)
in
keyp_from_rot_origin
]
# Create rotation matrix and rotate the image around the center
rot_mat
=
cv2
.
getRotationMatrix2D
(
rot_origin
,
angle_deg
,
1
)
rot_image
=
cv2
.
warpAffine
(
image_array
,
rot_mat
,
(
w
,
h
),
cv2
.
BORDER_CONSTANT
,
borderMode
=
cv2
.
BORDER_CONSTANT
,
borderValue
=
(
255
,
255
,
255
))
return
(
rot_image
,
after_rot_keypoints
)
def
shift_image
(
image
,
corner_keypoints
):
"""
Roll the image such that QR occupies coords
specified by the template.
"""
corner_keypoints
=
np
.
array
(
corner_keypoints
)
h
,
w
,
*
_
=
image
.
shape
xkeypoints
=
np
.
array
([
keypoint
[
0
]
for
keypoint
in
corner_keypoints
])
ykeypoints
=
np
.
array
([
keypoint
[
1
]
for
keypoint
in
corner_keypoints
])
is_left_half
=
xkeypoints
<
(
w
/
2
)
is_top_half
=
ykeypoints
<
(
h
/
2
)
# Get pixel locations to translate to. Currently only works with A4 sized
# paper
# TODO Add US letter functionality
x0
=
10
/
210
*
w
y0
=
10
/
297
*
h
# If there is a keypoint in the topleft, take that point as starting point
# for the translation
topleft
=
corner_keypoints
[
is_left_half
&
is_top_half
]
if
(
len
(
topleft
)
==
1
):
x
=
topleft
[
0
][
0
]
y
=
topleft
[
0
][
1
]
else
:
# If there is no keypoint in the topleft, try to check if there is one
# in the bottom left and bottom right. If so, infer the topleft
# coordinates from their coordinates
topright
=
corner_keypoints
[
~
is_left_half
&
is_top_half
]
bottomleft
=
corner_keypoints
[
is_left_half
&
~
is_top_half
]
if
(
len
(
topright
)
==
1
&
len
(
bottomleft
)
==
1
):
x
=
bottomleft
[
0
][
0
]
y
=
topright
[
0
][
1
]
else
:
# We can only end here if something went wrong with the detection
# of corner markers. If so, just don't shift at all.
x
=
x0
y
=
y0
shift
=
np
.
round
((
y0
-
y
,
x0
-
x
)).
astype
(
int
)
shifted_image
=
np
.
roll
(
image
,
shift
[
0
],
axis
=
0
)
shifted_image
=
np
.
roll
(
shifted_image
,
shift
[
1
],
axis
=
1
)
# Workaround of https://github.com/python-pillow/Pillow/issues/3109
if
shifted_image
.
dtype
==
bool
:
shifted_image
=
shifted_image
*
np
.
uint8
(
255
)
return
shifted_image
def
guess_student
(
exam_token
,
copy_number
,
app_config
=
None
,
force
=
False
):
"""
Update a submission with a guessed student number.
...
...
@@ -816,7 +718,7 @@ def realign_image(image_array, keypoints=None,
New keypoints properly aligned.
"""
if
(
not
keypoints
):
if
(
not
keypoints
):
keypoints
=
find_corner_marker_keypoints
(
image_array
)
check_corner_keypoints
(
image_array
,
keypoints
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment