formpy package

Subpackages

Submodules

formpy.answer module

class formpy.answer.Answer(x: int, y: int, value: str, circle_radius: int, filled_threshold: float = 0.8)

Bases: object

A class to represent a single answer circle

calc_filled_perc(form_img: numpy.ndarray) float

Returns percentage of circle that is filled in

Parameters

form_img (np.ndarray) – _description_

Returns

range from 0.0 - 1.0 representing percentage of circle filled in

Return type

float

is_filled(form_img: numpy.ndarray) bool
mark_answer(form_img: numpy.ndarray, circle_thickness: int = - 1, colour: Tuple[int] = (0, 0, 255)) None

Marks answer on image in place

Parameters
  • form_img (np.ndarray) – image of form read into array

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

  • circle_thickness (int, optional) – thickness of circle if a hollow

  • required. (circle is) –

  • -1 (Defaults to) –

  • colour (Tuple[int], optional) – colour of circle in BGR.

  • to (Defaults) –

Returns

marks answer in place so original image is modified

Return type

None

formpy.form module

class formpy.form.Form(img: numpy.ndarray, template: formpy.template.Template)

Bases: object

A class to represent a form.

mark_all_answers(colour: Tuple[int] = (0, 0, 255)) numpy.ndarray

mark all filled in answers on the form image with the question id and answer value

Parameters
  • colour (Tuple[int], optional) – color of text and circles to mark

  • to (answers in BGR. Defaults) –

Returns

form image with marked answers, answer values and question ids

Return type

np.ndarray

formpy.question module

class formpy.question.Question(question_id: int, answers: list[Answer], multiple: bool)

Bases: object

A class to represent a Question on a form or template.

find_answers(img: np.ndarray) list[Answer]

Find marked answer(s) for question

Parameters

img (np.ndarray) – image of the form

Returns

answers that have been marked i.e. return true for answer.check_fill() if question.multiple == False then the first marked answer will be returned and length of this list will be 1.

Return type

list[Answer]

property question_img: numpy.ndarray

return cropped form image of the question

Parameters
  • form_img (np.ndarray) – form image read into array

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

Returns

cropped form image

Return type

np.ndarray

formpy.template module

class formpy.template.Template(img: np.ndarray, questions: list[Question], circle_radius: int)

Bases: object

A class to represent a template that a form is built from.

classmethod from_dict(template: dict, img_path: str) formpy.template.Template

Create template from dictionary of template config.

Parameters

template (dict) – template config as shown below.

{"config":
        {"radius":"<CIRCLE_RADIUS>"},
    "questions":
        {"question_id":
            {
                "multiple":<BOOL>,
                "answers":
                [
                    {"answer_val":"<ANSWER_VAL>",
                    "answer_coords": ["<X_COORD>", "<Y_COORD>"]}
                ]
            }
        }
    }

img_path (str): path to image of form

Returns

Template

classmethod from_img_template(img_path: str, circle_radius: int, question_assignment: dict, question_config: Optional[dict] = None) formpy.template.Template

Initialise template from img

Parameters
  • img_path (str) – path to load image of template from

  • circle_radius (int) – size of the answer circles

  • question_assignment (dict) – map of question id to list of answer

  • id.

  • question_config (dict, optional) – map of question id to true/false

  • None. (flag for multiple answers. Defaults to) –

Returns

Template

classmethod from_json(json_path: str, img_path: str) Form

Return Form instance from pre-configured JSON.

Parameters
  • img_path (str) – Path to image of template.

  • json_path (str) – Path to JSON containing configuration for form

  • template

  • below. (see format) –

{"config":
        {"radius":"<CIRCLE_RADIUS>"},
    "questions":
        {"question_id":
            {
                "multiple":<BOOL>,
                "answers":
                [
                    {"answer_val":"<ANSWER_VAL>",
                    "answer_coords": ["<X_COORD>", "<Y_COORD>"]}
                ]
            }
        }
    }
Returns

Return form instantiated from JSON config and image.

Return type

Form

property perspective_matrix: numpy.ndarray

return perspective matrix from outer box detected in image used for alignment of template

Returns

perspective matrix

Return type

np.ndarray

to_dict() dict

Convert template obj to dictionary. See docs for dictionary structure.

Returns

dictionary representation of template as below.

Return type

dict

{"config":
        {"radius":"<CIRCLE_RADIUS>"},
    "questions":
        {"question_id":
            {
                "multiple":<BOOL>,
                "answers":
                [
                    {"answer_val":"<ANSWER_VAL>",
                    "answer_coords": ["<X_COORD>", "<Y_COORD>"]}
                ]
            }
        }
    }
to_json() str

Convert template into json string representation.

Returns

json string in format below.

Return type

str

{"config":
        {"radius":"<CIRCLE_RADIUS>"},
    "questions":
        {"question_id":
            {
                "multiple":<BOOL>,
                "answers":
                [
                    {"answer_val":"<ANSWER_VAL>",
                    "answer_coords": ["<X_COORD>", "<Y_COORD>"]}
                ]
            }
        }
    }

Module contents