libs.extract_ROI

Utilities behind select_rois(): autodetection of rectangles, residual finding, CLI glue, and the OpenCV selection widget.

formhtr.libs.extract_ROI.autodect.extract_framebox(image)[source]

Detect the main framebox of the template. Used to avoid rectangle detection outside this area.

Parameters:

image (Image) – given image

Returns:

coordinates of the framebox

Return type:

list

formhtr.libs.extract_ROI.autodect.detect_rectangles(image, filter_scale)[source]

Automatically detect rectangle within given image. These can be then used as potential candidates for ROIs.

Parameters:
  • image (Image) – image object

  • filter_scale (float) – scaling factor to filter out too small rectangles (recommended value between 1 and 3)

Returns:

list of discovered rectangles

Return type:

list

formhtr.libs.extract_ROI.autodect.find_residuals(image, credentials)[source]

Detect printed text via Google Vision and convert hits to Residual objects.

Parameters:
  • image – Template page as numpy array (RGB/BGR).

  • credentials – Path to Google service-account JSON (passed to GoogleVision).

Returns:

List of Residual instances for config to_ignore entries.

formhtr.libs.extract_ROI.cli_inputs.process_cli(widget)[source]

Dispatch keyboard commands for SelectROIsWidget.

Parameters:

widget – Instance with undo_add_rectangle.

Returns:

None when the user presses q or Esc.

class formhtr.libs.extract_ROI.select_ROIs_widget.SelectROIsWidget(image, config, display_residuals)[source]

Bases: object

Widget to draw ROIs on the given template.

process_events(event, x, y, *kwargs)[source]

React to mouse events.

draw_existing_rectangles()[source]

Include existing regions into the image.

reset_image()[source]

Start again with empty image.

undo_add_rectangle()[source]

Remove last region (undo).