Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import os
|
3 |
+
import pytesseract
|
4 |
+
import gradio as gr
|
5 |
+
from gradio import Interface, Image, Text
|
6 |
+
import numpy as np
|
7 |
+
from PIL import Image
|
8 |
+
from PIL import UnidentifiedImageError
|
9 |
+
|
10 |
+
def process_image(input_image):
|
11 |
+
try:
|
12 |
+
# Convert the input image to a NumPy array if it's a PIL Image
|
13 |
+
if isinstance(input_image, Image.Image):
|
14 |
+
img = np.array(input_image)
|
15 |
+
else:
|
16 |
+
# If it's a file path or file-like object, read it directly with OpenCV
|
17 |
+
img = cv2.imread(input_image)
|
18 |
+
|
19 |
+
# Check that the image is in the expected format
|
20 |
+
if img is None or img.dtype != np.uint8:
|
21 |
+
raise Exception("Could not read the image. Please check the image format.")
|
22 |
+
|
23 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
24 |
+
|
25 |
+
# img = cv2.imdecode(np.fromstring(input_image.read(), np.uint8), cv2.IMREAD_COLOR)
|
26 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
27 |
+
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
|
28 |
+
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
|
29 |
+
dilation = cv2.dilate(thresh1, rect_kernel, iterations=1)
|
30 |
+
|
31 |
+
# Find text lines using connected component analysis
|
32 |
+
text_lines = []
|
33 |
+
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
34 |
+
for cnt in contours:
|
35 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
36 |
+
text_lines.append((y, y + h, x, x + w))
|
37 |
+
|
38 |
+
# Sort text lines by their y-coordinates
|
39 |
+
text_lines.sort(key=lambda line: line[0])
|
40 |
+
|
41 |
+
# Extract text from each line using Tesseract
|
42 |
+
recognized_text = []
|
43 |
+
for y_min, y_max, x_min, x_max in text_lines:
|
44 |
+
cropped_img = img[y_min:y_max, x_min:x_max]
|
45 |
+
custom_config = r'-l eng+khm --oem 3 --psm 6'
|
46 |
+
extracted_text = pytesseract.image_to_string(cropped_img, config=custom_config)
|
47 |
+
recognized_text.append(extracted_text.strip())
|
48 |
+
|
49 |
+
# Combine recognized text into a single string
|
50 |
+
full_text = "\n".join(recognized_text)
|
51 |
+
|
52 |
+
# Draw bounding boxes on the image
|
53 |
+
result_rgb = img.copy()
|
54 |
+
for y_min, y_max, x_min, x_max in text_lines:
|
55 |
+
cv2.rectangle(result_rgb, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
56 |
+
|
57 |
+
return full_text, result_rgb
|
58 |
+
except Exception as e:
|
59 |
+
return "Could not process the image. Error: " + str(e), None
|
60 |
+
|
61 |
+
iface = gr.Interface(
|
62 |
+
process_image,
|
63 |
+
inputs=[gr.Image(type="pil", label="Processed Image")],
|
64 |
+
outputs=[
|
65 |
+
gr.Text(label="Detected Labels"),
|
66 |
+
gr.Image(type="pil", label="Processed Image")
|
67 |
+
],
|
68 |
+
title="Bank Statement OCR",
|
69 |
+
# description="Upload an image containing text to perform OCR and see the detected text and image."
|
70 |
+
flagging_options=["blurry", "incorrect", "other"],)
|
71 |
+
|
72 |
+
iface.launch(debug=True)
|