Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -214,7 +214,9 @@ def _get_mouse_pos() -> tuple[int, int]:
|
|
| 214 |
|
| 215 |
def capture_screen_raw() -> tuple[object, int, int]:
|
| 216 |
"""
|
| 217 |
-
ููุชูุท ุงูุดุงุดุฉ ุจู 5 ุทุฑู ู
ุชุณูุณูุฉ โ ุชุถู
ู ูุฌุงุญ ูุงุญุฏุฉ
|
|
|
|
|
|
|
| 218 |
1. scrot (ุงูุฃุณุฑุน)
|
| 219 |
2. import (ImageMagick)
|
| 220 |
3. xwd + convert
|
|
@@ -224,9 +226,10 @@ def capture_screen_raw() -> tuple[object, int, int]:
|
|
| 224 |
from PIL import Image
|
| 225 |
env = {**os.environ, "DISPLAY": DISPLAY}
|
| 226 |
tmp = f"/tmp/zs_{int(time.time()*1000)}"
|
|
|
|
| 227 |
|
| 228 |
def _load(path) -> tuple:
|
| 229 |
-
"""ุชุญู
ูู ุงูุตูุฑุฉ ุฅุฐุง ููุฌุฏุช ูุญุฌู
ูุง > 0"""
|
| 230 |
if not path or not os.path.exists(path):
|
| 231 |
return None, 0, 0
|
| 232 |
size = os.path.getsize(path)
|
|
@@ -237,123 +240,139 @@ def capture_screen_raw() -> tuple[object, int, int]:
|
|
| 237 |
w, h = img.size
|
| 238 |
if w < 10 or h < 10:
|
| 239 |
return None, 0, 0
|
| 240 |
-
try: os.unlink(path)
|
| 241 |
-
except: pass
|
| 242 |
return img, w, h
|
| 243 |
except Exception as ex:
|
| 244 |
print(f"[_load] {ex}")
|
| 245 |
return None, 0, 0
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
p
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
if img:
|
| 256 |
-
print("[capture] โ
scrot")
|
| 257 |
-
return img, w, h
|
| 258 |
-
print(f"[capture] scrot failed rc={r.returncode} err={r.stderr[:80]}")
|
| 259 |
-
except Exception as e:
|
| 260 |
-
print(f"[capture] scrot ex: {e}")
|
| 261 |
|
| 262 |
-
# โโ ุทุฑููุฉ 2: ImageMagick import โโโโโโโโโโโโโโโโโโโโโ
|
| 263 |
try:
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
except
|
| 291 |
-
|
| 292 |
-
if img:
|
| 293 |
-
print("[capture] โ
xwd+convert")
|
| 294 |
-
return img, w, h
|
| 295 |
-
print(f"[capture] xwd rc={r1.returncode} convert rc={r2.returncode}")
|
| 296 |
-
except Exception as e:
|
| 297 |
-
print(f"[capture] xwd ex: {e}")
|
| 298 |
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
except Exception as e:
|
| 342 |
-
print(f"[capture] xlib ex: {e}")
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
|
| 359 |
def capture_screen(scale=0.6, quality=70) -> str:
|
|
@@ -1136,4 +1155,11 @@ async def quick_search(query: str):
|
|
| 1136 |
|
| 1137 |
if __name__ == "__main__":
|
| 1138 |
port = int(os.environ.get("PORT", 7860))
|
| 1139 |
-
uvicorn.run("app:app", host="0.0.0.0", port=port, log_level="info")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
def capture_screen_raw() -> tuple[object, int, int]:
|
| 216 |
"""
|
| 217 |
+
ููุชูุท ุงูุดุงุดุฉ ุจู 5 ุทุฑู ู
ุชุณูุณูุฉ โ ุชุถู
ู ูุฌุงุญ ูุงุญุฏุฉ.
|
| 218 |
+
ูู ุงูู
ููุงุช ุงูู
ุคูุชุฉ (ูุงุฌุญุฉ ุฃู ูุงุดูุฉ) ุชูุญุฐู ุฏุงุฆู
ุงู ูู
ูุน ุชุฑุงูู
ูุง ูู /tmp
|
| 219 |
+
ูุงู
ุชูุงุก ุงููุฑุต/ุงูุฐุงูุฑุฉ ุจุนุฏ ุนุฏุฉ ููุทุงุช ุดุงุดุฉ ู
ุชุชุงููุฉ.
|
| 220 |
1. scrot (ุงูุฃุณุฑุน)
|
| 221 |
2. import (ImageMagick)
|
| 222 |
3. xwd + convert
|
|
|
|
| 226 |
from PIL import Image
|
| 227 |
env = {**os.environ, "DISPLAY": DISPLAY}
|
| 228 |
tmp = f"/tmp/zs_{int(time.time()*1000)}"
|
| 229 |
+
_tmp_files_to_clean = [] # ูู ุงูู
ููุงุช ุงูู
ุคูุชุฉ ุงูุชู ุฃููุดุฆุช ูู ูุฐู ุงูู
ุญุงููุฉ
|
| 230 |
|
| 231 |
def _load(path) -> tuple:
|
| 232 |
+
"""ุชุญู
ูู ุงูุตูุฑุฉ ุฅุฐุง ููุฌุฏุช ูุญุฌู
ูุง > 0 โ ูุง ูุญุฐู ุงูู
ูู ููุงุ ุงูุญุฐู ู
ุฑูุฒู ูู ุงูููุงูุฉ"""
|
| 233 |
if not path or not os.path.exists(path):
|
| 234 |
return None, 0, 0
|
| 235 |
size = os.path.getsize(path)
|
|
|
|
| 240 |
w, h = img.size
|
| 241 |
if w < 10 or h < 10:
|
| 242 |
return None, 0, 0
|
|
|
|
|
|
|
| 243 |
return img, w, h
|
| 244 |
except Exception as ex:
|
| 245 |
print(f"[_load] {ex}")
|
| 246 |
return None, 0, 0
|
| 247 |
|
| 248 |
+
def _cleanup():
|
| 249 |
+
"""ูุญุฐู ูู ุงูู
ููุงุช ุงูู
ุคูุชุฉ ุงูู
ุชุจููุฉ ู
ู ูู ุงูู
ุญุงููุงุช (ูุงุฌุญุฉ ุฃู ูุงุดูุฉ)"""
|
| 250 |
+
for p in _tmp_files_to_clean:
|
| 251 |
+
try:
|
| 252 |
+
if p and os.path.exists(p):
|
| 253 |
+
os.unlink(p)
|
| 254 |
+
except Exception:
|
| 255 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
|
|
|
| 257 |
try:
|
| 258 |
+
# โโ ุทุฑููุฉ 1: scrot โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 259 |
+
try:
|
| 260 |
+
p = tmp + "_1.png"
|
| 261 |
+
_tmp_files_to_clean.append(p)
|
| 262 |
+
r = subprocess.run(
|
| 263 |
+
["scrot", "-q", "95", p],
|
| 264 |
+
env=env, timeout=8, capture_output=True
|
| 265 |
+
)
|
| 266 |
+
img, w, h = _load(p)
|
| 267 |
+
if img:
|
| 268 |
+
print("[capture] โ
scrot")
|
| 269 |
+
return img, w, h
|
| 270 |
+
print(f"[capture] scrot failed rc={r.returncode} err={r.stderr[:80]}")
|
| 271 |
+
except Exception as e:
|
| 272 |
+
print(f"[capture] scrot ex: {e}")
|
| 273 |
|
| 274 |
+
# โโ ุทุฑููุฉ 2: ImageMagick import โโโโโโโโโโโโโโโโโโโโโ
|
| 275 |
+
try:
|
| 276 |
+
p = tmp + "_2.png"
|
| 277 |
+
_tmp_files_to_clean.append(p)
|
| 278 |
+
r = subprocess.run(
|
| 279 |
+
["import", "-window", "root", "-silent", p],
|
| 280 |
+
env=env, timeout=10, capture_output=True
|
| 281 |
+
)
|
| 282 |
+
img, w, h = _load(p)
|
| 283 |
+
if img:
|
| 284 |
+
print("[capture] โ
import (ImageMagick)")
|
| 285 |
+
return img, w, h
|
| 286 |
+
print(f"[capture] import failed rc={r.returncode}")
|
| 287 |
+
except Exception as e:
|
| 288 |
+
print(f"[capture] import ex: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
+
# โโ ุทุฑููุฉ 3: xwd + convert โโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 291 |
+
try:
|
| 292 |
+
xwd_p = tmp + "_3.xwd"
|
| 293 |
+
png_p = tmp + "_3.png"
|
| 294 |
+
_tmp_files_to_clean.append(xwd_p)
|
| 295 |
+
_tmp_files_to_clean.append(png_p)
|
| 296 |
+
r1 = subprocess.run(
|
| 297 |
+
["xwd", "-root", "-silent", "-out", xwd_p],
|
| 298 |
+
env=env, timeout=10, capture_output=True
|
| 299 |
+
)
|
| 300 |
+
r2 = subprocess.run(
|
| 301 |
+
["convert", xwd_p, png_p],
|
| 302 |
+
timeout=10, capture_output=True
|
| 303 |
+
)
|
| 304 |
+
img, w, h = _load(png_p)
|
| 305 |
+
if img:
|
| 306 |
+
print("[capture] โ
xwd+convert")
|
| 307 |
+
return img, w, h
|
| 308 |
+
print(f"[capture] xwd rc={r1.returncode} convert rc={r2.returncode}")
|
| 309 |
+
except Exception as e:
|
| 310 |
+
print(f"[capture] xwd ex: {e}")
|
| 311 |
|
| 312 |
+
# โโ ุทุฑููุฉ 4: ffmpeg x11grab โโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 313 |
+
try:
|
| 314 |
+
p = tmp + "_4.png"
|
| 315 |
+
_tmp_files_to_clean.append(p)
|
| 316 |
+
sw, sh = _get_screen_size()
|
| 317 |
+
r = subprocess.run([
|
| 318 |
+
"ffmpeg", "-y",
|
| 319 |
+
"-f", "x11grab",
|
| 320 |
+
"-video_size", f"{sw}x{sh}",
|
| 321 |
+
"-i", DISPLAY,
|
| 322 |
+
"-vframes", "1",
|
| 323 |
+
"-q:v", "2",
|
| 324 |
+
p
|
| 325 |
+
], env=env, timeout=12, capture_output=True)
|
| 326 |
+
img, w, h = _load(p)
|
| 327 |
+
if img:
|
| 328 |
+
print("[capture] โ
ffmpeg x11grab")
|
| 329 |
+
return img, w, h
|
| 330 |
+
print(f"[capture] ffmpeg rc={r.returncode} err={r.stderr[-120:]}")
|
| 331 |
+
except Exception as e:
|
| 332 |
+
print(f"[capture] ffmpeg ex: {e}")
|
|
|
|
|
|
|
| 333 |
|
| 334 |
+
# โโ ุทุฑููุฉ 5: python-xlib (Xlib ู
ุจุงุดุฑุฉ) โโโโโโโโโโโโโโ
|
| 335 |
+
try:
|
| 336 |
+
from Xlib import display as Xdisplay
|
| 337 |
+
from Xlib.ext.xtest import fake_input
|
| 338 |
+
xdisp = Xdisplay.Display(DISPLAY)
|
| 339 |
+
root = xdisp.screen().root
|
| 340 |
+
geom = root.get_geometry()
|
| 341 |
+
w, h = geom.width, geom.height
|
| 342 |
+
raw = root.get_image(0, 0, w, h,
|
| 343 |
+
Xdisplay.X.ZPixmap,
|
| 344 |
+
0xFFFFFFFF)
|
| 345 |
+
import struct
|
| 346 |
+
data = raw.data
|
| 347 |
+
# BGRA โ RGB
|
| 348 |
+
pixels = []
|
| 349 |
+
for i in range(0, len(data), 4):
|
| 350 |
+
b, g, r_, a = struct.unpack_from('BBBB', data, i)
|
| 351 |
+
pixels.extend([r_, g, b])
|
| 352 |
+
img = Image.frombytes("RGB", (w, h), bytes(pixels))
|
| 353 |
+
print("[capture] โ
python-xlib")
|
| 354 |
+
return img, w, h
|
| 355 |
+
except Exception as e:
|
| 356 |
+
print(f"[capture] xlib ex: {e}")
|
| 357 |
+
|
| 358 |
+
# โโ ุทุฑููุฉ 6: ุตูุฑุฉ placeholder ูุงุถุญุฉ โโโโโโโโโโโโโโโโ
|
| 359 |
+
print("[capture] โ ๏ธ ALL methods failed โ returning placeholder")
|
| 360 |
+
try:
|
| 361 |
+
from PIL import ImageDraw
|
| 362 |
+
sw, sh = _get_screen_size()
|
| 363 |
+
img = Image.new("RGB", (sw or 1280, sh or 720), (20, 20, 30))
|
| 364 |
+
draw = ImageDraw.Draw(img)
|
| 365 |
+
draw.rectangle([(0, 0), (sw, 40)], fill=(30, 30, 50))
|
| 366 |
+
draw.text((10, 10), f"โ ๏ธ Screenshot failed โ DISPLAY={DISPLAY}", fill=(255, 100, 100))
|
| 367 |
+
draw.text((10, 50), "Methods tried: scrot, import, xwd, ffmpeg, xlib", fill=(150, 150, 150))
|
| 368 |
+
return img, sw or 1280, sh or 720
|
| 369 |
+
except:
|
| 370 |
+
return None, 0, 0
|
| 371 |
+
finally:
|
| 372 |
+
# โโ ูุญุฐู ูู ุงูู
ููุงุช ุงูู
ุคูุชุฉ ุฏุงุฆู
ุงูุ ูุฌุญุช ุงูู
ุญุงููุฉ ุฃู ูุดูุช โโ
|
| 373 |
+
# ูุฐุง ูู
ูุน ุชุฑุงูู
ู
ููุงุช /tmp ุงูุชู ูุงูุช ุชู
ุชูุฆ ุจุนุฏ ุนุฏุฉ ููุทุงุช ูุชุชุณุจุจ
|
| 374 |
+
# ุจูุชู ุงูุนู
ููุงุช (SIGTERM/OOM) ุนูู ุจูุฆุงุช ู
ุซู Hugging Face ุงูู
ุฌุงููุฉ.
|
| 375 |
+
_cleanup()
|
| 376 |
|
| 377 |
|
| 378 |
def capture_screen(scale=0.6, quality=70) -> str:
|
|
|
|
| 1155 |
|
| 1156 |
if __name__ == "__main__":
|
| 1157 |
port = int(os.environ.get("PORT", 7860))
|
| 1158 |
+
uvicorn.run("app:app", host="0.0.0.0", port=port, log_level="info")
|
| 1159 |
+
|
| 1160 |
+
|
| 1161 |
+
|
| 1162 |
+
|
| 1163 |
+
|
| 1164 |
+
|
| 1165 |
+
|