Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from gradio_webrtc import WebRTC
|
4 |
+
from twilio.rest import Client
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Twilio configuration for TURN servers (required for cloud deployment)
|
8 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID", "your_twilio_account_sid")
|
9 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN", "your_twilio_auth_token")
|
10 |
+
client = Client(account_sid, auth_token)
|
11 |
+
token = client.tokens.create()
|
12 |
+
rtc_configuration = {
|
13 |
+
"iceServers": token.ice_servers,
|
14 |
+
"iceTransportPolicy": "relay",
|
15 |
+
}
|
16 |
+
|
17 |
+
# Video processing function (example: flip video vertically)
|
18 |
+
def flip_vertically(image):
|
19 |
+
return np.flip(image, axis=0)
|
20 |
+
|
21 |
+
# Gradio app setup
|
22 |
+
with gr.Blocks() as demo:
|
23 |
+
gr.HTML("<h1 style='text-align: center'>Real-Time Webcam Stream with WebRTC</h1>")
|
24 |
+
|
25 |
+
# WebRTC component for video streaming
|
26 |
+
webrtc = WebRTC(
|
27 |
+
label="Webcam Stream",
|
28 |
+
mode="send-receive",
|
29 |
+
modality="video",
|
30 |
+
rtc_configuration=rtc_configuration,
|
31 |
+
)
|
32 |
+
|
33 |
+
# Stream video and apply the flip filter
|
34 |
+
webrtc.stream(
|
35 |
+
handler=flip_vertically,
|
36 |
+
inputs=[webrtc],
|
37 |
+
outputs=[webrtc],
|
38 |
+
time_limit=90, # Limit processing time per user
|
39 |
+
stream_every=0.1, # Capture and process frames every 0.1 seconds for real-time effect
|
40 |
+
)
|
41 |
+
|
42 |
+
# Launch the app
|
43 |
+
demo.launch()
|