libs.processing
Image alignment, per-ROI interpretation, spatial queries, and writing results. Used by
extract_logsheet() and related export paths.
- formhtr.libs.processing.align_images.validate_corners(corners, height, width, tol=20)[source]
Check that four corners form a plausible document quad.
- Parameters:
corners – Four
(x, y)points in order TL, TR, BR, BL.height – Image height in pixels.
width – Image width in pixels.
tol – Maximum relative margin (percent of image) for side length mismatch.
- Returns:
Trueif opposite sides match within tolerance.
- formhtr.libs.processing.align_images.compute_closest_point(point, corners)[source]
Pick the corner nearest to a reference image corner.
- Parameters:
point – Reference
(x, y).corners – Candidate corner coordinates.
- Returns:
The closest corner from
corners.
- formhtr.libs.processing.align_images.find_corners(image, filter_grayscale, num=10, gray_filter=20)[source]
Detect outer document corners via contours and min-area rectangles.
- Parameters:
image – BGR
numpyimage.filter_grayscale – If True, threshold grayscale before edge detection.
num – Number of largest contours to consider (increased recursively on failure).
gray_filter – Threshold value when
filter_grayscaleis True.
- Returns:
(outer_corners, valid)whereouter_cornersare four(x, y)tuples andvalidreflectsvalidate_corners(may recurse with largernum).
- formhtr.libs.processing.align_images.transform(scanned, template, scanned_points, template_points)[source]
Apply a homography mapping scan corners to template corners.
- Parameters:
scanned – Source BGR image.
template – Target image defining output size.
scanned_points – Four scan corner coordinates.
template_points – Four template corner coordinates.
- Returns:
Warped
scannedwith template dimensions.
- formhtr.libs.processing.align_images.align_images(scanned, template, filter_grayscale)[source]
Align
scannedtotemplateusing automatically detected corners.- Parameters:
scanned – Scanned page BGR image.
template – Template BGR image.
filter_grayscale – Passed to
find_cornersfor the scan and template.
- Returns:
Warped scan, or
Noneif corners are invalid for either image.
- formhtr.libs.processing.align_images.format_point(point)[source]
Serialize a corner to JSON-friendly ints.
- Parameters:
point –
(x, y)numeric pair.- Returns:
Dict with
xandykeys.
- formhtr.libs.processing.align_images.get_alignment_data(scanned, template)[source]
Compute template and scan corner sets for external alignment UIs.
- Parameters:
scanned – Scanned BGR image.
template – Template BGR image.
- Returns:
Dict with
templatePointsandtargetPointslists of{"x","y"}dicts.
- formhtr.libs.processing.barcode.extract_barcode(candidates)[source]
Pick a barcode string from per-service single-word OCR fallbacks.
- Parameters:
candidates – Dict mapping provider name to a list of
Rectangle(or empty).- Returns:
Most frequent barcode string among singleton lists, or
Noneif none.
- formhtr.libs.processing.barcode.read_barcode(image, candidates)[source]
Decode a barcode from a ROI image, with rotation retry and OCR fallback.
- Parameters:
image – ROI crop as
numpyBGR array.candidates – Per-service OCR rectangles (used if decoders fail).
- Returns:
Decoded payload string, or
Noneif nothing is found.
- formhtr.libs.processing.checkbox.is_ticked(image_array, edge_ignore_percentage=0.2, threshold_percentage=0.1)[source]
Detected whether checkbox is ticked
- formhtr.libs.processing.process_area.is_a_number(string)[source]
Polish given string and check if can be converted to float.
- formhtr.libs.processing.process_area.identify_number(values)[source]
If some number were found, give them priority
Otherwise we will handle the region is general text area
- formhtr.libs.processing.process_area.separate_to_lines(rectangles)[source]
Split set of rectangles into lines.
This is determined by center of rectangle being inside of previous rectangle bounds.
- formhtr.libs.processing.process_area.get_max_dimensions(candidates)[source]
Identify dimensions of identified text used to determine its size
- formhtr.libs.processing.process_area.majority_vote(strings)[source]
Vote on individual positions of identified words
- formhtr.libs.processing.process_area.remove_non_ascii(string)[source]
Remove non-ascii characters (these do not work in the alignment)
- formhtr.libs.processing.process_area.identify_words(lines, is_number)[source]
Identify words from lines. Behaves differently based on how many lines there are.
- formhtr.libs.processing.process_area.filter_exceeding_words(lines, roi)[source]
Filter regions exceeding bounds of ROI
There are three cases: 1. None of the regions exceeds the bounds 2. Some of them 3. All of them
We keep everything as is in cases 1. and 3., in case 2. we filter out the exceeding ones (as at least one of the services thinks the exceeding part does not belong here)
- formhtr.libs.processing.process_area.process_lines(lines, roi, is_number)[source]
Join lines to words let majority voting decide
- formhtr.libs.processing.process_area.align_lines(candidate_lines)[source]
Group lines to categories by y-coordinate
Also sort them by y-coordinate to ensure correct order.
- formhtr.libs.processing.process_area.general_text_area(candidates, roi, is_number)[source]
Process text area
- formhtr.libs.processing.read_content.process_content(indetified_content, logsheet_image, config, checkbox_edges)[source]
Fill each configured ROI using OCR unions, barcodes, or checkbox heuristics.
- Parameters:
indetified_content – Dict
google/amazon/azure-> word lists orNone.logsheet_image – Full-page aligned image as
numpyarray.config –
LogsheetConfigwithregionsandresiduals.checkbox_edges – Fraction of ROI border to ignore when scoring checkbox ink.
- Returns:
(results, artefacts)whereresultsis a list of[varname, content_dict, fragment]andartefactsmaps each service to leftover OCR snippets not assigned to ROIs.
- formhtr.libs.processing.store_results.order_results(values)[source]
Collect non-empty per-provider values in column order.
- Parameters:
values – Dict with optional keys
inferred,google,amazon,azure.- Returns:
List of truthy values in fixed key order.
- formhtr.libs.processing.store_results.write_header(worksheet)[source]
Write the main metadata sheet column titles.
- Parameters:
worksheet –
xlsxwriterworksheet for the Metadata tab.- Returns:
None.
- formhtr.libs.processing.store_results.store_image(image, location, index)[source]
Temporarily store image
- formhtr.libs.processing.store_results.store_results(results, artefacts, output_file, include_validation=False)[source]
Write ROI results and artefact crops into an XLSX workbook.
- Parameters:
results – List of
[varname, value_dict, crop_numpy]rows.artefacts – Dict mapping service name to
[text, crop_numpy]lists.output_file – Path to the
.xlsxfile to create.include_validation – If True, add Excel data validation where applicable.
- Returns:
None. Temporary PNG crops next tooutput_fileare removed after close.
- formhtr.libs.processing.store_results.store_results_csv(results, artefacts, output_file)[source]
Write variable names and inferred values to UTF-8 CSV (no images).
- Parameters:
results – List of
[varname, value_dict, crop](crop ignored).artefacts – Unused; kept for API symmetry with
store_results.output_file – Path to the
.csvfile to create.
- Returns:
None.