Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,3 @@
|
|
1 |
-
# Install system dependencies first
|
2 |
-
|
3 |
-
|
4 |
import nest_asyncio
|
5 |
nest_asyncio.apply()
|
6 |
|
@@ -26,19 +23,15 @@ logging.basicConfig(
|
|
26 |
)
|
27 |
|
28 |
# Roboflow and model configuration
|
29 |
-
ROBOFLOW_API_KEY = "KUP9w62eUcD5PrrRMJsV"
|
30 |
PROJECT_NAME = "model_verification_project"
|
31 |
VERSION_NUMBER = 2
|
32 |
|
33 |
-
# ----------------------------
|
34 |
-
# Asynchronous function to generate handwriting image via Pyppeteer
|
35 |
-
# ----------------------------
|
36 |
async def _generate_handwriting_image(text_prompt, screenshot_path):
|
37 |
try:
|
38 |
-
# Launch Chromium with the correct path
|
39 |
browser = await launch(
|
40 |
headless=True,
|
41 |
-
executablePath="/usr/bin/chromium-browser", #
|
42 |
args=[
|
43 |
'--no-sandbox',
|
44 |
'--disable-setuid-sandbox',
|
@@ -54,7 +47,7 @@ async def _generate_handwriting_image(text_prompt, screenshot_path):
|
|
54 |
# Navigate to Calligraphr
|
55 |
await page.goto('https://www.calligraphr.com/en/font/', {
|
56 |
'waitUntil': 'networkidle2',
|
57 |
-
'timeout': 60000
|
58 |
})
|
59 |
|
60 |
# Wait for the text input field
|
@@ -78,14 +71,10 @@ async def _generate_handwriting_image(text_prompt, screenshot_path):
|
|
78 |
return None
|
79 |
|
80 |
finally:
|
81 |
-
# Close the browser
|
82 |
if 'browser' in locals():
|
83 |
await browser.close()
|
84 |
|
85 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
86 |
-
"""
|
87 |
-
Synchronous wrapper around the async Pyppeteer call.
|
88 |
-
"""
|
89 |
try:
|
90 |
loop = asyncio.get_event_loop()
|
91 |
result = loop.run_until_complete(_generate_handwriting_image(text_prompt, screenshot_path))
|
@@ -94,9 +83,24 @@ def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.pn
|
|
94 |
logging.error(f"Error generating handwriting image: {e}")
|
95 |
return None
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
def process_image(image, text):
|
101 |
try:
|
102 |
# Initialize Roboflow
|
@@ -120,26 +124,20 @@ def process_image(image, text):
|
|
120 |
pil_image = image.convert("RGBA")
|
121 |
logging.debug("Converted image to RGBA mode.")
|
122 |
|
123 |
-
# Iterate over detected objects (assumed white paper)
|
124 |
for obj in prediction['predictions']:
|
125 |
-
# Paper dimensions
|
126 |
white_paper_width = obj['width']
|
127 |
white_paper_height = obj['height']
|
128 |
-
|
129 |
-
# Padding
|
130 |
padding_x = int(white_paper_width * 0.1)
|
131 |
padding_y = int(white_paper_height * 0.1)
|
132 |
box_width = white_paper_width - 2 * padding_x
|
133 |
box_height = white_paper_height - 2 * padding_y
|
134 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
135 |
|
136 |
-
# Calculate padded coordinates
|
137 |
x1_padded = int(obj['x'] - white_paper_width / 2 + padding_x)
|
138 |
y1_padded = int(obj['y'] - white_paper_height / 2 + padding_y)
|
139 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
140 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
141 |
|
142 |
-
# Detect paper angle
|
143 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
144 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
145 |
|
@@ -150,7 +148,6 @@ def process_image(image, text):
|
|
150 |
debug_layer.save("/tmp/debug_bounding_box.png")
|
151 |
logging.debug("Saved bounding box debug image to /tmp/debug_bounding_box.png.")
|
152 |
|
153 |
-
# Generate handwriting image
|
154 |
handwriting_path = generate_handwriting_image(text, "/tmp/handwriting.png")
|
155 |
if not handwriting_path:
|
156 |
logging.error("Handwriting image generation failed.")
|
@@ -160,7 +157,6 @@ def process_image(image, text):
|
|
160 |
handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
|
161 |
rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
|
162 |
|
163 |
-
# Composite the handwriting
|
164 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
165 |
paste_x = int(obj['x'] - rotated_handwriting.size[0] / 2)
|
166 |
paste_y = int(obj['y'] - rotated_handwriting.size[1] / 2)
|
@@ -168,7 +164,6 @@ def process_image(image, text):
|
|
168 |
pil_image = Image.alpha_composite(pil_image, text_layer)
|
169 |
logging.debug("Handwriting layer composited onto the original image.")
|
170 |
|
171 |
-
# Save output
|
172 |
output_image_path = "/tmp/output_image.png"
|
173 |
pil_image.convert("RGB").save(output_image_path)
|
174 |
logging.debug(f"Output image saved to {output_image_path}.")
|
@@ -178,9 +173,15 @@ def process_image(image, text):
|
|
178 |
logging.error(f"Error during image processing: {e}")
|
179 |
return None
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
interface = gr.Interface(
|
185 |
fn=gradio_inference,
|
186 |
inputs=[
|
@@ -201,5 +202,6 @@ if __name__ == "__main__":
|
|
201 |
interface.launch(
|
202 |
server_name="0.0.0.0",
|
203 |
server_port=int(os.environ.get("PORT", 7860)),
|
|
|
204 |
enable_queue=True
|
205 |
-
)
|
|
|
|
|
|
|
|
|
1 |
import nest_asyncio
|
2 |
nest_asyncio.apply()
|
3 |
|
|
|
23 |
)
|
24 |
|
25 |
# Roboflow and model configuration
|
26 |
+
ROBOFLOW_API_KEY = "KUP9w62eUcD5PrrRMJsV"
|
27 |
PROJECT_NAME = "model_verification_project"
|
28 |
VERSION_NUMBER = 2
|
29 |
|
|
|
|
|
|
|
30 |
async def _generate_handwriting_image(text_prompt, screenshot_path):
|
31 |
try:
|
|
|
32 |
browser = await launch(
|
33 |
headless=True,
|
34 |
+
executablePath="/usr/bin/chromium-browser", # Path to Chromium
|
35 |
args=[
|
36 |
'--no-sandbox',
|
37 |
'--disable-setuid-sandbox',
|
|
|
47 |
# Navigate to Calligraphr
|
48 |
await page.goto('https://www.calligraphr.com/en/font/', {
|
49 |
'waitUntil': 'networkidle2',
|
50 |
+
'timeout': 60000
|
51 |
})
|
52 |
|
53 |
# Wait for the text input field
|
|
|
71 |
return None
|
72 |
|
73 |
finally:
|
|
|
74 |
if 'browser' in locals():
|
75 |
await browser.close()
|
76 |
|
77 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
|
|
|
|
|
|
78 |
try:
|
79 |
loop = asyncio.get_event_loop()
|
80 |
result = loop.run_until_complete(_generate_handwriting_image(text_prompt, screenshot_path))
|
|
|
83 |
logging.error(f"Error generating handwriting image: {e}")
|
84 |
return None
|
85 |
|
86 |
+
def detect_paper_angle(image, bounding_box):
|
87 |
+
x1, y1, x2, y2 = bounding_box
|
88 |
+
roi = np.array(image)[y1:y2, x1:x2]
|
89 |
+
gray = cv2.cvtColor(roi, cv2.COLOR_RGBA2GRAY)
|
90 |
+
edges = cv2.Canny(gray, 50, 150)
|
91 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
92 |
+
if lines is not None:
|
93 |
+
longest_line = max(
|
94 |
+
lines, key=lambda line: np.linalg.norm((line[0][2] - line[0][0], line[0][3] - line[0][1]))
|
95 |
+
)
|
96 |
+
x1_line, y1_line, x2_line, y2_line = longest_line[0]
|
97 |
+
dx = x2_line - x1_line
|
98 |
+
dy = y2_line - y1_line
|
99 |
+
angle = degrees(atan2(dy, dx))
|
100 |
+
return angle
|
101 |
+
else:
|
102 |
+
return 0
|
103 |
+
|
104 |
def process_image(image, text):
|
105 |
try:
|
106 |
# Initialize Roboflow
|
|
|
124 |
pil_image = image.convert("RGBA")
|
125 |
logging.debug("Converted image to RGBA mode.")
|
126 |
|
|
|
127 |
for obj in prediction['predictions']:
|
|
|
128 |
white_paper_width = obj['width']
|
129 |
white_paper_height = obj['height']
|
|
|
|
|
130 |
padding_x = int(white_paper_width * 0.1)
|
131 |
padding_y = int(white_paper_height * 0.1)
|
132 |
box_width = white_paper_width - 2 * padding_x
|
133 |
box_height = white_paper_height - 2 * padding_y
|
134 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
135 |
|
|
|
136 |
x1_padded = int(obj['x'] - white_paper_width / 2 + padding_x)
|
137 |
y1_padded = int(obj['y'] - white_paper_height / 2 + padding_y)
|
138 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
139 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
140 |
|
|
|
141 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
142 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
143 |
|
|
|
148 |
debug_layer.save("/tmp/debug_bounding_box.png")
|
149 |
logging.debug("Saved bounding box debug image to /tmp/debug_bounding_box.png.")
|
150 |
|
|
|
151 |
handwriting_path = generate_handwriting_image(text, "/tmp/handwriting.png")
|
152 |
if not handwriting_path:
|
153 |
logging.error("Handwriting image generation failed.")
|
|
|
157 |
handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
|
158 |
rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
|
159 |
|
|
|
160 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
161 |
paste_x = int(obj['x'] - rotated_handwriting.size[0] / 2)
|
162 |
paste_y = int(obj['y'] - rotated_handwriting.size[1] / 2)
|
|
|
164 |
pil_image = Image.alpha_composite(pil_image, text_layer)
|
165 |
logging.debug("Handwriting layer composited onto the original image.")
|
166 |
|
|
|
167 |
output_image_path = "/tmp/output_image.png"
|
168 |
pil_image.convert("RGB").save(output_image_path)
|
169 |
logging.debug(f"Output image saved to {output_image_path}.")
|
|
|
173 |
logging.error(f"Error during image processing: {e}")
|
174 |
return None
|
175 |
|
176 |
+
def gradio_inference(image, text):
|
177 |
+
logging.debug("Starting Gradio inference.")
|
178 |
+
result_path = process_image(image, text)
|
179 |
+
if result_path:
|
180 |
+
logging.debug("Gradio inference successful.")
|
181 |
+
return result_path, result_path, "Processing complete! Download the image below."
|
182 |
+
logging.error("Gradio inference failed.")
|
183 |
+
return None, None, "An error occurred while processing the image. Please check the logs."
|
184 |
+
|
185 |
interface = gr.Interface(
|
186 |
fn=gradio_inference,
|
187 |
inputs=[
|
|
|
202 |
interface.launch(
|
203 |
server_name="0.0.0.0",
|
204 |
server_port=int(os.environ.get("PORT", 7860)),
|
205 |
+
# Remove enable_queue if your Gradio version doesn't support it
|
206 |
enable_queue=True
|
207 |
+
)
|