kreemyyyy commited on
Commit
b342da6
·
verified ·
1 Parent(s): 8195755

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -69
app.py CHANGED
@@ -1,6 +1,3 @@
1
- import nest_asyncio
2
- nest_asyncio.apply()
3
-
4
  import os
5
  import gradio as gr
6
  import logging
@@ -9,8 +6,6 @@ from PIL import Image, ImageDraw
9
  import cv2
10
  import numpy as np
11
  from math import atan2, degrees
12
- import asyncio
13
- from pyppeteer import launch
14
 
15
  # Configure logging
16
  logging.basicConfig(
@@ -27,62 +22,6 @@ 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',
38
- '--disable-dev-shm-usage',
39
- '--disable-gpu',
40
- '--single-process',
41
- '--no-zygote',
42
- '--window-size=1920,1080'
43
- ]
44
- )
45
- page = await browser.newPage()
46
-
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
54
- await page.waitForSelector('#text-input', {'timeout': 30000})
55
-
56
- # Type the text prompt
57
- await page.type('#text-input', text_prompt)
58
-
59
- # Wait for rendering
60
- await asyncio.sleep(5)
61
-
62
- # Take a screenshot
63
- await page.screenshot({
64
- 'path': screenshot_path,
65
- 'clip': {'x': 100, 'y': 200, 'width': 600, 'height': 150}
66
- })
67
- return screenshot_path
68
-
69
- except Exception as e:
70
- logging.error(f"Pyppeteer error: {str(e)}")
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))
81
- return result
82
- except Exception as e:
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]
@@ -148,11 +87,8 @@ def process_image(image, text):
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.")
154
- continue
155
-
156
  handwriting_img = Image.open(handwriting_path).convert("RGBA")
157
  handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
158
  rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
@@ -194,7 +130,7 @@ interface = gr.Interface(
194
  gr.Textbox(label="Status")
195
  ],
196
  title="Roboflow Detection with Handwriting Overlay",
197
- description="Upload an image and enter text to overlay. The Roboflow model detects the white paper area, and a handwriting image is generated via Calligraphr using Pyppeteer. The output image is composited accordingly.",
198
  allow_flagging="never"
199
  )
200
 
@@ -202,6 +138,5 @@ if __name__ == "__main__":
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
- )
 
 
 
 
1
  import os
2
  import gradio as gr
3
  import logging
 
6
  import cv2
7
  import numpy as np
8
  from math import atan2, degrees
 
 
9
 
10
  # Configure logging
11
  logging.basicConfig(
 
22
  PROJECT_NAME = "model_verification_project"
23
  VERSION_NUMBER = 2
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def detect_paper_angle(image, bounding_box):
26
  x1, y1, x2, y2 = bounding_box
27
  roi = np.array(image)[y1:y2, x1:x2]
 
87
  debug_layer.save("/tmp/debug_bounding_box.png")
88
  logging.debug("Saved bounding box debug image to /tmp/debug_bounding_box.png.")
89
 
90
+ # Load pre-generated handwriting image
91
+ handwriting_path = "/path/to/pre-generated/handwriting.png"
 
 
 
92
  handwriting_img = Image.open(handwriting_path).convert("RGBA")
93
  handwriting_img = handwriting_img.resize((box_width, box_height), Image.ANTIALIAS)
94
  rotated_handwriting = handwriting_img.rotate(-angle, resample=Image.BICUBIC, expand=True)
 
130
  gr.Textbox(label="Status")
131
  ],
132
  title="Roboflow Detection with Handwriting Overlay",
133
+ description="Upload an image and enter text to overlay. The Roboflow model detects the white paper area, and a handwriting image is composited accordingly.",
134
  allow_flagging="never"
135
  )
136
 
 
138
  interface.launch(
139
  server_name="0.0.0.0",
140
  server_port=int(os.environ.get("PORT", 7860)),
 
141
  enable_queue=True
142
+ )