Create one class named "BoundingBoxFInder" and cover all the function in that class…
- User
Create one class named "BoundingBoxFInder" and cover all the function in that class
import cv2 import pandas as pd import json from thefuzz import fuzz from itertools import product used_bounding_boxes = {}
def preprocess_entity(entity): try: token = entity.replace(",", "").strip() return token except: pass
def calculate_proximity_score(box_a, box_b): vertical_overlap = max(0, min(box_a["bottom"], box_b["bottom"]) - max(box_a["top"], box_b["top"])) vertical_distance = 0 if vertical_overlap > 0 else min(abs(box_a["top"] - box_b["bottom"]), abs(box_a["bottom"] - box_b["top"])) horizontal_overlap = max(0, min(box_a["right"], box_b["right"]) - max(box_a["left"], box_b["left"])) horizontal_distance = 0 if horizontal_overlap > 0 else abs(box_a["right"] - box_b["left"]) return horizontal_distance + 2 * vertical_distance
def is_nearby(box_a, box_b, max_line_difference=5, max_distance=100): return calculate_proximity_score(box_a, box_b) <= max_distance + 2 * max_line_difference
def merge_boxes(boxes): min_left = min(box["left"] for box in boxes) max_right = max(box["right"] for box in boxes) min_top = min(box["top"] for box in boxes) max_bottom = max(box["bottom"] for box in boxes) return {"left": min_left, "right": max_right, "top": min_top, "bottom": max_bottom}
def find_potential_matches(dataframe, token, threshold=75): potential_matches = [] for _, row in dataframe.iterrows(): ocr_text = preprocess_entity(row["text"]) score = fuzz.ratio(token, ocr_text) if score > threshold: potential_matches.append({ "box": {"left": row["left"], "right": row["right"], "top": row["top"], "bottom": row["bottom"]}, "score": score }) return potential_matches
def find_best_sequence_heuristic(matches_list): if not matches_list or len(matches_list[0]) == 0: return [] best_sequence = [min(matches_list[0], key=lambda match: match["score"])] for next_matches in matches_list[1:]: current_box = best_sequence[-1]["box"] next_best_match = min(next_matches, key=lambda match: calculate_proximity_score(current_box, match["box"])) best_sequence.append(next_best_match) return best_sequence
def process_single_token_entity(dataframe, entity, threshold=75): global used_bounding_boxes best_match = None best_score = threshold entity = preprocess_entity(entity)
if entity not in used_bounding_boxes: used_bounding_boxes[entity] = [] for _, row in dataframe.iterrows(): ocr_text = preprocess_entity(row['text']) score = fuzz.ratio(entity, ocr_text) current_box = {'left': row['left'], 'right': row['right'], 'top': row['top'], 'bottom': row['bottom']} if score > best_score and current_box not in used_bounding_boxes[entity]: best_score = score best_match = current_box if best_match: used_bounding_boxes[entity].append(best_match) return best_matchdef box_overlap(box1, box2): """Check if there’s any overlap in any coordinate between two boxes.""" return box1["left"] == box2["left"] or box1["right"] == box2["right"]
- Assistant
- User
return the complete code
- Assistant