Analyse the complete code and Fix this error Exception has occurred: AttributeError…
- User
Analyse the complete code and Fix this error Exception has occurred: AttributeError 'BoundingBoxFinder' object has no attribute 'used_bounding_boxes' File "/home/ritik1s/Desktop/bbox_issues/temp_GPT/bbx_by_me.py", line 62, in process_single_token_entity if entity not in self.used_bounding_boxes: ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ritik1s/Desktop/bbox_issues/temp_GPT/bbx_by_me.py", line 177, in process_data box = self.process_single_token_entity(dataframe, entity_value_no_comma) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/ritik1s/Desktop/bbox_issues/temp_GPT/bbx_by_me.py", line 201, in bbox_finder.process_data("/home/ritik1s/Desktop/bbox_issues/temp_GPT/row_skip.json", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.csv", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.jpeg") AttributeError: 'BoundingBoxFinder' object has no attribute 'used_bounding_boxes' in the given code
import cv2 import pandas as pd import json from thefuzz import fuzz from itertools import product
class BoundingBoxFinder: def init(self): self.used_bounding_boxes = {}
def preprocess_entity(self, entity): try: token = entity.replace(",", "").strip() return token except: pass def calculate_proximity_score(self, 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(self, box_a, box_b, max_line_difference=5, max_distance=100): return self.calculate_proximity_score(box_a, box_b) <= max_distance + 2 * max_line_difference def merge_boxes(self, 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(self, dataframe, token, threshold=75): potential_matches = [] for _, row in dataframe.iterrows(): ocr_text = self.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(self, 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: self.calculate_proximity_score(current_box, match["box"])) best_sequence.append(next_best_match) return best_sequence def process_single_token_entity(self, dataframe, entity, threshold=75): best_match = None best_score = threshold entity = self.preprocess_entity(entity) if entity not in self.used_bounding_boxes: self.used_bounding_boxes[entity] = [] for _, row in dataframe.iterrows(): ocr_text = self.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 self.used_bounding_boxes[entity]: best_score = score best_match = current_box if best_match: self.used_bounding_boxes[entity].append(best_match) return best_match def box_overlap(self, box1, box2): """Check if there"s any overlap in any coordinate between two boxes.""" return box1["left"] == box2["left"] or box1["right"] == box2["right"] def all_boxes_unique(self, sequence_boxes, used_boxes): """Ensure no part of the boxes in sequence_boxes overlaps with any box in used_boxes.""" for seq_box in sequence_boxes: for used_box in used_boxes: if self.box_overlap(seq_box, used_box): return False return True def get_next_best_sequence(self, all_potential_matches, previous_matches, entity): """ Try to find the next best sequence of matches that hasn"t used any part of the bounding boxes. """ # Flatten the list of used boxes for easier comparison. used_boxes = [box for sequence in previous_matches.get(entity, []) for box in sequence] for sequence in product(*all_potential_matches): sequence_boxes = [match["box"] for match in sequence] if self.all_boxes_unique(sequence_boxes, used_boxes): return sequence # Found a sequence where no box part has been used before return None # No unique sequence found def process_multi_token_entity(self, dataframe, entity, threshold=75, max_distance=100, max_line_difference=3): tokens = entity.split() all_potential_matches = [self.find_potential_matches(dataframe, token, threshold) for token in tokens] if not all(matches for matches in all_potential_matches): return None if entity not in self.used_bounding_boxes: self.used_bounding_boxes[entity] = [] previous_matches = self.used_bounding_boxes.get(entity, []) all_sequences = list(product(*all_potential_matches)) # Create all possible sequences valid_sequences = [] # List to hold sequences that meet the is_nearby condition for sequence in all_sequences: sequence_boxes = [match["box"] for match in sequence] sequence_is_valid = True for i in range(len(sequence_boxes) - 1): if not self.is_nearby(sequence_boxes[i], sequence_boxes[i + 1], max_line_difference, max_distance): sequence_is_valid = False break if sequence_is_valid: valid_sequences.append(sequence) # Sort valid sequences by their cumulative proximity score, to prioritize those with boxes closer together valid_sequences = sorted(valid_sequences, key=lambda seq: sum(self.calculate_proximity_score(seq[i]["box"], seq[i+1]["box"]) for i in range(len(seq) - 1))) next_best_sequence = None for sequence in valid_sequences: sequence_boxes = [match["box"] for match in sequence] if self.all_boxes_unique(sequence_boxes, [box for sublist in previous_matches for box in sublist]): next_best_sequence = sequence break if next_best_sequence: new_boxes_sequence = [match["box"] for match in next_best_sequence] merged_box = self.merge_boxes(new_boxes_sequence) self.used_bounding_boxes[entity].append(new_boxes_sequence) return merged_box return None def draw_bounding_boxes(self, image_path, bounding_boxes, entity_names): image = cv2.imread(image_path) font = cv2.FONT_HERSHEY_SIMPLEX for box, name in zip(bounding_boxes, entity_names): if box: cv2.rectangle(image, (box["left"], box["top"]), (box["right"], box["bottom"]), (0, 255, 0), 2) cv2.putText(image, name, (box["left"], max(box["top"] - 10, 0)), font, 0.5, (0, 0, 255), 2) cv2.imwrite("annotated_image.jpg", image) def process_data(self, json_path, csv_path, image_path): with open(json_path, "r") as f: data = json.load(f) dataframe = pd.read_csv(csv_path) bounding_boxes = [] entity_names = [] # Existing processing for non-special sections special_sections = ["amounts_and_tax", "Payment Details"] # Define special handling cases here for section in ["invoice_details", "Payment Details", "amounts_and_tax"]: entities = data.get(section, {}) # Check if the current section needs special handling if section not in special_sections: for entity_name, entity_value in entities.items(): entity_value_no_comma = self.preprocess_entity(entity_value) if " " in entity_value_no_comma: box = self.process_multi_token_entity(dataframe, entity_value_no_comma) else: box = self.process_single_token_entity(dataframe, entity_value_no_comma) if box: bounding_boxes.append(box) entity_names.append(entity_name) else: # Special handling for "amounts_and_tax" section reversed_dataframe = dataframe.iloc[::-1].reset_index(drop=True) # Reverse the dataframe for entity_name, entity_value in entities.items(): entity_value_no_comma = self.preprocess_entity(entity_value) if " " in entity_value_no_comma: # Use the reversed_dataframe for multi-token entities box = self.process_multi_token_entity(reversed_dataframe, entity_value_no_comma) else: # Use the reversed_dataframe for single-token entities box = self.process_single_token_entity(reversed_dataframe, entity_value_no_comma) if box: bounding_boxes.append(box) entity_names.append(entity_name) self.draw_bounding_boxes(image_path, bounding_boxes, entity_names)Example usage
if name == "main": bbox_finder = BoundingBoxFinder() bbox_finder.process_data("/home/ritik1s/Desktop/bbox_issues/temp_GPT/row_skip.json", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.csv", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.jpeg")
bbox_finder.main("/home/ritik1s/Desktop/bbox_issues/temp_GPT/row_skip.json", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.csv", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.jpeg")
- Assistant
The error you are experiencing,
AttributeError: 'BoundingBoxFinder' object has no attribute 'used_bounding_boxes', seems to originate from theBoundingBoxFinderclass not initializing itsused_bounding_boxesattribute correctly. This is due to a mistake in the method definition for the initializer (constructor).In Python, the constructor method should be named
__init__with double underscores (__) before and afterinit, but in your code snippet, it's incorrectly defined asinit(missing the double underscores). Due to this, Python does not recognize it as the constructor, and thusself.used_bounding_boxesis never initialized, leading to theAttributeErrorwhen you try to access it.To fix the error, modify the constructor definition in the
BoundingBoxFinderclass as follows:class BoundingBoxFinder: def __init__(self): # Corrected from 'def init(self):' self.used_bounding_boxes = {}Also, there's an issue with the
if __name__ == "main":statement. It should be corrected toif __name__ == "__main__":for the code block to be executed when the script is run as the main program.After making this correction, the
BoundingBoxFinderobject should have itsused_bounding_boxesattribute properly initialized, and you should be able to run your code without encountering the mentionedAttributeError.Here is the corrected version of the last part of your script:
# Correct usage if __name__ == "__main__": # Corrected from if __name__ == "main": bbox_finder = BoundingBoxFinder() bbox_finder.process_data("/home/ritik1s/Desktop/bbox_issues/temp_GPT/row_skip.json", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.csv", "/home/ritik1s/Desktop/bbox_issues/temp_GPT/check.jpeg")Make sure you've corrected both mistakes (the constructor and the
if __name__ == "__main__":statement) for your code to work as expected. - User
Still same error after correcting the given correction
- Assistant
If you've already corrected the
__init__method as well as theif __name__ == "__main__":statement and are still encountering the same error, let's reassess your code to ensure that theBoundingBoxFinderinstantiation and the attribute access are done correctly.Given the scenario where the corrections were correctly applied but the error persists, let's ensure that the instantiation of the
BoundingBoxFinderclass (where__init__is correctly defined with double underscores) happens properly before any method attempts to accessself.used_bounding_boxes. It's crucial that the object is correctly created with its__init__method being called to initialize theused_bounding_boxes.Given the corrections I provided earlier were applied, your class's
__init__method should look like this:class BoundingBoxFinder: def __init__(self): self.used_bounding_boxes = {}And the conditional block to run your code script should be:
if __name__ == "__main__": ...If your corrections match the above and you're still facing issues, it's worth revisiting how and where the
BoundingBoxFinderinstance is used. Make sure you are not accessingself.used_bounding_boxessomewhere else before it has been initialized (which is unlikely after the correction unless there are multiple files or instances involved).In complex projects, another potential reason for such an issue persisting could be related to the environment or how Python might be caching your modules. Try the following steps to troubleshoot further:
-
Restart your Python environment or IDE completely. If you're using Jupyter notebooks, restart the kernel. This step ensures that any variable or module caching is cleared.
-
Ensure there are no old bytecode (
*.pyc) files in your project directory that might be getting loaded instead of your updated script. Although not common, this can happen in some misconfigured environments. -
Double-check for multiple definitions of the
BoundingBoxFinderclass within your project. If you have definedBoundingBoxFinderin multiple files, ensure you are editing and running the correct one. -
Print Debugging: As a quick check, add a print statement in the
__init__method to ensure it is being executed:
def __init__(self): print("Initializing used_bounding_boxes") self.used_bounding_boxes = {}After you run your script again, look for the printed message to confirm that initialization is occurring as expected.
If after following these steps you still experience the issue, please ensure that there's no confusion between different versions of your code or files. Verify the file paths and ensure you're editing and executing the intended script without any mix-up.
Considering all above steps should rectify issues arising from improper attribute initialization or file handling in Python, it's crucial to ensure every correction is applied as intended and the runtime environment is correctly set up.
-