Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import mss
|
| 3 |
+
import numpy as np
|
| 4 |
+
import PIL.Image
|
| 5 |
+
import threading
|
| 6 |
+
import time
|
| 7 |
+
|
| 8 |
+
latest_frame = None
|
| 9 |
+
|
| 10 |
+
def capture_loop():
|
| 11 |
+
global latest_frame
|
| 12 |
+
with mss.mss() as sct:
|
| 13 |
+
monitor = sct.monitors[1]
|
| 14 |
+
while True:
|
| 15 |
+
screenshot = sct.grab(monitor)
|
| 16 |
+
img = PIL.Image.frombytes("RGB", screenshot.size, screenshot.rgb)
|
| 17 |
+
latest_frame = img
|
| 18 |
+
time.sleep(0.2) # ~5 times per second
|
| 19 |
+
|
| 20 |
+
def get_latest_frame():
|
| 21 |
+
return latest_frame
|
| 22 |
+
|
| 23 |
+
# Start the capture loop in the background
|
| 24 |
+
threading.Thread(target=capture_loop, daemon=True).start()
|
| 25 |
+
|
| 26 |
+
# UI
|
| 27 |
+
demo = gr.Interface(fn=get_latest_frame, inputs=[], outputs="image", live=True)
|
| 28 |
+
demo.launch()
|