I have a code you have to analyse the code and modify it so that we can use same code to…
- User
I have a code you have to analyse the code and modify it so that we can use same code to map line_items as well. I am sharing how line_items looks like in given json "line_items": [ { "hsn_code": "8443", "description": "Hp 1606 Pressure Roller", "unit_of_measurement_or_uom": "NOS", "unit_price": "750.00", "quantity": "3", "sgst_rate": "None", "sgst_amount": "None", "cgst_rate": "None", "cgst_amount": "None", "igst_rate": "None", "igst_amount": "None", "taxable_amount_or_subtotal": "2250.00", "total_amount_or_gross_amount_of_the_item": "None" }, { "hsn_code": "8443", "description": "Teflon 1010 (OG)", "unit_of_measurement_or_uom": "NOS", "unit_price": "800.00", "quantity": "3", "sgst_rate": "None", "sgst_amount": "None", "cgst_rate": "None", "cgst_amount": "None", "igst_rate": "None", "igst_amount": "None", "taxable_amount_or_subtotal": "2400.00", "total_amount_or_gross_amount_of_the_item": "None" }, { "hsn_code": "84439959", "description": "Pick Up Roll 1606", "unit_of_measurement_or_uom": "NOS", "unit_price": "250.00", "quantity": "3", "sgst_rate": "None", "sgst_amount": "None", "cgst_rate": "None", "cgst_amount": "None", "igst_rate": "None", "igst_amount": "None", "taxable_amount_or_subtotal": "750.00", "total_amount_or_gross_amount_of_the_item": "None" }, { "hsn_code": "8443", "description": "Hp 1606 Lower Bush", "unit_of_measurement_or_uom": "NOS", "unit_price": "250.00", "quantity": "2", "sgst_rate": "None", "sgst_amount": "None", "cgst_rate": "None", "cgst_amount": "None", "igst_rate": "None", "igst_amount": "None", "taxable_amount_or_subtotal": "500.00", "total_amount_or_gross_amount_of_the_item": "None" }, { "hsn_code": "8443", "description": "1505 /1606 Fuser Drive Gear", "unit_of_measurement_or_uom": "NOS", "unit_price": "250.00", "quantity": "1", "sgst_rate": "None", "sgst_amount": "None", "cgst_rate": "None", "cgst_amount": "None", "igst_rate": "None", "igst_amount": "None", "taxable_amount_or_subtotal": "250.00", "total_amount_or_gross_amount_of_the_item": "None" } ],
Code that is working for header entities
import cv2 import pandas as pd import json from thefuzz import fuzz from itertools import product used_bounding_boxes = {}
def preprocess_entity(entity): return entity.replace(",", "").strip()
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=1, max_distance=50): 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}
- Assistant
- User
return the complete code
- Assistant