| |
| import sys |
| import re |
| from PIL import Image |
| import supervision as sv |
|
|
| |
| from src.logger import logging |
| from src.exception import CustomExceptionHandling |
|
|
|
|
| |
| def clean_text(text: str) -> str: |
| """ |
| Cleans the given text by removing unwanted tokens, extra spaces, |
| and ensures proper spacing between words and after punctuation marks. |
| |
| Args: |
| text (str): The input text to be cleaned. |
| |
| Returns: |
| str: The cleaned and properly formatted text. |
| """ |
| try: |
| |
| text = text.replace("<pad>", "").replace("</s>", "").strip() |
|
|
| |
| lines = text.split("\n") |
| cleaned_lines = [line.strip() for line in lines if line.strip()] |
|
|
| |
| cleaned_text = " ".join(cleaned_lines) |
|
|
| |
| cleaned_text = re.sub( |
| r"\s+", " ", cleaned_text |
| ) |
| cleaned_text = re.sub( |
| r"(?<=[.,!?])(?=[^\s])", r" ", cleaned_text |
| ) |
| cleaned_text = re.sub( |
| r"(?<=[a-z])(?=[A-Z])", r" ", cleaned_text |
| ) |
| cleaned_text = re.sub( |
| r"(\w)([A-Z][a-z])", r"\1 \2", cleaned_text |
| ) |
|
|
| |
| logging.info("Text cleaned successfully.") |
|
|
| |
| return cleaned_text |
|
|
| |
| except Exception as e: |
| |
| raise CustomExceptionHandling(e, sys) from e |
|
|
|
|
| |
| def draw_ocr_bboxes(image: Image.Image, detections: sv.Detections) -> Image.Image: |
| """ |
| Draws bounding boxes and labels on the input image based on the OCR detections. |
| |
| Args: |
| - image (PIL.Image.Image): The input image on which to draw the bounding boxes and labels. |
| - detections (sv.Detections): The OCR detections containing the bounding box coordinates and labels. |
| |
| Returns: |
| PIL.Image.Image: The annotated image with bounding boxes and labels. |
| """ |
| try: |
| |
| annotated_image = image.copy() |
|
|
| |
| thickness = sv.calculate_optimal_line_thickness(resolution_wh=image.size) |
| text_scale = sv.calculate_optimal_text_scale(resolution_wh=image.size) |
|
|
| |
| bounding_box_annotator = sv.BoxAnnotator( |
| color_lookup=sv.ColorLookup.INDEX, thickness=thickness |
| ) |
| label_annotator = sv.LabelAnnotator( |
| color_lookup=sv.ColorLookup.INDEX, |
| text_scale=text_scale, |
| text_thickness=thickness, |
| ) |
|
|
| |
| annotated_image = bounding_box_annotator.annotate(annotated_image, detections) |
| annotated_image = label_annotator.annotate(annotated_image, detections) |
|
|
| |
| logging.info("Bounding boxes and labels drawn successfully.") |
|
|
| |
| return annotated_image |
|
|
| |
| except Exception as e: |
| |
| raise CustomExceptionHandling(e, sys) from e |
|
|