From e9a90c013f3bedba895646c70ebb1f9e631f0399 Mon Sep 17 00:00:00 2001 From: Ruben Young On <r.d.youngon@student.tudelft.nl> Date: Sat, 25 May 2019 14:03:37 +0200 Subject: [PATCH] fix_corner_markers now returns the top left point --- tests/test_three_corners.py | 8 +++++--- zesje/images.py | 38 ++++++++++++++++++------------------- zesje/pregrader.py | 7 ++----- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/tests/test_three_corners.py b/tests/test_three_corners.py index 3305cde0..1169be12 100644 --- a/tests/test_three_corners.py +++ b/tests/test_three_corners.py @@ -10,18 +10,20 @@ def test_three_straight_corners_1(): shape = (240, 200, 3) corner_markers = [(50, 50), (120, 50), (50, 200)] - corner_markers = fix_corner_markers(corner_markers, shape) + top_left, corner_markers = fix_corner_markers(corner_markers, shape) assert (120, 200) in corner_markers + assert top_left == (50, 50) def test_three_straight_corners_2(): shape = (240, 200, 3) corner_markers = [(120, 50), (50, 200), (120, 200)] - corner_markers = fix_corner_markers(corner_markers, shape) + top_left, corner_markers = fix_corner_markers(corner_markers, shape) assert (50, 50) in corner_markers + assert top_left == (50, 50) def test_pdf(datadir): @@ -46,7 +48,7 @@ def test_pdf(datadir): diff = [corner for corner in corners1 if corner not in corners2] diff_marker = min(diff) - fixed_corners2 = fix_corner_markers(corners2, page_img.shape) + _, fixed_corners2 = fix_corner_markers(corners2, page_img.shape) added_marker = [corner for corner in fixed_corners2 if corner not in corners2][0] # Check if 'inferred' corner marker is not too far away diff --git a/zesje/images.py b/zesje/images.py index df61de90..06eef077 100644 --- a/zesje/images.py +++ b/zesje/images.py @@ -45,14 +45,17 @@ def fix_corner_markers(corner_keypoints, shape): Parameters ---------- - corner_keypoints: list of corner marker locations as tuples - dpi: the dpi of the image in which the corner markers are from - image_format: either A4 or US letter + corner_keypoints : + List of corner marker locations as tuples + shape : + Shape of the image in (x, y, dim) Returns ------- - A list of four corner markers. - + corner_keypoints : + A list of four corner markers. + top_left : tuple + Coordinates of the top left corner marker """ if len(corner_keypoints) == 4 or len(corner_keypoints) < 3: @@ -66,33 +69,30 @@ def fix_corner_markers(corner_keypoints, shape): top_right = [(x, y) for x, y in corner_keypoints if x > x_sep and y < y_sep] bottom_right = [(x, y) for x, y in corner_keypoints if x > x_sep and y > y_sep] + missing_point = () + if not top_left: # Top left point is missing (dx, dy) = tuple(map(sub, top_right[0], bottom_right[0])) - top_left = tuple(map(add, bottom_left[0], (dx, dy))) - - return corner_keypoints + [top_left] + missing_point = tuple(map(add, bottom_left[0], (dx, dy))) + top_left = [missing_point] - if not bottom_left: + elif not bottom_left: # Bottom left point is missing (dx, dy) = tuple(map(sub, top_right[0], bottom_right[0])) - bottom_left = tuple(map(sub, top_left[0], (dx, dy))) + missing_point = tuple(map(sub, top_left[0], (dx, dy))) - return corner_keypoints + [bottom_left] - - if not top_right: + elif not top_right: # Top right point is missing (dx, dy) = tuple(map(sub, top_left[0], bottom_left[0])) - top_right = tuple(map(add, bottom_right[0], (dx, dy))) - - return corner_keypoints + [top_right] + missing_point = tuple(map(add, bottom_right[0], (dx, dy))) - if not bottom_right: + elif not bottom_right: # bottom right (dx, dy) = tuple(map(sub, top_left[0], bottom_left[0])) - bottom_right = tuple(map(sub, top_right[0], (dx, dy))) + missing_point = tuple(map(sub, top_right[0], (dx, dy))) - return corner_keypoints + [bottom_right] + return top_left[0], corner_keypoints + [missing_point] def box_is_filled(image_array, box_coords, padding=0.3, threshold=150, pixels=False): diff --git a/zesje/pregrader.py b/zesje/pregrader.py index c5907ddc..37c3535c 100644 --- a/zesje/pregrader.py +++ b/zesje/pregrader.py @@ -22,11 +22,8 @@ def add_feedback_to_solution(sub, exam, page, page_img, corner_keypoints): """ problems_on_page = [problem for problem in exam.problems if problem.widget.page == page] - fixed_corner_keypoints = fix_corner_markers(corner_keypoints, page_img.shape) - - # TODO: It is not exactly known which corner marker is the top left one - # TODO: Also, what do if less than 3 keypoints are found? - top_left_point = sorted(fixed_corner_keypoints, key=lambda x: x[0])[0] if fixed_corner_keypoints else [] + # TODO: What if less than 3 keypoints are found? + top_left_point, fixed_corner_keypoints = fix_corner_markers(corner_keypoints, page_img.shape) for problem in problems_on_page: sol = Solution.query.filter(Solution.problem_id == problem.id, Solution.submission_id == sub.id).one_or_none() -- GitLab