Modify this code to check if any entity that is present in json has received thier…
- User
Modify this code to check if any entity that is present in json has received thier respective bounding box or not. If not then try to find their bounding boxes. Any entity that is present in json should have its bounding box. And only process these sections "invoice_details", "Payment Details" and "amounts_and_tax" from json
import json import pandas as pd import cv2 from thefuzz import fuzz from thefuzz import process
def preprocess_entity(entity): """Preprocess entities by removing commas in numerical values.""" return entity.replace(",", "").strip()
def is_nearby(current_box, next_box, max_line_difference=10, max_distance=100): """Check if the next bounding box is nearby to the current one. Both should be on the same line or the next line and within a certain distance.""" same_line = abs(current_box["top"] - next_box["top"]) <= max_line_difference close_enough = abs(current_box["right"] - next_box["left"]) <= max_distance return same_line and close_enough
def merge_boxes(boxes): """Merge bounding boxes of a multi-token entity.""" if not boxes: return None 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 process_single_token_entity(dataframe, entity, threshold=85): """Process a single token entity to find its bounding box using fuzzy matching.""" best_score = 0 best_box = None entity = preprocess_entity(entity)
for _, row in dataframe.iterrows(): ocr_text = preprocess_entity(row['text']) score = fuzz.ratio(entity, ocr_text) if score > best_score and score > threshold: best_score = score best_box = { "left": row["left"], "right": row["right"], "top": row["top"], "bottom": row["bottom"] } return best_boxdef process_multi_token_entity(dataframe, entity, threshold=85): """Process a multi-token entity to find and merge its bounding boxes using fuzzy matching.""" tokens = entity.split() boxes = [] for token in tokens: box = process_single_token_entity(dataframe, token, threshold) if box is None: return None # Unable to find all tokens boxes.append(box)
# Verify if boxes are near to each other if all(is_nearby(boxes[i], boxes[i + 1]) for i in range(len(boxes) - 1)): return merge_boxes(boxes) else: return Nonedef draw_bounding_boxes(image_path, bounding_boxes): """Draw bounding boxes on the image.""" image = cv2.imread(image_path) for box in bounding_boxes: if box is not None: top_left = (box["left"], box["top"]) bottom_right = (box["right"], box["bottom"]) cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2) cv2.imwrite("/home/ritik1s/Desktop/bbox_issues/temp_GPT/output_image_15_march.jpg", image)
def main(json_path, csv_path, image_path): with open(json_path, 'r') as f: data = json.load(f)
- Assistant
- User
Write down entity name over their bounding box
- Assistant
- User
In process_multi_token_entity do not use process_single_token_entity function to get bounding box write completly different logic to process multi token entity and calculate distance and take only nearby tokens only
- Assistant
- User
Handle all these cases in the code This implementation provides a basic framework but has simplifications. Specifically, is_nearby_sequence checks for any nearby sequence but does not return the specific sequence found. In a more comprehensive solution, you’d need to track and return the specific tokens (and their boxes) forming the nearby sequence, then merge those.
Note that this code snippet simplifies some aspects of the problem:
- It assumes the first potential match for each token is part of the correct sequence, which may not always be the case. You might need a way to identify and select the correct sequence of matches amongst the potential matches, especially if tokens have multiple nearby matches.
- Handling sequences properly when tokens can appear in multiple nearby locations or when the document contains repeated similar tokens/phrases requires more sophisticated logic.
- Assistant
- User
Optimize the code for these cases - Introducing a more complex proximity scoring system that accounts for vertical and horizontal distances more precisely.
- Optimizing the search for the best sequence through techniques like dynamic programming if applicable.
- Assistant
- User
Please give the complete final code
- Assistant
- User
Where is the code to handle single token entity
- Assistant
- User
Give the complete final code
- Assistant
- User
If any two entity have exact same values then this code is returning bounding box only at one place please modify the code in such way that if any two entity same value it should return bounding box for both entity.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.