Skip to content
Snippets Groups Projects
Commit adc2a1f3 authored by RABijl's avatar RABijl
Browse files

merges latest develop

parents 4b349575 6f5fdfdd
Branches mc-checkbox-exam-api
No related tags found
1 merge request!28combine precise positioning with pregrading
Pipeline #18359 passed
......@@ -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
# standardly, the image has a dpi of 72 so 72*2=144 dpi when using the standard scale
# the standard dpi is 72 so 72*2=144 dpi with the standard scale.
new_size = (int(scale * width), int(scale * height))
image = image.resize(new_size, resample=1)
return image
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment