Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,13 +7,12 @@ import numpy as np
|
|
7 |
import os
|
8 |
from math import atan2, degrees
|
9 |
import asyncio
|
10 |
-
from pyppeteer import launch
|
11 |
import multiprocessing
|
12 |
|
13 |
# Configure logging
|
14 |
logging.basicConfig(
|
15 |
level=logging.DEBUG,
|
16 |
-
format=
|
17 |
handlers=[
|
18 |
logging.FileHandler("debug.log"),
|
19 |
logging.StreamHandler()
|
@@ -21,30 +20,40 @@ logging.basicConfig(
|
|
21 |
)
|
22 |
|
23 |
# Roboflow and model configuration
|
24 |
-
ROBOFLOW_API_KEY = "KUP9w62eUcD5PrrRMJsV" # Replace with your API key
|
25 |
PROJECT_NAME = "model_verification_project"
|
26 |
VERSION_NUMBER = 2
|
27 |
|
28 |
# ----------------------------
|
29 |
-
#
|
30 |
# ----------------------------
|
31 |
def generate_handwriting_image_process(text_prompt, screenshot_path, return_dict):
|
32 |
"""
|
33 |
-
This function runs in a separate process so that
|
34 |
-
|
35 |
"""
|
36 |
import asyncio
|
37 |
from pyppeteer import launch
|
38 |
|
39 |
async def _generate():
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
page = await browser.newPage()
|
42 |
await page.goto('https://www.calligraphr.com/en/font/', {'waitUntil': 'networkidle2'})
|
43 |
await page.waitForSelector('#text-input')
|
44 |
await page.type('#text-input', text_prompt)
|
45 |
await asyncio.sleep(2) # Wait for the handwriting preview to render
|
46 |
|
47 |
-
# Adjust
|
48 |
await page.screenshot({
|
49 |
'path': screenshot_path,
|
50 |
'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
|
@@ -55,13 +64,25 @@ def generate_handwriting_image_process(text_prompt, screenshot_path, return_dict
|
|
55 |
# Create a new event loop for this process
|
56 |
loop = asyncio.new_event_loop()
|
57 |
asyncio.set_event_loop(loop)
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
def get_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
|
|
|
|
|
|
62 |
manager = multiprocessing.Manager()
|
63 |
return_dict = manager.dict()
|
64 |
-
process = multiprocessing.Process(
|
|
|
|
|
|
|
65 |
process.start()
|
66 |
process.join()
|
67 |
return return_dict.get('result', None)
|
@@ -76,7 +97,9 @@ def detect_paper_angle(image, bounding_box):
|
|
76 |
edges = cv2.Canny(gray, 50, 150)
|
77 |
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
78 |
if lines is not None:
|
79 |
-
longest_line = max(
|
|
|
|
|
80 |
x1_line, y1_line, x2_line, y2_line = longest_line[0]
|
81 |
dx = x2_line - x1_line
|
82 |
dy = y2_line - y1_line
|
@@ -108,6 +131,7 @@ def process_image(image, text):
|
|
108 |
prediction = model.predict(input_image_path, confidence=70, overlap=50).json()
|
109 |
logging.debug(f"Inference result: {prediction}")
|
110 |
|
|
|
111 |
pil_image = image.convert("RGBA")
|
112 |
logging.debug("Converted image to RGBA mode.")
|
113 |
|
@@ -126,10 +150,11 @@ def process_image(image, text):
|
|
126 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
127 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
128 |
|
|
|
129 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
130 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
131 |
|
132 |
-
#
|
133 |
debug_layer = pil_image.copy()
|
134 |
debug_draw = ImageDraw.Draw(debug_layer)
|
135 |
debug_draw.rectangle([(x1_padded, y1_padded), (x2_padded, y2_padded)], outline="red", width=3)
|
@@ -146,6 +171,7 @@ def process_image(image, text):
|
|
146 |
handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
|
147 |
rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
|
148 |
|
|
|
149 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
150 |
paste_x = int(obj['x'] - rotated_handwriting.size[0] / 2)
|
151 |
paste_y = int(obj['y'] - rotated_handwriting.size[1] / 2)
|
@@ -153,6 +179,7 @@ def process_image(image, text):
|
|
153 |
pil_image = Image.alpha_composite(pil_image, text_layer)
|
154 |
logging.debug("Handwriting layer composited onto the original image.")
|
155 |
|
|
|
156 |
output_image_path = "/tmp/output_image.png"
|
157 |
pil_image.convert("RGB").save(output_image_path)
|
158 |
logging.debug(f"Output image saved to {output_image_path}.")
|
|
|
7 |
import os
|
8 |
from math import atan2, degrees
|
9 |
import asyncio
|
|
|
10 |
import multiprocessing
|
11 |
|
12 |
# Configure logging
|
13 |
logging.basicConfig(
|
14 |
level=logging.DEBUG,
|
15 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
16 |
handlers=[
|
17 |
logging.FileHandler("debug.log"),
|
18 |
logging.StreamHandler()
|
|
|
20 |
)
|
21 |
|
22 |
# Roboflow and model configuration
|
23 |
+
ROBOFLOW_API_KEY = "KUP9w62eUcD5PrrRMJsV" # Replace with your API key if needed
|
24 |
PROJECT_NAME = "model_verification_project"
|
25 |
VERSION_NUMBER = 2
|
26 |
|
27 |
# ----------------------------
|
28 |
+
# Function to generate handwriting image using Pyppeteer in a separate process
|
29 |
# ----------------------------
|
30 |
def generate_handwriting_image_process(text_prompt, screenshot_path, return_dict):
|
31 |
"""
|
32 |
+
This function runs in a separate process so that Pyppeteer's signal handling
|
33 |
+
works correctly in its main thread.
|
34 |
"""
|
35 |
import asyncio
|
36 |
from pyppeteer import launch
|
37 |
|
38 |
async def _generate():
|
39 |
+
# Launch Chromium with additional flags for containerized environments
|
40 |
+
browser = await launch(
|
41 |
+
headless=True,
|
42 |
+
args=[
|
43 |
+
'--no-sandbox',
|
44 |
+
'--disable-setuid-sandbox',
|
45 |
+
'--disable-dev-shm-usage',
|
46 |
+
'--disable-gpu',
|
47 |
+
'--single-process'
|
48 |
+
]
|
49 |
+
)
|
50 |
page = await browser.newPage()
|
51 |
await page.goto('https://www.calligraphr.com/en/font/', {'waitUntil': 'networkidle2'})
|
52 |
await page.waitForSelector('#text-input')
|
53 |
await page.type('#text-input', text_prompt)
|
54 |
await asyncio.sleep(2) # Wait for the handwriting preview to render
|
55 |
|
56 |
+
# Adjust the clip values as needed to capture the proper area of the page
|
57 |
await page.screenshot({
|
58 |
'path': screenshot_path,
|
59 |
'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
|
|
|
64 |
# Create a new event loop for this process
|
65 |
loop = asyncio.new_event_loop()
|
66 |
asyncio.set_event_loop(loop)
|
67 |
+
try:
|
68 |
+
result = loop.run_until_complete(_generate())
|
69 |
+
return_dict['result'] = result
|
70 |
+
except Exception as e:
|
71 |
+
logging.error("Error in handwriting generation process: " + str(e))
|
72 |
+
return_dict['result'] = None
|
73 |
+
finally:
|
74 |
+
loop.close()
|
75 |
|
76 |
def get_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
77 |
+
"""
|
78 |
+
Starts a separate process to generate a handwriting image and returns the image path.
|
79 |
+
"""
|
80 |
manager = multiprocessing.Manager()
|
81 |
return_dict = manager.dict()
|
82 |
+
process = multiprocessing.Process(
|
83 |
+
target=generate_handwriting_image_process,
|
84 |
+
args=(text_prompt, screenshot_path, return_dict)
|
85 |
+
)
|
86 |
process.start()
|
87 |
process.join()
|
88 |
return return_dict.get('result', None)
|
|
|
97 |
edges = cv2.Canny(gray, 50, 150)
|
98 |
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
99 |
if lines is not None:
|
100 |
+
longest_line = max(
|
101 |
+
lines, key=lambda line: np.linalg.norm((line[0][2] - line[0][0], line[0][3] - line[0][1]))
|
102 |
+
)
|
103 |
x1_line, y1_line, x2_line, y2_line = longest_line[0]
|
104 |
dx = x2_line - x1_line
|
105 |
dy = y2_line - y1_line
|
|
|
131 |
prediction = model.predict(input_image_path, confidence=70, overlap=50).json()
|
132 |
logging.debug(f"Inference result: {prediction}")
|
133 |
|
134 |
+
# Convert image for processing
|
135 |
pil_image = image.convert("RGBA")
|
136 |
logging.debug("Converted image to RGBA mode.")
|
137 |
|
|
|
150 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
151 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
152 |
|
153 |
+
# Detect paper angle
|
154 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
155 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
156 |
|
157 |
+
# (Optional) Save a debug image with the bounding box drawn
|
158 |
debug_layer = pil_image.copy()
|
159 |
debug_draw = ImageDraw.Draw(debug_layer)
|
160 |
debug_draw.rectangle([(x1_padded, y1_padded), (x2_padded, y2_padded)], outline="red", width=3)
|
|
|
171 |
handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
|
172 |
rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
|
173 |
|
174 |
+
# Composite the handwriting onto the original image
|
175 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
176 |
paste_x = int(obj['x'] - rotated_handwriting.size[0] / 2)
|
177 |
paste_y = int(obj['y'] - rotated_handwriting.size[1] / 2)
|
|
|
179 |
pil_image = Image.alpha_composite(pil_image, text_layer)
|
180 |
logging.debug("Handwriting layer composited onto the original image.")
|
181 |
|
182 |
+
# Save and return the output image
|
183 |
output_image_path = "/tmp/output_image.png"
|
184 |
pil_image.convert("RGB").save(output_image_path)
|
185 |
logging.debug(f"Output image saved to {output_image_path}.")
|