formpy.utils package

Submodules

formpy.utils.img_processing module

exception formpy.utils.img_processing.ImageAlignmentError

Bases: Exception

formpy.utils.img_processing.align_page(img: np.ndarray, corner_pts: np.ndarray | None = None) np.ndarray

Applys perspective transform to align the image using a rectangle alignment feature on the image

Parameters
  • img (np.ndarray) – image of form or template read into array

  • cv2.imread() (e.g. via) –

  • corner_pts (np.ndarray | None, optional) – 4 coordinates of the corners

  • feature (of the rectangle alignment) –

:param : :param ordered from top-left clockwise. Defaults to None: :param and the rectangle: :param feature will be detected automatically.:

Returns

aligned image

Return type

np.ndarray

formpy.utils.img_processing.get_outer_box(img: numpy.ndarray) numpy.ndarray

Finds the rectangle alignment feature in the image

Parameters

img (np.ndarray) – image to detect the outer box from

Raises

ImageAlignmentError – if outer box is not detected

Returns

returns the 4 coordinates of the corners of the rectangle alignment feature, ordered from top-left clockwise

Return type

np.ndarray

formpy.utils.img_processing.get_perspective_matrix(ordered_corner_pts: numpy.ndarray) numpy.ndarray

Calculates a matrix from the corners of the aligment feature

Parameters
  • ordered_corner_pts (np.ndarray) – Corner points of rectangle alignment

  • feature

:param : :param ordered from top-left clockwise:

Returns

perspective matrix to use to align page using cv2.getPerspectiveTransform()

Return type

np.ndarray

formpy.utils.img_processing.process_img(img: numpy.ndarray) numpy.ndarray

Converts image to binary black & white and aligns the page using the rectangle alignment feature

Parameters

img (np.ndarray) – image read into array e.g. via cv2.imread()

Returns

binary black and white, aligned image

Return type

np.ndarray

formpy.utils.img_processing.show_img(img: np.ndarray | list[np.ndarray], time: int = 0) None

Utility to display all images in a window with window name = window(i) where i = index

Parameters
  • img (np.ndarray | list[np.ndarray]) – image to display as

  • cv2.imread() (array read into array e.g. via) –

  • time (int, optional) – Delay time in millisec to display window.

  • 0. (Defaults to) –

formpy.utils.img_processing.thresh_img(img: numpy.ndarray, min_thresh: int = 100, max_thresh: int = 255) numpy.ndarray

Convert image to binary black and white image using thresholds

Parameters
  • img (np.ndarray) – image to threshold read into array

  • cv2.imread() (e.g. via) –

  • min_thresh (int, optional) – Pixels below this grayscale value will be

  • 100. (converted to white pixels . Defaults to) –

  • max_thresh (int, optional) – Pixels above this grayscale value will be

  • 255. (converted to black pixels. Defaults to) –

Returns

thresholded image with only black or white pixels

Return type

np.ndarray

formpy.utils.template_definition module

formpy.utils.template_definition.find_spots(img: np.array, max_radius: int = 35, min_radius: int = 10) list[list[int]]

Use contour detection to find all circles that may correspond to answers on a template. Min and max radius and the aspect ratio of between 0.9 - 1.1 are used to determine if the detected contour is an answer

Parameters
  • img (np.array) – image of template read into array e.g. via cv2.imread()

  • the (This should be a template page with all the answers filled in so) –

  • detected (coordinates can be) –

  • max_radius (int, optional) – max threshold used to determine if the

  • 35. (contour is a answer circle. Defaults to) –

  • min_radius (int, optional) – min threshold used to determine if the

  • 10. (contour is a answer circle. Defaults to) –

Returns

sorted list of [x,y] coordinates representing the centre of the answer circle. Sorted by x first, then y

Return type

list[list[int]]

Module contents