Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,208 +1,190 @@
|
|
1 |
-
import os
|
2 |
-
import logging
|
3 |
-
import cv2
|
4 |
-
import numpy as np
|
5 |
-
from
|
6 |
-
from
|
7 |
-
from
|
8 |
-
from
|
9 |
-
from surya.
|
10 |
-
from surya.model.
|
11 |
-
from surya.model.recognition.
|
12 |
-
|
13 |
-
import
|
14 |
-
import
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
pytesseract
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
#
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
return outputs
|
192 |
-
|
193 |
-
# Gradio App
|
194 |
-
iface = gr.Interface(
|
195 |
-
fn=process_document_interface,
|
196 |
-
inputs=gr.File(label="Upload PDF or Image"),
|
197 |
-
outputs=[
|
198 |
-
gr.Gallery(label="Corrected Images"),
|
199 |
-
gr.Gallery(label="Detected Text Images"),
|
200 |
-
gr.Gallery(label="Detected Layout Images"),
|
201 |
-
gr.Textbox(label="Extracted Text")
|
202 |
-
],
|
203 |
-
title="Document Processor",
|
204 |
-
description="Upload a PDF or image to correct skew/rotation, detect text/layout, and extract text using OCR."
|
205 |
-
)
|
206 |
-
|
207 |
-
if __name__ == "__main__":
|
208 |
-
iface.launch()
|
|
|
1 |
+
import os
|
2 |
+
import logging
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from pdf2image import convert_from_path
|
6 |
+
from pytesseract import Output, pytesseract
|
7 |
+
from scipy.ndimage import rotate
|
8 |
+
from surya.ocr import run_ocr
|
9 |
+
from surya.model.detection.model import load_model as load_det_model, load_processor as load_det_processor
|
10 |
+
from surya.model.recognition.model import load_model as load_rec_model
|
11 |
+
from surya.model.recognition.processor import load_processor as load_rec_processor
|
12 |
+
import imutils
|
13 |
+
import gradio as gr
|
14 |
+
import subprocess
|
15 |
+
import glob
|
16 |
+
from PIL import Image, ImageDraw
|
17 |
+
from pytesseract import Output
|
18 |
+
import pytesseract
|
19 |
+
|
20 |
+
# Function to correct image skew
|
21 |
+
def correct_skew(image, delta=0.1, limit=3):
|
22 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
23 |
+
thresh = cv2.adaptiveThreshold(
|
24 |
+
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
25 |
+
cv2.THRESH_BINARY_INV, 41, 15
|
26 |
+
)
|
27 |
+
|
28 |
+
scores = []
|
29 |
+
angles = np.arange(-limit, limit + delta, delta)
|
30 |
+
for angle in angles:
|
31 |
+
_, score = determine_score(thresh, angle)
|
32 |
+
scores.append(score)
|
33 |
+
|
34 |
+
best_angle = angles[scores.index(max(scores))]
|
35 |
+
|
36 |
+
(h, w) = image.shape[:2]
|
37 |
+
center = (w // 2, h // 2)
|
38 |
+
M = cv2.getRotationMatrix2D(center, best_angle, 1.0)
|
39 |
+
rotated = cv2.warpAffine(
|
40 |
+
image, M, (w, h), flags=cv2.INTER_LINEAR,
|
41 |
+
borderMode=cv2.BORDER_CONSTANT, borderValue=(255, 255, 255)
|
42 |
+
)
|
43 |
+
|
44 |
+
print(f"[INFO] Detected skew angle: {best_angle} degrees")
|
45 |
+
return rotated
|
46 |
+
|
47 |
+
def determine_score(arr, angle):
|
48 |
+
data = rotate(arr, angle, reshape=False, order=0)
|
49 |
+
histogram = np.sum(data, axis=1, dtype=float)
|
50 |
+
score = np.sum((histogram[1:] - histogram[:-1]) ** 2, dtype=float)
|
51 |
+
return histogram, score
|
52 |
+
|
53 |
+
def correct_image_rotation(image):
|
54 |
+
if isinstance(image, Image.Image):
|
55 |
+
original_size = image.size
|
56 |
+
print('image original size is:', original_size)
|
57 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
58 |
+
|
59 |
+
image_required = image.copy()
|
60 |
+
h, w = image_required.shape[:2]
|
61 |
+
cropped_rotated = cv2.resize(image_required, (w * 4, h * 4))
|
62 |
+
|
63 |
+
results = pytesseract.image_to_osd(
|
64 |
+
cropped_rotated,
|
65 |
+
output_type=Output.DICT,
|
66 |
+
config='--dpi 300 --psm 0 -c min_characters_to_try=5 -c tessedit_script_lang=Arabic'
|
67 |
+
)
|
68 |
+
|
69 |
+
if results["script"] not in ['Bengali', 'Latin', 'Greek', 'Katakana'] and results["orientation"] != 180:
|
70 |
+
print("[INFO] Detected orientation: {}".format(results["orientation"]))
|
71 |
+
print("[INFO] Rotate by {} degrees to correct".format(results["rotate"]))
|
72 |
+
print("[INFO] Detected script: {}".format(results["script"]))
|
73 |
+
rotated = imutils.rotate_bound(image, angle=results['rotate'])
|
74 |
+
if results['rotate'] in [90, 270]:
|
75 |
+
rotated_h, rotated_w = rotated.shape[:2]
|
76 |
+
original_size = (rotated_w, rotated_h)
|
77 |
+
print(f"Rotated dimensions: {rotated_w}x{rotated_h}")
|
78 |
+
if (rotated_w, rotated_h) != (h, w):
|
79 |
+
rotated = cv2.resize(rotated, (w, h))
|
80 |
+
else:
|
81 |
+
print("[INFO] Major orientation is correct, proceeding to fine-tune...")
|
82 |
+
rotated = image
|
83 |
+
|
84 |
+
final_rotated = correct_skew(rotated)
|
85 |
+
rotated_pil = Image.fromarray(cv2.cvtColor(final_rotated, cv2.COLOR_BGR2RGB))
|
86 |
+
|
87 |
+
print('resize the image to its original size: ', original_size)
|
88 |
+
corrected_image = rotated_pil.resize(original_size, Image.Resampling.LANCZOS)
|
89 |
+
return corrected_image
|
90 |
+
|
91 |
+
# Function to process PDF or image and detect text lines
|
92 |
+
def process_pdf(file_path):
|
93 |
+
# Define the results directories
|
94 |
+
detected_text_dir = "/home/Detected_Text_Line"
|
95 |
+
detected_layout_dir = "/home/Detected_layout"
|
96 |
+
ocr_dir = "/home/OCR"
|
97 |
+
|
98 |
+
# Ensure the results directories exist
|
99 |
+
os.makedirs(detected_text_dir, exist_ok=True)
|
100 |
+
os.makedirs(detected_layout_dir, exist_ok=True)
|
101 |
+
os.makedirs(ocr_dir, exist_ok=True)
|
102 |
+
|
103 |
+
# Extract the PDF name (without extension)
|
104 |
+
pdf_name = os.path.splitext(os.path.basename(file_path))[0]
|
105 |
+
|
106 |
+
# Step 1: Run surya_detect
|
107 |
+
try:
|
108 |
+
subprocess.run(
|
109 |
+
["surya_detect", "--results_dir", detected_text_dir, "--images", file_path],
|
110 |
+
check=True,
|
111 |
+
)
|
112 |
+
print(f"[INFO] surya_detect completed for {file_path}")
|
113 |
+
except subprocess.CalledProcessError as e:
|
114 |
+
print(f"[ERROR] surya_detect failed: {e}")
|
115 |
+
return None
|
116 |
+
|
117 |
+
# Step 2: Remove column files (if they exist)
|
118 |
+
column_files = glob.glob(f"{detected_text_dir}/{pdf_name}/*column*")
|
119 |
+
if column_files:
|
120 |
+
try:
|
121 |
+
subprocess.run(["rm"] + column_files, check=True)
|
122 |
+
print(f"[INFO] Removed column files for {pdf_name}")
|
123 |
+
except subprocess.CalledProcessError as e:
|
124 |
+
print(f"[ERROR] Failed to remove column files: {e}")
|
125 |
+
else:
|
126 |
+
print(f"[INFO] No column files found for {pdf_name}")
|
127 |
+
|
128 |
+
# Return the path to the directory containing the output images
|
129 |
+
output_dir = os.path.join(detected_text_dir, pdf_name)
|
130 |
+
return output_dir
|
131 |
+
|
132 |
+
# Function to handle the Gradio interface
|
133 |
+
def gradio_interface(file):
|
134 |
+
# Step 1: Correct the skew of the input file
|
135 |
+
corrected_images = []
|
136 |
+
if file.name.endswith('.pdf'):
|
137 |
+
images = convert_from_path(file.name)
|
138 |
+
for i, image in enumerate(images):
|
139 |
+
corrected_image = correct_image_rotation(image)
|
140 |
+
corrected_images.append(corrected_image)
|
141 |
+
else:
|
142 |
+
image = Image.open(file.name)
|
143 |
+
corrected_image = correct_image_rotation(image)
|
144 |
+
corrected_images.append(corrected_image)
|
145 |
+
|
146 |
+
# Save corrected images to a folder
|
147 |
+
corrected_dir = "/home/Corrected_Images"
|
148 |
+
os.makedirs(corrected_dir, exist_ok=True)
|
149 |
+
for i, corrected_image in enumerate(corrected_images):
|
150 |
+
corrected_image.save(os.path.join(corrected_dir, f"corrected_{i}.png"))
|
151 |
+
|
152 |
+
# Step 2: Detect text lines in the corrected images
|
153 |
+
detected_dir = process_pdf(corrected_dir)
|
154 |
+
|
155 |
+
if detected_dir is None:
|
156 |
+
# Return a placeholder image with an error message
|
157 |
+
error_image = Image.new("RGB", (400, 200), color="red")
|
158 |
+
error_draw = ImageDraw.Draw(error_image)
|
159 |
+
error_draw.text((10, 10), "Error detecting text lines. Check the logs for details.", fill="white")
|
160 |
+
return corrected_images, [error_image]
|
161 |
+
|
162 |
+
# Load and return the detected text line images
|
163 |
+
detected_images = []
|
164 |
+
for image_file in sorted(os.listdir(detected_dir)):
|
165 |
+
if image_file.endswith((".png", ".jpg", ".jpeg")):
|
166 |
+
image_path = os.path.join(detected_dir, image_file)
|
167 |
+
detected_images.append(Image.open(image_path))
|
168 |
+
|
169 |
+
if not detected_images:
|
170 |
+
# Return a placeholder image if no output images are found
|
171 |
+
placeholder_image = Image.new("RGB", (400, 200), color="gray")
|
172 |
+
placeholder_draw = ImageDraw.Draw(placeholder_image)
|
173 |
+
placeholder_draw.text((10, 10), "No detected text line images found.", fill="white")
|
174 |
+
return corrected_images, [placeholder_image]
|
175 |
+
|
176 |
+
return corrected_images, detected_images
|
177 |
+
|
178 |
+
# Gradio Interface
|
179 |
+
iface = gr.Interface(
|
180 |
+
fn=gradio_interface,
|
181 |
+
inputs=gr.File(label="Upload PDF or Image"),
|
182 |
+
outputs=[
|
183 |
+
gr.Gallery(label="Corrected Images", columns=[2], height="auto"),
|
184 |
+
gr.Gallery(label="Detected Text Lines", columns=[2], height="auto"),
|
185 |
+
],
|
186 |
+
title="PDF/Image Skew Correction and Text Line Detection",
|
187 |
+
description="Upload a PDF or image to correct skew and detect text lines.",
|
188 |
+
)
|
189 |
+
|
190 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|