Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,10 @@ import gradio as gr
|
|
| 2 |
import logging
|
| 3 |
from roboflow import Roboflow
|
| 4 |
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
|
|
|
|
|
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(
|
|
@@ -20,6 +23,36 @@ PROJECT_NAME = "model_verification_project"
|
|
| 20 |
VERSION_NUMBER = 2
|
| 21 |
FONT_PATH = "./STEVEHANDWRITING-REGULAR.TTF"
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Function to process image and overlay text
|
| 24 |
def process_image(image, text):
|
| 25 |
try:
|
|
@@ -54,9 +87,13 @@ def process_image(image, text):
|
|
| 54 |
|
| 55 |
logging.debug(f"Bounding box coordinates: ({x1}, {y1}), ({x2}, {y2}).")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Calculate padded bounding box dimensions
|
| 58 |
-
padding_x = int((x2 - x1) * 0.
|
| 59 |
-
padding_y = int((y2 - y1) * 0.
|
| 60 |
|
| 61 |
x1_padded = x1 + padding_x
|
| 62 |
y1_padded = y1 + padding_y
|
|
@@ -94,14 +131,15 @@ def process_image(image, text):
|
|
| 94 |
total_text_height = len(lines) * line_height
|
| 95 |
return font, line_height
|
| 96 |
|
| 97 |
-
#
|
| 98 |
words = text.split()
|
| 99 |
lines = []
|
| 100 |
current_line = ""
|
|
|
|
| 101 |
for word in words:
|
| 102 |
test_line = f"{current_line} {word}".strip()
|
| 103 |
bbox = text_draw.textbbox((0, 0), test_line, font=font)
|
| 104 |
-
if bbox[2] - bbox[0] <= box_width * 0.
|
| 105 |
current_line = test_line
|
| 106 |
else:
|
| 107 |
lines.append(current_line)
|
|
@@ -122,12 +160,11 @@ def process_image(image, text):
|
|
| 122 |
|
| 123 |
logging.debug(f"Drew text within bounding box: ({x1_padded}, {y1_padded}), ({x2_padded}, {y2_padded}).")
|
| 124 |
|
| 125 |
-
# Apply
|
| 126 |
-
|
| 127 |
-
logging.debug("Applied Gaussian blur to the text layer.")
|
| 128 |
|
| 129 |
-
# Composite the
|
| 130 |
-
pil_image = Image.alpha_composite(pil_image,
|
| 131 |
logging.debug("Composite the text layer onto the original image.")
|
| 132 |
|
| 133 |
# Save and return output image path
|
|
@@ -182,3 +219,4 @@ if __name__ == "__main__":
|
|
| 182 |
|
| 183 |
|
| 184 |
|
|
|
|
|
|
| 2 |
import logging
|
| 3 |
from roboflow import Roboflow
|
| 4 |
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
import os
|
| 8 |
+
from math import atan2, degrees
|
| 9 |
|
| 10 |
# Configure logging
|
| 11 |
logging.basicConfig(
|
|
|
|
| 23 |
VERSION_NUMBER = 2
|
| 24 |
FONT_PATH = "./STEVEHANDWRITING-REGULAR.TTF"
|
| 25 |
|
| 26 |
+
# Function to detect paper angle within bounding box
|
| 27 |
+
def detect_paper_angle(image, bounding_box):
|
| 28 |
+
x1, y1, x2, y2 = bounding_box
|
| 29 |
+
|
| 30 |
+
# Crop the region of interest (ROI) based on the bounding box
|
| 31 |
+
roi = np.array(image)[y1:y2, x1:x2]
|
| 32 |
+
|
| 33 |
+
# Convert ROI to grayscale
|
| 34 |
+
gray = cv2.cvtColor(roi, cv2.COLOR_RGBA2GRAY)
|
| 35 |
+
|
| 36 |
+
# Apply edge detection
|
| 37 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 38 |
+
|
| 39 |
+
# Detect lines using Hough Line Transformation
|
| 40 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
| 41 |
+
|
| 42 |
+
if lines is not None:
|
| 43 |
+
# Find the longest line (most prominent edge)
|
| 44 |
+
longest_line = max(lines, key=lambda line: np.linalg.norm((line[0][2] - line[0][0], line[0][3] - line[0][1])))
|
| 45 |
+
x1, y1, x2, y2 = longest_line[0]
|
| 46 |
+
|
| 47 |
+
# Calculate the angle of the line relative to the horizontal axis
|
| 48 |
+
dx = x2 - x1
|
| 49 |
+
dy = y2 - y1
|
| 50 |
+
angle = degrees(atan2(dy, dx))
|
| 51 |
+
|
| 52 |
+
return angle # Angle of the paper
|
| 53 |
+
else:
|
| 54 |
+
return 0 # Default to no rotation if no lines are found
|
| 55 |
+
|
| 56 |
# Function to process image and overlay text
|
| 57 |
def process_image(image, text):
|
| 58 |
try:
|
|
|
|
| 87 |
|
| 88 |
logging.debug(f"Bounding box coordinates: ({x1}, {y1}), ({x2}, {y2}).")
|
| 89 |
|
| 90 |
+
# Detect the angle of the paper
|
| 91 |
+
angle = detect_paper_angle(np.array(image), (x1, y1, x2, y2))
|
| 92 |
+
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 93 |
+
|
| 94 |
# Calculate padded bounding box dimensions
|
| 95 |
+
padding_x = int((x2 - x1) * 0.1) # 10% padding horizontally
|
| 96 |
+
padding_y = int((y2 - y1) * 0.1) # 10% padding vertically
|
| 97 |
|
| 98 |
x1_padded = x1 + padding_x
|
| 99 |
y1_padded = y1 + padding_y
|
|
|
|
| 131 |
total_text_height = len(lines) * line_height
|
| 132 |
return font, line_height
|
| 133 |
|
| 134 |
+
# Improved text wrapping logic to fit within the bounding box
|
| 135 |
words = text.split()
|
| 136 |
lines = []
|
| 137 |
current_line = ""
|
| 138 |
+
|
| 139 |
for word in words:
|
| 140 |
test_line = f"{current_line} {word}".strip()
|
| 141 |
bbox = text_draw.textbbox((0, 0), test_line, font=font)
|
| 142 |
+
if bbox[2] - bbox[0] <= box_width * 0.9:
|
| 143 |
current_line = test_line
|
| 144 |
else:
|
| 145 |
lines.append(current_line)
|
|
|
|
| 160 |
|
| 161 |
logging.debug(f"Drew text within bounding box: ({x1_padded}, {y1_padded}), ({x2_padded}, {y2_padded}).")
|
| 162 |
|
| 163 |
+
# Apply rotation to align with the paper's angle
|
| 164 |
+
rotated_text_layer = text_layer.rotate(-angle, resample=Image.BICUBIC, center=(x1 + box_width / 2, y1 + box_height / 2))
|
|
|
|
| 165 |
|
| 166 |
+
# Composite the rotated text layer onto the original image
|
| 167 |
+
pil_image = Image.alpha_composite(pil_image, rotated_text_layer)
|
| 168 |
logging.debug("Composite the text layer onto the original image.")
|
| 169 |
|
| 170 |
# Save and return output image path
|
|
|
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
+
|