Update app.py
Browse files
app.py
CHANGED
|
@@ -1,145 +1,326 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
-
import torch
|
| 4 |
-
from doctr.models import ocr_predictor
|
| 5 |
-
from doctr.utils.visualization import visualize_page
|
| 6 |
-
import tempfile
|
| 7 |
import cv2
|
|
|
|
| 8 |
from PIL import Image
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
)
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
if input_image_pil is None:
|
| 26 |
-
return None, None
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
print(f"Visualization error: {e}")
|
| 44 |
-
# Fallback: return original image
|
| 45 |
-
final_image_rgb = input_image_numpy
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp_file:
|
| 52 |
-
cv2.imwrite(tmp_file.name, final_image_bgr)
|
| 53 |
-
return final_image_rgb, tmp_file.name
|
| 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 |
-
def extract_text_from_image(input_image_pil):
|
| 97 |
"""
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
"""
|
| 100 |
if input_image_pil is None:
|
| 101 |
-
return None, None, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
# Gradio Interface
|
| 129 |
-
# ==============================================================================
|
| 130 |
-
demo = gr.Interface(
|
| 131 |
-
fn=extract_text_from_image,
|
| 132 |
-
inputs=gr.Image(type="pil", label="Upload Document Photo"),
|
| 133 |
-
outputs=[
|
| 134 |
-
gr.Image(type="numpy", label="Text Detection Visualization"),
|
| 135 |
-
gr.File(label="Download Visualization"),
|
| 136 |
-
gr.Textbox(label="Extracted Text", lines=10)
|
| 137 |
-
],
|
| 138 |
-
title="π AI-Powered Document Scanner & OCR",
|
| 139 |
-
description="Upload a document image to detect and extract text using the Doctr deep learning library. The tool will show detected text regions and provide the extracted text.",
|
| 140 |
-
flagging_options=None
|
| 141 |
-
)
|
| 142 |
|
|
|
|
| 143 |
if __name__ == "__main__":
|
| 144 |
-
demo.launch()
|
| 145 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import cv2
|
| 4 |
+
import tempfile
|
| 5 |
from PIL import Image
|
| 6 |
+
import math
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
def order_points(pts):
|
| 10 |
+
"""Order points in top-left, top-right, bottom-right, bottom-left order"""
|
| 11 |
+
rect = np.zeros((4, 2), dtype="float32")
|
| 12 |
+
|
| 13 |
+
# Sum and difference to find corners
|
| 14 |
+
s = pts.sum(axis=1)
|
| 15 |
+
diff = np.diff(pts, axis=1)
|
| 16 |
+
|
| 17 |
+
rect[0] = pts[np.argmin(s)] # top-left
|
| 18 |
+
rect[2] = pts[np.argmax(s)] # bottom-right
|
| 19 |
+
rect[1] = pts[np.argmin(diff)] # top-right
|
| 20 |
+
rect[3] = pts[np.argmax(diff)] # bottom-left
|
| 21 |
+
|
| 22 |
+
return rect
|
| 23 |
|
| 24 |
+
def four_point_transform(image, pts):
|
| 25 |
+
"""Apply perspective transformation to get bird's eye view"""
|
| 26 |
+
rect = order_points(pts)
|
| 27 |
+
(tl, tr, br, bl) = rect
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Compute width of new image
|
| 30 |
+
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
|
| 31 |
+
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
|
| 32 |
+
maxWidth = max(int(widthA), int(widthB))
|
| 33 |
|
| 34 |
+
# Compute height of new image
|
| 35 |
+
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
|
| 36 |
+
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
|
| 37 |
+
maxHeight = max(int(heightA), int(heightB))
|
| 38 |
|
| 39 |
+
# Ensure minimum dimensions
|
| 40 |
+
maxWidth = max(maxWidth, 100)
|
| 41 |
+
maxHeight = max(maxHeight, 100)
|
| 42 |
|
| 43 |
+
# Destination points for perspective transform
|
| 44 |
+
dst = np.array([
|
| 45 |
+
[0, 0],
|
| 46 |
+
[maxWidth - 1, 0],
|
| 47 |
+
[maxWidth - 1, maxHeight - 1],
|
| 48 |
+
[0, maxHeight - 1]], dtype="float32")
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Perspective transformation
|
| 51 |
+
M = cv2.getPerspectiveTransform(rect, dst)
|
| 52 |
+
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
|
| 53 |
|
| 54 |
+
return warped
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
def detect_document_edges(image):
|
| 57 |
+
"""Detect document edges using contour detection"""
|
| 58 |
+
# Convert to grayscale
|
| 59 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 60 |
+
|
| 61 |
+
# Apply Gaussian blur
|
| 62 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 63 |
+
|
| 64 |
+
# Edge detection
|
| 65 |
+
edged = cv2.Canny(blurred, 75, 200)
|
| 66 |
+
|
| 67 |
+
# Morphological operations to close gaps
|
| 68 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
|
| 69 |
+
edged = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
|
| 70 |
+
|
| 71 |
+
# Find contours
|
| 72 |
+
contours, _ = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 73 |
+
|
| 74 |
+
if not contours:
|
| 75 |
+
# Fallback to image corners
|
| 76 |
+
h, w = image.shape[:2]
|
| 77 |
+
return np.array([[0, 0], [w-1, 0], [w-1, h-1], [0, h-1]], dtype="float32")
|
| 78 |
+
|
| 79 |
+
# Sort contours by area (largest first)
|
| 80 |
+
contours = sorted(contours, key=cv2.contourArea, reverse=True)
|
| 81 |
+
|
| 82 |
+
# Find the largest rectangular contour
|
| 83 |
+
for contour in contours:
|
| 84 |
+
# Skip very small contours
|
| 85 |
+
if cv2.contourArea(contour) < 1000:
|
| 86 |
+
continue
|
| 87 |
+
|
| 88 |
+
# Approximate contour
|
| 89 |
+
epsilon = 0.02 * cv2.arcLength(contour, True)
|
| 90 |
+
approx = cv2.approxPolyDP(contour, epsilon, True)
|
| 91 |
+
|
| 92 |
+
# If we found a 4-sided contour, it's likely our document
|
| 93 |
+
if len(approx) == 4:
|
| 94 |
+
return approx.reshape(4, 2).astype("float32")
|
| 95 |
+
|
| 96 |
+
# If no rectangular contour found, use image corners
|
| 97 |
+
h, w = image.shape[:2]
|
| 98 |
+
return np.array([[0, 0], [w-1, 0], [w-1, h-1], [0, h-1]], dtype="float32")
|
| 99 |
+
|
| 100 |
+
def enhance_document(image):
|
| 101 |
+
"""Enhance the document image for better readability"""
|
| 102 |
+
try:
|
| 103 |
+
# Convert to LAB color space
|
| 104 |
+
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
|
| 105 |
+
l, a, b = cv2.split(lab)
|
| 106 |
+
|
| 107 |
+
# Apply CLAHE to L channel
|
| 108 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
| 109 |
+
l = clahe.apply(l)
|
| 110 |
+
|
| 111 |
+
# Merge channels and convert back to RGB
|
| 112 |
+
enhanced = cv2.merge([l, a, b])
|
| 113 |
+
enhanced = cv2.cvtColor(enhanced, cv2.COLOR_LAB2RGB)
|
| 114 |
+
|
| 115 |
+
return enhanced
|
| 116 |
+
except:
|
| 117 |
+
# Fallback: simple contrast enhancement
|
| 118 |
+
return cv2.convertScaleAbs(image, alpha=1.2, beta=10)
|
| 119 |
+
|
| 120 |
+
def auto_rotate_image(image):
|
| 121 |
+
"""Auto-rotate image to correct orientation using text line detection"""
|
| 122 |
+
try:
|
| 123 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 124 |
+
|
| 125 |
+
# Detect lines using HoughLinesP
|
| 126 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
| 127 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=100, maxLineGap=10)
|
| 128 |
+
|
| 129 |
+
if lines is not None and len(lines) > 0:
|
| 130 |
+
angles = []
|
| 131 |
+
for line in lines:
|
| 132 |
+
x1, y1, x2, y2 = line[0]
|
| 133 |
+
angle = math.atan2(y2 - y1, x2 - x1)
|
| 134 |
+
angles.append(angle)
|
| 135 |
+
|
| 136 |
+
# Get median angle
|
| 137 |
+
if angles:
|
| 138 |
+
median_angle = np.median(angles)
|
| 139 |
+
angle_deg = np.degrees(median_angle)
|
| 140 |
|
| 141 |
+
# Correct angle to nearest 90-degree orientation
|
| 142 |
+
if angle_deg > 45:
|
| 143 |
+
angle_deg -= 90
|
| 144 |
+
elif angle_deg < -45:
|
| 145 |
+
angle_deg += 90
|
| 146 |
|
| 147 |
+
# Rotate image if significant rotation detected
|
| 148 |
+
if abs(angle_deg) > 1: # Only rotate if angle > 1 degree
|
| 149 |
+
h, w = image.shape[:2]
|
| 150 |
+
center = (w // 2, h // 2)
|
| 151 |
+
M = cv2.getRotationMatrix2D(center, angle_deg, 1.0)
|
| 152 |
+
|
| 153 |
+
# Calculate new image dimensions
|
| 154 |
+
cos = np.abs(M[0, 0])
|
| 155 |
+
sin = np.abs(M[0, 1])
|
| 156 |
+
new_w = int((h * sin) + (w * cos))
|
| 157 |
+
new_h = int((h * cos) + (w * sin))
|
| 158 |
+
|
| 159 |
+
# Adjust rotation matrix for new center
|
| 160 |
+
M[0, 2] += (new_w / 2) - center[0]
|
| 161 |
+
M[1, 2] += (new_h / 2) - center[1]
|
| 162 |
+
|
| 163 |
+
rotated = cv2.warpAffine(image, M, (new_w, new_h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REFLECT)
|
| 164 |
+
return rotated
|
| 165 |
+
except Exception as e:
|
| 166 |
+
print(f"Auto-rotation failed: {e}")
|
| 167 |
+
|
| 168 |
+
return image
|
| 169 |
|
| 170 |
+
def scan_document(input_image_pil):
|
|
|
|
| 171 |
"""
|
| 172 |
+
Complete document scanning pipeline:
|
| 173 |
+
1. Detect document edges
|
| 174 |
+
2. Apply perspective correction
|
| 175 |
+
3. Auto-rotate for correct orientation
|
| 176 |
+
4. Enhance image quality
|
| 177 |
"""
|
| 178 |
if input_image_pil is None:
|
| 179 |
+
return None, None, "β No image uploaded"
|
| 180 |
+
|
| 181 |
+
try:
|
| 182 |
+
# Convert PIL to numpy array
|
| 183 |
+
image = np.array(input_image_pil)
|
| 184 |
+
original_image = image.copy()
|
| 185 |
+
|
| 186 |
+
# Validate image
|
| 187 |
+
if image.size == 0:
|
| 188 |
+
return original_image, None, "β Invalid image"
|
| 189 |
+
|
| 190 |
+
# Step 1: Auto-rotate to correct orientation
|
| 191 |
+
print("π Auto-rotating image...")
|
| 192 |
+
rotated_image = auto_rotate_image(image)
|
| 193 |
+
|
| 194 |
+
# Step 2: Detect document edges
|
| 195 |
+
print("π Detecting document edges...")
|
| 196 |
+
edges = detect_document_edges(rotated_image)
|
| 197 |
+
|
| 198 |
+
# Step 3: Apply perspective transformation
|
| 199 |
+
print("βοΈ Applying perspective correction...")
|
| 200 |
+
scanned = four_point_transform(rotated_image, edges)
|
| 201 |
+
|
| 202 |
+
# Step 4: Enhance the scanned document
|
| 203 |
+
print("β¨ Enhancing document...")
|
| 204 |
+
enhanced = enhance_document(scanned)
|
| 205 |
+
|
| 206 |
+
# Save to temporary file
|
| 207 |
+
enhanced_bgr = cv2.cvtColor(enhanced, cv2.COLOR_RGB2BGR)
|
| 208 |
+
temp_path = tempfile.mktemp(suffix=".jpg")
|
| 209 |
+
cv2.imwrite(temp_path, enhanced_bgr)
|
| 210 |
+
|
| 211 |
+
return enhanced, temp_path, "β
Document scanned successfully!"
|
| 212 |
+
|
| 213 |
+
except Exception as e:
|
| 214 |
+
print(f"Error in scan_document: {e}")
|
| 215 |
+
return original_image if 'original_image' in locals() else None, None, f"β Error: {str(e)}"
|
| 216 |
+
|
| 217 |
+
# Custom CSS for better UI
|
| 218 |
+
custom_css = """
|
| 219 |
+
#image_upload {
|
| 220 |
+
max-height: 400px !important;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
.gradio-container {
|
| 224 |
+
max-width: 1200px !important;
|
| 225 |
+
margin: auto !important;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
#output_image {
|
| 229 |
+
max-height: 500px !important;
|
| 230 |
+
}
|
| 231 |
+
|
| 232 |
+
.primary {
|
| 233 |
+
background: linear-gradient(45deg, #4CAF50, #45a049) !important;
|
| 234 |
+
border: none !important;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
.primary:hover {
|
| 238 |
+
background: linear-gradient(45deg, #45a049, #4CAF50) !important;
|
| 239 |
+
transform: translateY(-2px) !important;
|
| 240 |
+
}
|
| 241 |
+
"""
|
| 242 |
+
|
| 243 |
+
# Create Gradio interface
|
| 244 |
+
with gr.Blocks(css=custom_css, title="π AI Document Scanner", theme=gr.themes.Soft()) as demo:
|
| 245 |
|
| 246 |
+
gr.HTML("""
|
| 247 |
+
<div style="text-align: center; margin-bottom: 30px;">
|
| 248 |
+
<h1 style="color: #2E7D32; font-size: 2.5em; margin-bottom: 10px;">π AI Document Scanner</h1>
|
| 249 |
+
<p style="color: #666; font-size: 1.2em;">Professional document scanning with automatic perspective correction, rotation, and enhancement</p>
|
| 250 |
+
</div>
|
| 251 |
+
""")
|
| 252 |
|
| 253 |
+
with gr.Row():
|
| 254 |
+
with gr.Column(scale=1):
|
| 255 |
+
gr.HTML("<h3 style='color: #1976D2; text-align: center;'>π€ Upload Document</h3>")
|
| 256 |
+
|
| 257 |
+
input_image = gr.Image(
|
| 258 |
+
type="pil",
|
| 259 |
+
label="Upload your document photo",
|
| 260 |
+
elem_id="image_upload",
|
| 261 |
+
height=400,
|
| 262 |
+
sources=["upload", "webcam"]
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
scan_btn = gr.Button(
|
| 266 |
+
"π Scan Document",
|
| 267 |
+
variant="primary",
|
| 268 |
+
size="lg"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
status_text = gr.Textbox(
|
| 272 |
+
label="π Status",
|
| 273 |
+
value="Ready to scan documents",
|
| 274 |
+
interactive=False,
|
| 275 |
+
lines=2
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
with gr.Column(scale=1):
|
| 279 |
+
gr.HTML("<h3 style='color: #1976D2; text-align: center;'>π Scanned Result</h3>")
|
| 280 |
+
|
| 281 |
+
output_image = gr.Image(
|
| 282 |
+
type="numpy",
|
| 283 |
+
label="Scanned Document",
|
| 284 |
+
elem_id="output_image",
|
| 285 |
+
height=400
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
download_file = gr.File(
|
| 289 |
+
label="π₯ Download Scanned Document"
|
| 290 |
+
)
|
| 291 |
|
| 292 |
+
# Features section
|
| 293 |
+
gr.HTML("""
|
| 294 |
+
<div style="margin-top: 30px; padding: 20px; background: linear-gradient(135deg, #E8F5E8, #F0F8FF); border-radius: 15px;">
|
| 295 |
+
<h3 style="color: #2E7D32; text-align: center; margin-bottom: 15px;">β¨ Key Features</h3>
|
| 296 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 15px;">
|
| 297 |
+
<div style="text-align: center;">
|
| 298 |
+
<span style="font-size: 2em;">π</span>
|
| 299 |
+
<p><strong>Auto Rotation</strong><br>Automatically detects and corrects orientation</p>
|
| 300 |
+
</div>
|
| 301 |
+
<div style="text-align: center;">
|
| 302 |
+
<span style="font-size: 2em;">π</span>
|
| 303 |
+
<p><strong>Perspective Correction</strong><br>Straightens tilted and skewed documents</p>
|
| 304 |
+
</div>
|
| 305 |
+
<div style="text-align: center;">
|
| 306 |
+
<span style="font-size: 2em;">βοΈ</span>
|
| 307 |
+
<p><strong>Smart Cropping</strong><br>Automatically crops to document boundaries</p>
|
| 308 |
+
</div>
|
| 309 |
+
<div style="text-align: center;">
|
| 310 |
+
<span style="font-size: 2em;">β¨</span>
|
| 311 |
+
<p><strong>Enhancement</strong><br>Improves contrast and readability</p>
|
| 312 |
+
</div>
|
| 313 |
+
</div>
|
| 314 |
+
</div>
|
| 315 |
+
""")
|
| 316 |
|
| 317 |
+
# Set up the scanning function
|
| 318 |
+
scan_btn.click(
|
| 319 |
+
fn=scan_document,
|
| 320 |
+
inputs=[input_image],
|
| 321 |
+
outputs=[output_image, download_file, status_text]
|
| 322 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
|
| 324 |
+
# Launch the app
|
| 325 |
if __name__ == "__main__":
|
| 326 |
+
demo.launch()
|
|
|