Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import nest_asyncio
|
| 2 |
nest_asyncio.apply()
|
| 3 |
|
|
@@ -23,16 +34,19 @@ logging.basicConfig(
|
|
| 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 |
-
|
| 35 |
-
executablePath="/usr/bin/chromium-browser",
|
| 36 |
args=[
|
| 37 |
'--no-sandbox',
|
| 38 |
'--disable-setuid-sandbox',
|
|
@@ -44,26 +58,42 @@ async def _generate_handwriting_image(text_prompt, screenshot_path):
|
|
| 44 |
]
|
| 45 |
)
|
| 46 |
page = await browser.newPage()
|
|
|
|
|
|
|
| 47 |
await page.goto('https://www.calligraphr.com/en/font/', {
|
| 48 |
'waitUntil': 'networkidle2',
|
| 49 |
-
'timeout': 60000
|
| 50 |
})
|
|
|
|
|
|
|
| 51 |
await page.waitForSelector('#text-input', {'timeout': 30000})
|
|
|
|
|
|
|
| 52 |
await page.type('#text-input', text_prompt)
|
|
|
|
|
|
|
| 53 |
await asyncio.sleep(5)
|
|
|
|
|
|
|
| 54 |
await page.screenshot({
|
| 55 |
'path': screenshot_path,
|
| 56 |
'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
|
| 57 |
})
|
| 58 |
return screenshot_path
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
logging.error(f"Pyppeteer error: {str(e)}")
|
| 61 |
return None
|
|
|
|
| 62 |
finally:
|
|
|
|
| 63 |
if 'browser' in locals():
|
| 64 |
await browser.close()
|
| 65 |
|
| 66 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
|
|
|
|
|
|
|
|
|
| 67 |
try:
|
| 68 |
loop = asyncio.get_event_loop()
|
| 69 |
result = loop.run_until_complete(_generate_handwriting_image(text_prompt, screenshot_path))
|
|
@@ -72,24 +102,9 @@ def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.pn
|
|
| 72 |
logging.error(f"Error generating handwriting image: {e}")
|
| 73 |
return None
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
gray = cv2.cvtColor(roi, cv2.COLOR_RGBA2GRAY)
|
| 79 |
-
edges = cv2.Canny(gray, 50, 150)
|
| 80 |
-
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
| 81 |
-
if lines is not None:
|
| 82 |
-
longest_line = max(
|
| 83 |
-
lines, key=lambda line: np.linalg.norm((line[0][2] - line[0][0], line[0][3] - line[0][1]))
|
| 84 |
-
)
|
| 85 |
-
x1_line, y1_line, x2_line, y2_line = longest_line[0]
|
| 86 |
-
dx = x2_line - x1_line
|
| 87 |
-
dy = y2_line - y1_line
|
| 88 |
-
angle = degrees(atan2(dy, dx))
|
| 89 |
-
return angle
|
| 90 |
-
else:
|
| 91 |
-
return 0
|
| 92 |
-
|
| 93 |
def process_image(image, text):
|
| 94 |
try:
|
| 95 |
# Initialize Roboflow
|
|
@@ -113,23 +128,30 @@ def process_image(image, text):
|
|
| 113 |
pil_image = image.convert("RGBA")
|
| 114 |
logging.debug("Converted image to RGBA mode.")
|
| 115 |
|
|
|
|
| 116 |
for obj in prediction['predictions']:
|
|
|
|
| 117 |
white_paper_width = obj['width']
|
| 118 |
white_paper_height = obj['height']
|
|
|
|
|
|
|
| 119 |
padding_x = int(white_paper_width * 0.1)
|
| 120 |
padding_y = int(white_paper_height * 0.1)
|
| 121 |
box_width = white_paper_width - 2 * padding_x
|
| 122 |
box_height = white_paper_height - 2 * padding_y
|
| 123 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
| 124 |
|
|
|
|
| 125 |
x1_padded = int(obj['x'] - white_paper_width / 2 + padding_x)
|
| 126 |
y1_padded = int(obj['y'] - white_paper_height / 2 + padding_y)
|
| 127 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
| 128 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
| 129 |
|
|
|
|
| 130 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
| 131 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 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 +168,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 +176,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}.")
|
|
@@ -162,15 +186,9 @@ def process_image(image, text):
|
|
| 162 |
logging.error(f"Error during image processing: {e}")
|
| 163 |
return None
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
if result_path:
|
| 169 |
-
logging.debug("Gradio inference successful.")
|
| 170 |
-
return result_path, result_path, "Processing complete! Download the image below."
|
| 171 |
-
logging.error("Gradio inference failed.")
|
| 172 |
-
return None, None, "An error occurred while processing the image. Please check the logs."
|
| 173 |
-
|
| 174 |
interface = gr.Interface(
|
| 175 |
fn=gradio_inference,
|
| 176 |
inputs=[
|
|
@@ -192,4 +210,4 @@ if __name__ == "__main__":
|
|
| 192 |
server_name="0.0.0.0",
|
| 193 |
server_port=int(os.environ.get("PORT", 7860)),
|
| 194 |
enable_queue=True
|
| 195 |
-
)
|
|
|
|
| 1 |
+
# Install system dependencies first
|
| 2 |
+
!apt-get update
|
| 3 |
+
!apt-get install -y \
|
| 4 |
+
chromium-browser \
|
| 5 |
+
chromium-chromedriver \
|
| 6 |
+
libnss3 \
|
| 7 |
+
libxss1 \
|
| 8 |
+
libatk-bridge2.0-0 \
|
| 9 |
+
libgtk-3-0 \
|
| 10 |
+
libgbm-dev
|
| 11 |
+
|
| 12 |
import nest_asyncio
|
| 13 |
nest_asyncio.apply()
|
| 14 |
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
# Roboflow and model configuration
|
| 37 |
+
ROBOFLOW_API_KEY = "KUP9w62eUcD5PrrRMJsV" # Replace with your API key if needed
|
| 38 |
PROJECT_NAME = "model_verification_project"
|
| 39 |
VERSION_NUMBER = 2
|
| 40 |
|
| 41 |
+
# ----------------------------
|
| 42 |
+
# Asynchronous function to generate handwriting image via Pyppeteer
|
| 43 |
+
# ----------------------------
|
| 44 |
async def _generate_handwriting_image(text_prompt, screenshot_path):
|
| 45 |
try:
|
| 46 |
+
# Launch Chromium with the correct path
|
| 47 |
browser = await launch(
|
| 48 |
headless=True,
|
| 49 |
+
executablePath="/usr/bin/chromium-browser", # Explicit path to Chromium
|
|
|
|
| 50 |
args=[
|
| 51 |
'--no-sandbox',
|
| 52 |
'--disable-setuid-sandbox',
|
|
|
|
| 58 |
]
|
| 59 |
)
|
| 60 |
page = await browser.newPage()
|
| 61 |
+
|
| 62 |
+
# Navigate to Calligraphr
|
| 63 |
await page.goto('https://www.calligraphr.com/en/font/', {
|
| 64 |
'waitUntil': 'networkidle2',
|
| 65 |
+
'timeout': 60000 # 60 seconds timeout
|
| 66 |
})
|
| 67 |
+
|
| 68 |
+
# Wait for the text input field
|
| 69 |
await page.waitForSelector('#text-input', {'timeout': 30000})
|
| 70 |
+
|
| 71 |
+
# Type the text prompt
|
| 72 |
await page.type('#text-input', text_prompt)
|
| 73 |
+
|
| 74 |
+
# Wait for rendering
|
| 75 |
await asyncio.sleep(5)
|
| 76 |
+
|
| 77 |
+
# Take a screenshot
|
| 78 |
await page.screenshot({
|
| 79 |
'path': screenshot_path,
|
| 80 |
'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
|
| 81 |
})
|
| 82 |
return screenshot_path
|
| 83 |
+
|
| 84 |
except Exception as e:
|
| 85 |
logging.error(f"Pyppeteer error: {str(e)}")
|
| 86 |
return None
|
| 87 |
+
|
| 88 |
finally:
|
| 89 |
+
# Close the browser
|
| 90 |
if 'browser' in locals():
|
| 91 |
await browser.close()
|
| 92 |
|
| 93 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
| 94 |
+
"""
|
| 95 |
+
Synchronous wrapper around the async Pyppeteer call.
|
| 96 |
+
"""
|
| 97 |
try:
|
| 98 |
loop = asyncio.get_event_loop()
|
| 99 |
result = loop.run_until_complete(_generate_handwriting_image(text_prompt, screenshot_path))
|
|
|
|
| 102 |
logging.error(f"Error generating handwriting image: {e}")
|
| 103 |
return None
|
| 104 |
|
| 105 |
+
# ----------------------------
|
| 106 |
+
# Main processing function
|
| 107 |
+
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
def process_image(image, text):
|
| 109 |
try:
|
| 110 |
# Initialize Roboflow
|
|
|
|
| 128 |
pil_image = image.convert("RGBA")
|
| 129 |
logging.debug("Converted image to RGBA mode.")
|
| 130 |
|
| 131 |
+
# Iterate over detected objects (assumed white paper)
|
| 132 |
for obj in prediction['predictions']:
|
| 133 |
+
# Paper dimensions
|
| 134 |
white_paper_width = obj['width']
|
| 135 |
white_paper_height = obj['height']
|
| 136 |
+
|
| 137 |
+
# Padding
|
| 138 |
padding_x = int(white_paper_width * 0.1)
|
| 139 |
padding_y = int(white_paper_height * 0.1)
|
| 140 |
box_width = white_paper_width - 2 * padding_x
|
| 141 |
box_height = white_paper_height - 2 * padding_y
|
| 142 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
| 143 |
|
| 144 |
+
# Calculate padded coordinates
|
| 145 |
x1_padded = int(obj['x'] - white_paper_width / 2 + padding_x)
|
| 146 |
y1_padded = int(obj['y'] - white_paper_height / 2 + padding_y)
|
| 147 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
| 148 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
| 149 |
|
| 150 |
+
# Detect paper angle
|
| 151 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
| 152 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 153 |
|
| 154 |
+
# (Optional) debug bounding box
|
| 155 |
debug_layer = pil_image.copy()
|
| 156 |
debug_draw = ImageDraw.Draw(debug_layer)
|
| 157 |
debug_draw.rectangle([(x1_padded, y1_padded), (x2_padded, y2_padded)], outline="red", width=3)
|
|
|
|
| 168 |
handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
|
| 169 |
rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
|
| 170 |
|
| 171 |
+
# Composite the handwriting
|
| 172 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
| 173 |
paste_x = int(obj['x'] - rotated_handwriting.size[0] / 2)
|
| 174 |
paste_y = int(obj['y'] - rotated_handwriting.size[1] / 2)
|
|
|
|
| 176 |
pil_image = Image.alpha_composite(pil_image, text_layer)
|
| 177 |
logging.debug("Handwriting layer composited onto the original image.")
|
| 178 |
|
| 179 |
+
# Save output
|
| 180 |
output_image_path = "/tmp/output_image.png"
|
| 181 |
pil_image.convert("RGB").save(output_image_path)
|
| 182 |
logging.debug(f"Output image saved to {output_image_path}.")
|
|
|
|
| 186 |
logging.error(f"Error during image processing: {e}")
|
| 187 |
return None
|
| 188 |
|
| 189 |
+
# ----------------------------
|
| 190 |
+
# Gradio interface
|
| 191 |
+
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
interface = gr.Interface(
|
| 193 |
fn=gradio_inference,
|
| 194 |
inputs=[
|
|
|
|
| 210 |
server_name="0.0.0.0",
|
| 211 |
server_port=int(os.environ.get("PORT", 7860)),
|
| 212 |
enable_queue=True
|
| 213 |
+
)
|