Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
!apt-get install -y chromium-browser chromium-chromedriver libnss3 libxss1 libatk-bridge2.0-0 libgtk-3-0 libgbm-dev
|
| 2 |
-
|
| 3 |
import nest_asyncio
|
| 4 |
nest_asyncio.apply()
|
| 5 |
|
|
@@ -25,17 +23,15 @@ logging.basicConfig(
|
|
| 25 |
)
|
| 26 |
|
| 27 |
# Roboflow and model configuration
|
| 28 |
-
ROBOFLOW_API_KEY = "KUP9w62eUcD5PrrRMJsV"
|
| 29 |
PROJECT_NAME = "model_verification_project"
|
| 30 |
VERSION_NUMBER = 2
|
| 31 |
|
| 32 |
-
# ----------------------------
|
| 33 |
-
# Asynchronous function to generate handwriting image via Pyppeteer
|
| 34 |
-
# ----------------------------
|
| 35 |
async def _generate_handwriting_image(text_prompt, screenshot_path):
|
| 36 |
try:
|
| 37 |
browser = await launch(
|
| 38 |
headless=True,
|
|
|
|
| 39 |
executablePath="/usr/bin/chromium-browser",
|
| 40 |
args=[
|
| 41 |
'--no-sandbox',
|
|
@@ -68,9 +64,6 @@ async def _generate_handwriting_image(text_prompt, screenshot_path):
|
|
| 68 |
await browser.close()
|
| 69 |
|
| 70 |
def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.png"):
|
| 71 |
-
"""
|
| 72 |
-
Synchronous wrapper around the async Pyppeteer call.
|
| 73 |
-
"""
|
| 74 |
try:
|
| 75 |
loop = asyncio.get_event_loop()
|
| 76 |
result = loop.run_until_complete(_generate_handwriting_image(text_prompt, screenshot_path))
|
|
@@ -79,9 +72,6 @@ def generate_handwriting_image(text_prompt, screenshot_path="/tmp/handwriting.pn
|
|
| 79 |
logging.error(f"Error generating handwriting image: {e}")
|
| 80 |
return None
|
| 81 |
|
| 82 |
-
# ----------------------------
|
| 83 |
-
# Detect paper angle within bounding box
|
| 84 |
-
# ----------------------------
|
| 85 |
def detect_paper_angle(image, bounding_box):
|
| 86 |
x1, y1, x2, y2 = bounding_box
|
| 87 |
roi = np.array(image)[y1:y2, x1:x2]
|
|
@@ -100,9 +90,6 @@ def detect_paper_angle(image, bounding_box):
|
|
| 100 |
else:
|
| 101 |
return 0
|
| 102 |
|
| 103 |
-
# ----------------------------
|
| 104 |
-
# Main processing function
|
| 105 |
-
# ----------------------------
|
| 106 |
def process_image(image, text):
|
| 107 |
try:
|
| 108 |
# Initialize Roboflow
|
|
@@ -126,30 +113,23 @@ def process_image(image, text):
|
|
| 126 |
pil_image = image.convert("RGBA")
|
| 127 |
logging.debug("Converted image to RGBA mode.")
|
| 128 |
|
| 129 |
-
# Iterate over detected objects (assumed white paper)
|
| 130 |
for obj in prediction['predictions']:
|
| 131 |
-
# Paper dimensions
|
| 132 |
white_paper_width = obj['width']
|
| 133 |
white_paper_height = obj['height']
|
| 134 |
-
|
| 135 |
-
# Padding
|
| 136 |
padding_x = int(white_paper_width * 0.1)
|
| 137 |
padding_y = int(white_paper_height * 0.1)
|
| 138 |
box_width = white_paper_width - 2 * padding_x
|
| 139 |
box_height = white_paper_height - 2 * padding_y
|
| 140 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
| 141 |
|
| 142 |
-
# Calculate padded coordinates
|
| 143 |
x1_padded = int(obj['x'] - white_paper_width / 2 + padding_x)
|
| 144 |
y1_padded = int(obj['y'] - white_paper_height / 2 + padding_y)
|
| 145 |
x2_padded = int(obj['x'] + white_paper_width / 2 - padding_x)
|
| 146 |
y2_padded = int(obj['y'] + white_paper_height / 2 - padding_y)
|
| 147 |
|
| 148 |
-
# Detect paper angle
|
| 149 |
angle = detect_paper_angle(np.array(image), (x1_padded, y1_padded, x2_padded, y2_padded))
|
| 150 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 151 |
|
| 152 |
-
# (Optional) debug bounding box
|
| 153 |
debug_layer = pil_image.copy()
|
| 154 |
debug_draw = ImageDraw.Draw(debug_layer)
|
| 155 |
debug_draw.rectangle([(x1_padded, y1_padded), (x2_padded, y2_padded)], outline="red", width=3)
|
|
@@ -166,7 +146,6 @@ def process_image(image, text):
|
|
| 166 |
handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
|
| 167 |
rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
|
| 168 |
|
| 169 |
-
# Composite the handwriting
|
| 170 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
| 171 |
paste_x = int(obj['x'] - rotated_handwriting.size[0] / 2)
|
| 172 |
paste_y = int(obj['y'] - rotated_handwriting.size[1] / 2)
|
|
@@ -174,7 +153,6 @@ def process_image(image, text):
|
|
| 174 |
pil_image = Image.alpha_composite(pil_image, text_layer)
|
| 175 |
logging.debug("Handwriting layer composited onto the original image.")
|
| 176 |
|
| 177 |
-
# Save output
|
| 178 |
output_image_path = "/tmp/output_image.png"
|
| 179 |
pil_image.convert("RGB").save(output_image_path)
|
| 180 |
logging.debug(f"Output image saved to {output_image_path}.")
|
|
@@ -184,9 +162,6 @@ def process_image(image, text):
|
|
| 184 |
logging.error(f"Error during image processing: {e}")
|
| 185 |
return None
|
| 186 |
|
| 187 |
-
# ----------------------------
|
| 188 |
-
# Gradio inference function
|
| 189 |
-
# ----------------------------
|
| 190 |
def gradio_inference(image, text):
|
| 191 |
logging.debug("Starting Gradio inference.")
|
| 192 |
result_path = process_image(image, text)
|
|
@@ -196,9 +171,6 @@ def gradio_inference(image, text):
|
|
| 196 |
logging.error("Gradio inference failed.")
|
| 197 |
return None, None, "An error occurred while processing the image. Please check the logs."
|
| 198 |
|
| 199 |
-
# ----------------------------
|
| 200 |
-
# Gradio interface
|
| 201 |
-
# ----------------------------
|
| 202 |
interface = gr.Interface(
|
| 203 |
fn=gradio_inference,
|
| 204 |
inputs=[
|
|
@@ -215,8 +187,6 @@ interface = gr.Interface(
|
|
| 215 |
allow_flagging="never"
|
| 216 |
)
|
| 217 |
|
| 218 |
-
# ... [rest of your existing code] ...
|
| 219 |
-
|
| 220 |
if __name__ == "__main__":
|
| 221 |
interface.launch(
|
| 222 |
server_name="0.0.0.0",
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# If you installed chromium via apt, you can specify the path:
|
| 35 |
executablePath="/usr/bin/chromium-browser",
|
| 36 |
args=[
|
| 37 |
'--no-sandbox',
|
|
|
|
| 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 |
logging.error(f"Error generating handwriting image: {e}")
|
| 73 |
return None
|
| 74 |
|
|
|
|
|
|
|
|
|
|
| 75 |
def detect_paper_angle(image, bounding_box):
|
| 76 |
x1, y1, x2, y2 = bounding_box
|
| 77 |
roi = np.array(image)[y1:y2, x1:x2]
|
|
|
|
| 90 |
else:
|
| 91 |
return 0
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
def process_image(image, text):
|
| 94 |
try:
|
| 95 |
# Initialize Roboflow
|
|
|
|
| 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 |
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 |
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 |
logging.error(f"Error during image processing: {e}")
|
| 163 |
return None
|
| 164 |
|
|
|
|
|
|
|
|
|
|
| 165 |
def gradio_inference(image, text):
|
| 166 |
logging.debug("Starting Gradio inference.")
|
| 167 |
result_path = process_image(image, text)
|
|
|
|
| 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=[
|
|
|
|
| 187 |
allow_flagging="never"
|
| 188 |
)
|
| 189 |
|
|
|
|
|
|
|
| 190 |
if __name__ == "__main__":
|
| 191 |
interface.launch(
|
| 192 |
server_name="0.0.0.0",
|