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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import os
|
| 2 |
+
# import cv2
|
| 3 |
+
# import re
|
| 4 |
+
# import numpy as np
|
| 5 |
+
# from PIL import Image, ImageDraw, ImageFont
|
| 6 |
+
# from paddleocr import PaddleOCR
|
| 7 |
+
# from pdf2image import convert_from_path
|
| 8 |
+
# import gradio as gr
|
| 9 |
+
|
| 10 |
+
# # Specify the path to the Poppler bin directory
|
| 11 |
+
# poppler_path = r"C:\\poppler\\poppler-24.08.0\\Library\\bin"
|
| 12 |
+
|
| 13 |
+
# # Function to check proximity of bounding boxes
|
| 14 |
+
# def are_boxes_close(box1, box2, y_threshold=50):
|
| 15 |
+
# y1_center = (box1[0][1] + box1[2][1]) / 2
|
| 16 |
+
# y2_center = (box2[0][1] + box2[2][1]) / 2
|
| 17 |
+
# return abs(y1_center - y2_center) <= y_threshold
|
| 18 |
+
|
| 19 |
+
# # Function to extract terms with specific rules
|
| 20 |
+
# def extract_specific_terms(ocr_results):
|
| 21 |
+
# extracted_terms = []
|
| 22 |
+
|
| 23 |
+
# for line in ocr_results[0]:
|
| 24 |
+
# detected_text = line[1][0] # Extracted text
|
| 25 |
+
# box = line[0] # Bounding box of the detected text
|
| 26 |
+
|
| 27 |
+
# if re.match(r"Bill of Lading:\s*\d+", detected_text):
|
| 28 |
+
# extracted_terms.append({'detected_text': detected_text, 'bounding_box': box})
|
| 29 |
+
|
| 30 |
+
# elif re.match(r"Page:\s*\w+", detected_text):
|
| 31 |
+
# extracted_terms.append({'detected_text': detected_text, 'bounding_box': box})
|
| 32 |
+
|
| 33 |
+
# elif detected_text in ["Shipper", "Receiver", "Carrier"]:
|
| 34 |
+
# extracted_terms.append({'detected_text': detected_text + " Signature", 'bounding_box': box})
|
| 35 |
+
|
| 36 |
+
# elif detected_text == "Signature":
|
| 37 |
+
# extracted_terms.append({'detected_text': detected_text, 'bounding_box': box})
|
| 38 |
+
|
| 39 |
+
# return extracted_terms
|
| 40 |
+
|
| 41 |
+
# # Function to annotate image with detected terms
|
| 42 |
+
# def annotate_image_with_terms(image, terms):
|
| 43 |
+
# pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 44 |
+
# draw = ImageDraw.Draw(pil_image)
|
| 45 |
+
|
| 46 |
+
# font_size = 40
|
| 47 |
+
# try:
|
| 48 |
+
# font = ImageFont.truetype("arial.ttf", font_size)
|
| 49 |
+
# except IOError:
|
| 50 |
+
# font = ImageFont.load_default()
|
| 51 |
+
|
| 52 |
+
# for term in terms:
|
| 53 |
+
# box = term['bounding_box']
|
| 54 |
+
# detected_text = term['detected_text']
|
| 55 |
+
|
| 56 |
+
# points = [(int(x[0]), int(x[1])) for x in box]
|
| 57 |
+
# draw.polygon(points, outline="blue", width=2)
|
| 58 |
+
# position = (points[0][0], points[0][1] - font_size - 5)
|
| 59 |
+
# draw.text(position, detected_text, fill="red", font=font)
|
| 60 |
+
|
| 61 |
+
# return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
| 62 |
+
|
| 63 |
+
# # Main processing function
|
| 64 |
+
# def process_file(file):
|
| 65 |
+
# ocr = PaddleOCR(lang='en')
|
| 66 |
+
# extracted_terms = []
|
| 67 |
+
|
| 68 |
+
# if file.name.endswith(".pdf"):
|
| 69 |
+
# images = convert_from_path(file.name, poppler_path=poppler_path)
|
| 70 |
+
# processed_images = []
|
| 71 |
+
# for image in images:
|
| 72 |
+
# image_np = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 73 |
+
# ocr_results = ocr.ocr(image_np, cls=True)
|
| 74 |
+
# extracted_terms = extract_specific_terms(ocr_results)
|
| 75 |
+
# annotated_image = annotate_image_with_terms(image_np, extracted_terms)
|
| 76 |
+
# processed_images.append(annotated_image)
|
| 77 |
+
|
| 78 |
+
# return [Image.fromarray(img) for img in processed_images]
|
| 79 |
+
|
| 80 |
+
# else:
|
| 81 |
+
# image = cv2.imread(file.name)
|
| 82 |
+
# ocr_results = ocr.ocr(image, cls=True)
|
| 83 |
+
# extracted_terms = extract_specific_terms(ocr_results)
|
| 84 |
+
# annotated_image = annotate_image_with_terms(image, extracted_terms)
|
| 85 |
+
# return Image.fromarray(annotated_image)
|
| 86 |
+
|
| 87 |
+
# # Gradio Interface
|
| 88 |
+
# def gradio_interface(file):
|
| 89 |
+
# result = process_file(file)
|
| 90 |
+
# if isinstance(result, list):
|
| 91 |
+
# return result[0] # Display only the first page
|
| 92 |
+
# return result
|
| 93 |
+
|
| 94 |
+
# iface = gr.Interface(
|
| 95 |
+
# fn=gradio_interface,
|
| 96 |
+
# inputs=gr.File(label="Upload an Image or PDF", file_types=[".pdf", ".png", ".jpg", ".jpeg"]),
|
| 97 |
+
# outputs="image",
|
| 98 |
+
# live=True,
|
| 99 |
+
# title="OCR Term Extraction",
|
| 100 |
+
# description="Upload an image or PDF containing text to detect and annotate terms such as 'Bill of Lading', 'Page', and signatures.",
|
| 101 |
+
# allow_flagging="never"
|
| 102 |
+
# )
|
| 103 |
+
# iface.launch()
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
import os
|
| 108 |
+
import cv2
|
| 109 |
+
import re
|
| 110 |
+
import numpy as np
|
| 111 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 112 |
+
from paddleocr import PaddleOCR
|
| 113 |
+
import gradio as gr
|
| 114 |
+
|
| 115 |
+
# Function to check proximity of bounding boxes
|
| 116 |
+
def are_boxes_close(box1, box2, y_threshold=50):
|
| 117 |
+
y1_center = (box1[0][1] + box1[2][1]) / 2
|
| 118 |
+
y2_center = (box2[0][1] + box2[2][1]) / 2
|
| 119 |
+
return abs(y1_center - y2_center) <= y_threshold
|
| 120 |
+
|
| 121 |
+
# Function to extract terms with specific rules
|
| 122 |
+
def extract_specific_terms(ocr_results):
|
| 123 |
+
extracted_terms = []
|
| 124 |
+
|
| 125 |
+
for line in ocr_results[0]:
|
| 126 |
+
detected_text = line[1][0] # Extracted text
|
| 127 |
+
box = line[0] # Bounding box of the detected text
|
| 128 |
+
|
| 129 |
+
if re.match(r"Bill of Lading:\s*\d+", detected_text):
|
| 130 |
+
extracted_terms.append({'detected_text': detected_text, 'bounding_box': box})
|
| 131 |
+
|
| 132 |
+
elif re.match(r"Page:\s*\w+", detected_text):
|
| 133 |
+
extracted_terms.append({'detected_text': detected_text, 'bounding_box': box})
|
| 134 |
+
|
| 135 |
+
elif detected_text in ["Shipper", "Receiver", "Carrier"]:
|
| 136 |
+
extracted_terms.append({'detected_text': detected_text + " Signature", 'bounding_box': box})
|
| 137 |
+
|
| 138 |
+
elif detected_text == "Signature":
|
| 139 |
+
extracted_terms.append({'detected_text': detected_text, 'bounding_box': box})
|
| 140 |
+
|
| 141 |
+
return extracted_terms
|
| 142 |
+
|
| 143 |
+
# Function to annotate image with detected terms
|
| 144 |
+
def annotate_image_with_terms(image, terms):
|
| 145 |
+
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 146 |
+
draw = ImageDraw.Draw(pil_image)
|
| 147 |
+
|
| 148 |
+
font_size = 20
|
| 149 |
+
try:
|
| 150 |
+
font = ImageFont.truetype("arial.ttf", font_size)
|
| 151 |
+
except IOError:
|
| 152 |
+
font = ImageFont.load_default()
|
| 153 |
+
|
| 154 |
+
for term in terms:
|
| 155 |
+
box = term['bounding_box']
|
| 156 |
+
detected_text = term['detected_text']
|
| 157 |
+
|
| 158 |
+
points = [(int(x[0]), int(x[1])) for x in box]
|
| 159 |
+
draw.polygon(points, outline="blue", width=2)
|
| 160 |
+
position = (points[0][0], points[0][1] - font_size - 5)
|
| 161 |
+
draw.text(position, detected_text, fill="red", font=font)
|
| 162 |
+
|
| 163 |
+
return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
| 164 |
+
|
| 165 |
+
# Main processing function
|
| 166 |
+
def process_file(file):
|
| 167 |
+
ocr = PaddleOCR(lang='en')
|
| 168 |
+
extracted_terms = []
|
| 169 |
+
|
| 170 |
+
# Handle image files (PNG, JPG, JPEG)
|
| 171 |
+
image = cv2.imread(file.name)
|
| 172 |
+
ocr_results = ocr.ocr(image, cls=True)
|
| 173 |
+
extracted_terms = extract_specific_terms(ocr_results)
|
| 174 |
+
annotated_image = annotate_image_with_terms(image, extracted_terms)
|
| 175 |
+
return Image.fromarray(annotated_image)
|
| 176 |
+
|
| 177 |
+
# Gradio Interface
|
| 178 |
+
def gradio_interface(file):
|
| 179 |
+
result = process_file(file)
|
| 180 |
+
return result
|
| 181 |
+
|
| 182 |
+
iface = gr.Interface(
|
| 183 |
+
fn=gradio_interface,
|
| 184 |
+
inputs=gr.File(label="Upload an Image", file_types=[".png", ".jpg", ".jpeg"]),
|
| 185 |
+
outputs="image",
|
| 186 |
+
live=True,
|
| 187 |
+
title="OCR Term Extraction",
|
| 188 |
+
description="Upload an image containing text to detect and annotate terms such as 'Bill of Lading', 'Page', and signatures.",
|
| 189 |
+
allow_flagging="never"
|
| 190 |
+
)
|
| 191 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python
|
| 2 |
+
numpy
|
| 3 |
+
Pillow
|
| 4 |
+
paddlepaddle
|
| 5 |
+
# pdf2image
|
| 6 |
+
gradio
|
| 7 |
+
# poppler-utils
|