Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,50 @@
|
|
| 1 |
-
import random
|
| 2 |
-
import time
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
-
import colorsys
|
| 6 |
import spaces
|
| 7 |
-
|
| 8 |
from fastrtc import WebRTC, get_turn_credentials
|
| 9 |
|
| 10 |
@spaces.GPU
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
value = 0.8 + 0.2 * np.sin(frame_idx * 0.2)
|
| 19 |
-
|
| 20 |
-
r, g, b = colorsys.hsv_to_rgb(hue/360, saturation, value)
|
| 21 |
-
|
| 22 |
-
rgb_frame = np.full((height, width, 3), [r*255, g*255, b*255], dtype=np.uint8)
|
| 23 |
-
|
| 24 |
-
noise = np.random.randint(-15, 15, (height, width, 3), dtype=np.int16)
|
| 25 |
-
rgb_frame = np.clip(rgb_frame.astype(np.int16) + noise, 0, 255).astype(np.uint8)
|
| 26 |
-
|
| 27 |
-
x_grad = np.linspace(0, 1, width)
|
| 28 |
-
y_grad = np.linspace(0, 1, height)
|
| 29 |
-
X, Y = np.meshgrid(x_grad, y_grad)
|
| 30 |
-
|
| 31 |
-
wave = np.sin(X * 4 + frame_idx * 0.2) * np.sin(Y * 4 + frame_idx * 0.3)
|
| 32 |
-
wave_normalized = ((wave + 1) / 2 * 40).astype(np.uint8)
|
| 33 |
-
|
| 34 |
-
rgb_frame[:, :, (frame_idx // 20) % 3] = np.clip(
|
| 35 |
-
rgb_frame[:, :, (frame_idx // 20) % 3].astype(np.int16) + wave_normalized,
|
| 36 |
-
0, 255
|
| 37 |
-
).astype(np.uint8)
|
| 38 |
-
|
| 39 |
-
bgr_frame = rgb_frame[:, :, ::-1]
|
| 40 |
-
|
| 41 |
-
yield bgr_frame
|
| 42 |
-
time.sleep(0.033) # 30 fps
|
| 43 |
-
|
| 44 |
-
print("🎉 Dummy stream finished!")
|
| 45 |
|
| 46 |
# --- Gradio UI Layout ---
|
| 47 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="
|
| 48 |
-
gr.Markdown("# 🚀
|
| 49 |
-
gr.Markdown("*This
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
-
with gr.Column(
|
| 53 |
-
gr.Markdown("### 1.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
start = gr.Button("🚀 Start Generation", variant="primary")
|
| 63 |
-
|
| 64 |
-
with gr.Column(scale=3):
|
| 65 |
-
gr.Markdown("### 2. View Live Stream")
|
| 66 |
-
gr.Markdown("Click 'Start Generation' to begin streaming frames.")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
modality="video",
|
| 71 |
mode="receive",
|
| 72 |
-
height=480,
|
| 73 |
rtc_configuration=get_turn_credentials(),
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
demo.queue().launch(
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import spaces
|
|
|
|
| 4 |
from fastrtc import WebRTC, get_turn_credentials
|
| 5 |
|
| 6 |
@spaces.GPU
|
| 7 |
+
def flip_frame_handler(video_frame):
|
| 8 |
+
"""
|
| 9 |
+
Flips the incoming video frame vertically and yields it.
|
| 10 |
+
This needs to be a generator function because the output is a receive-only stream.
|
| 11 |
+
"""
|
| 12 |
+
if video_frame:
|
| 13 |
+
yield np.flip(video_frame, axis=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# --- Gradio UI Layout ---
|
| 16 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC Webcam Frame Flipper") as demo:
|
| 17 |
+
gr.Markdown("# 🚀 FastRTC Webcam Frame Flipper (Side-by-Side)")
|
| 18 |
+
gr.Markdown("*This demo takes your webcam feed from the left panel, flips each frame vertically on the server, and streams it back to the right panel in real-time.*")
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
+
with gr.Column():
|
| 22 |
+
gr.Markdown("### 1. Your Webcam Feed (Input)")
|
| 23 |
+
# This component only sends video to the server
|
| 24 |
+
webcam_input = WebRTC(
|
| 25 |
+
label="Webcam Input",
|
| 26 |
+
modality="video",
|
| 27 |
+
mode="send",
|
| 28 |
+
rtc_configuration=get_turn_credentials(),
|
| 29 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
with gr.Column():
|
| 32 |
+
gr.Markdown("### 2. Flipped Video (Output)")
|
| 33 |
+
# This component only receives video from the server
|
| 34 |
+
video_output = WebRTC(
|
| 35 |
+
label="Flipped Output Stream",
|
| 36 |
modality="video",
|
| 37 |
mode="receive",
|
|
|
|
| 38 |
rtc_configuration=get_turn_credentials(),
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# The `stream` event is triggered by the input component
|
| 42 |
+
webcam_input.stream(
|
| 43 |
+
fn=flip_frame_handler,
|
| 44 |
+
inputs=[webcam_input], # Input is the webcam feed
|
| 45 |
+
outputs=[video_output], # Output is the separate receive-only component
|
| 46 |
+
time_limit=60,
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
+
demo.queue().launch()
|