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
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
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
Réouven ASSOULY
zesje
Commits
192cefbc
Commit
192cefbc
authored
6 years ago
by
Nick Cleintuar
Browse files
Options
Downloads
Patches
Plain Diff
Added new test, cleaned up code
parent
5f906ff6
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/helpers/test_rotation_scan.py
+67
-19
67 additions, 19 deletions
tests/helpers/test_rotation_scan.py
with
67 additions
and
19 deletions
tests/helpers/test_rotation_scan.py
+
67
−
19
View file @
192cefbc
...
...
@@ -4,40 +4,88 @@ import PIL
import
os
import
math
import
numpy
as
np
from
zesje.helpers
import
image_helper
,
pdf_helper
from
zesje.helpers
import
image_helper
# Helper functions
# Given 2 keypoints, calculates the distance between them
def
distance
(
keyp1
,
keyp2
):
return
math
.
hypot
(
keyp1
.
pt
[
0
]
-
keyp2
.
pt
[
0
],
keyp1
.
pt
[
1
]
-
keyp2
.
pt
[
1
])
# Given a name of a exam image and the location it is stored, retrieves the
# image and converts it to binary image
def
generate_binary_image
(
name
,
datadir
):
pdf_path
=
os
.
path
.
join
(
datadir
,
'
scanned_pdfs
'
,
f
'
{
name
}
'
)
pil_im
=
PIL
.
Image
.
open
(
pdf_path
)
opencv_im
=
cv2
.
cvtColor
(
np
.
array
(
pil_im
),
cv2
.
COLOR_RGB2BGR
)
_
,
bin_im
=
cv2
.
threshold
(
opencv_im
,
150
,
255
,
cv2
.
THRESH_BINARY
)
return
bin_im
# Tests
@pytest.mark.parametrize
(
'
test_input1, test_input2, expected
'
,
[
(
cv2
.
KeyPoint
(
1337
,
69
,
0
),
cv2
.
KeyPoint
(
9001
,
69
,
0
),
0
),
(
cv2
.
KeyPoint
(
0
,
100
,
0
),
cv2
.
KeyPoint
(
0
,
1000
,
0
),
90
),
(
cv2
.
KeyPoint
(
25
,
25
,
0
),
cv2
.
KeyPoint
(
50
,
50
,
0
),
-
45
),
(
cv2
.
KeyPoint
(
25
,
25
,
0
),
cv2
.
KeyPoint
(
50
,
0
,
0
),
45
)],
ids
=
[
'
Same horizontal line
'
,
'
Same vertical line
'
,
'
Negative angle
'
,
'
Positive angle
'
]
)
# Tests whether the output of calc angle is correct
@pytest.mark.parametrize
(
'
test_input1, test_input2, expected
'
,
[
(
cv2
.
KeyPoint
(
1337
,
69
,
0
),
cv2
.
KeyPoint
(
9001
,
69
,
0
),
0
),
(
cv2
.
KeyPoint
(
0
,
100
,
0
),
cv2
.
KeyPoint
(
0
,
1000
,
0
),
90
),
(
cv2
.
KeyPoint
(
25
,
25
,
0
),
cv2
.
KeyPoint
(
50
,
50
,
0
),
-
45
),
(
cv2
.
KeyPoint
(
25
,
25
,
0
),
cv2
.
KeyPoint
(
50
,
0
,
0
),
45
)],
ids
=
[
'
Same horizontal line
'
,
'
Same vertical line
'
,
'
Negative angle
'
,
'
Positive angle
'
])
def
test_calc_angle
(
test_input1
,
test_input2
,
expected
):
assert
math
.
isclose
(
image_helper
.
calc_angle
(
test_input1
,
test_input2
),
expected
,
abs_tol
=
0.1
)
assert
math
.
isclose
(
image_helper
.
calc_angle
(
test_input1
,
test_input2
),
expected
,
abs_tol
=
0.1
)
@pytest.mark.parametrize
(
'
name
'
,
os
.
listdir
(
os
.
path
.
join
(
'
tests
'
,
'
data
'
,
'
scanned_pdfs
'
)),
ids
=
os
.
listdir
(
os
.
path
.
join
(
'
tests
'
,
'
data
'
,
'
scanned_pdfs
'
)))
# Tests whether the amount of cornermakers is enough to calculate the angle and
# whether it is lower than 5 as we only add 4 corner markers per page.
@pytest.mark.parametrize
(
'
name
'
,
os
.
listdir
(
os
.
path
.
join
(
'
tests
'
,
'
data
'
,
'
scanned_pdfs
'
)),
ids
=
os
.
listdir
(
os
.
path
.
join
(
'
tests
'
,
'
data
'
,
'
scanned_pdfs
'
)))
def
test_detect_enough_cornermarkers
(
name
,
datadir
):
bin_im
=
generate_binary_image
(
name
,
datadir
)
keypoints
=
image_helper
.
find_corner_marker_keypoints
(
bin_im
)
assert
(
len
(
keypoints
)
>=
2
&
len
(
keypoints
)
<=
4
)
def
test_detect_enough_cornermarkers
(
name
,
datadir
):
pdf_path
=
os
.
path
.
join
(
datadir
,
'
scanned_pdfs
'
,
f
'
{
name
}
'
)
pil_im
=
PIL
.
Image
.
open
(
pdf_path
)
# Tests whether the detected keypoints are actually corner markers.
# This is done by checking whether they are close enough to the corner
# of the image. Only A4 is considered as there is no test data yet for
# US letter size.
@pytest.mark.parametrize
(
'
name
'
,
os
.
listdir
(
os
.
path
.
join
(
'
tests
'
,
'
data
'
,
'
scanned_pdfs
'
)),
ids
=
os
.
listdir
(
os
.
path
.
join
(
'
tests
'
,
'
data
'
,
'
scanned_pdfs
'
)))
def
test_detect_valid_cornermarkers
(
name
,
datadir
):
bin_im
=
generate_binary_image
(
name
,
datadir
)
keypoints
=
image_helper
.
find_corner_marker_keypoints
(
bin_im
)
opencv_im
=
cv2
.
cvtColor
(
np
.
array
(
pil_im
),
cv2
.
COLOR_RGB2BGR
)
h
,
w
,
*
_
=
bin_im
.
shape
(
xmm
,
ymm
)
=
(
210
,
297
)
(
xcorner
,
ycorner
)
=
(
round
(
30
*
w
/
xmm
),
round
(
30
*
h
/
ymm
))
maxdist
=
math
.
hypot
(
xcorner
,
ycorner
)
_
,
bin_im
=
cv2
.
threshold
(
opencv_im
,
150
,
255
,
cv2
.
THRESH_BINARY
)
cornerlist
=
[
cv2
.
KeyPoint
(
0
,
0
,
0
),
cv2
.
KeyPoint
(
w
,
0
,
0
),
cv2
.
KeyPoint
(
0
,
h
,
0
),
cv2
.
KeyPoint
(
w
,
h
,
0
)]
keypoints
=
image_helper
.
find_corner_marker_keypoints
(
bin_im
)
# Checks whether there aren't multiple keypoints in the same corner.
# If there is, one of those probably isn't a corner marker.
result
=
np
.
array
([
0
,
0
,
0
,
0
])
for
detected_keypoint
in
keypoints
:
distlist
=
np
.
array
([
distance
(
detected_keypoint
,
corner_keypoint
)
for
corner_keypoint
in
cornerlist
])
binlist
=
distlist
<
maxdist
result
=
binlist
+
result
assert
(
sum
(
result
>
1
)
==
0
)
assert
(
len
(
keypoints
)
>=
2
&
len
(
keypoints
)
<=
4
)
# Untested:
#
# Other parts of the rotation function are not tested due to it being opencv
# functions.
#
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