Skip to content
Snippets Groups Projects
Commit 23badcc7 authored by Marc-Philipp Knechtle's avatar Marc-Philipp Knechtle
Browse files

add match_tables function

parent 79f04784
Branches
No related tags found
No related merge requests found
from typing import List, Dict, Optional
from docrecjson.elements import Table
import shapely.geometry as shapely
from shapely.validation import make_valid
def match_tables(tables_to_find_candidates: List[Table], matching_tables: List[Table]) -> Dict[Table, Table]:
"""
:param: tables_to_find_candidates: these are the tables where the candidates are found from.
:param: matching_tables: These are the tables which are tried to match to the tables_to_find_candidates.
:return: a dictionary with each table in tables_to_find_candidates, with one corresponding entry if matched table
was found, else none.
"""
table: Table
matched_tables: Dict[Table, Table] = {}
for table in tables_to_find_candidates:
table_gt_area: shapely.Polygon = shapely.Polygon(table.get_table_coordinates())
matching_table_with_highest_intersection: Optional[Table] = None
highest_intersection: float = 0
table_to_match: Table
for table_to_match in matching_tables:
table_prediction_area: shapely.Polygon = shapely.Polygon(table_to_match.get_table_coordinates())
if not table_prediction_area.is_valid:
table_prediction_area = make_valid(table_prediction_area)
if table_gt_area.intersects(table_prediction_area):
intersection = table_gt_area.intersection(table_prediction_area)
if intersection.area > highest_intersection:
highest_intersection = intersection.area
matching_table_with_highest_intersection = table_to_match
matching_tables.remove(matching_table_with_highest_intersection)
matched_tables[table] = matching_table_with_highest_intersection
return matched_tables
-e .
-e shared-file-format/python
shapely
loguru
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment