Update app.py
Browse files
app.py
CHANGED
|
@@ -6,43 +6,53 @@ from fastrtc import WebRTC, get_turn_credentials
|
|
| 6 |
@spaces.GPU
|
| 7 |
def flip_frame_handler(video_frame):
|
| 8 |
"""
|
| 9 |
-
Flips the incoming video frame vertically and
|
| 10 |
-
This
|
| 11 |
"""
|
| 12 |
-
if video_frame:
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# --- Gradio UI Layout ---
|
| 16 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC Webcam
|
| 17 |
-
gr.Markdown("# 🚀 FastRTC Webcam
|
| 18 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 19 |
|
| 20 |
with gr.Row():
|
| 21 |
with gr.Column():
|
| 22 |
gr.Markdown("### 1. Your Webcam Feed (Input)")
|
| 23 |
-
# This component only
|
| 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
|
| 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
|
| 42 |
webcam_input.stream(
|
| 43 |
fn=flip_frame_handler,
|
| 44 |
-
inputs=[webcam_input], #
|
| 45 |
-
outputs=[video_output], #
|
| 46 |
time_limit=60,
|
| 47 |
)
|
| 48 |
|
|
|
|
| 6 |
@spaces.GPU
|
| 7 |
def flip_frame_handler(video_frame):
|
| 8 |
"""
|
| 9 |
+
Flips the incoming video frame both vertically and horizontally.
|
| 10 |
+
This function now uses 'return' because it processes one frame at a time.
|
| 11 |
"""
|
| 12 |
+
if video_frame is not None:
|
| 13 |
+
# A single call to np.flip with axis=(0, 1) flips both vertically and horizontally.
|
| 14 |
+
return np.flip(video_frame, axis=(0, 1))
|
| 15 |
+
|
| 16 |
+
# Return None if no frame is received to handle the case gracefully.
|
| 17 |
+
return None
|
| 18 |
|
| 19 |
# --- Gradio UI Layout ---
|
| 20 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC Webcam Double Flipper") as demo:
|
| 21 |
+
gr.Markdown("# 🚀 FastRTC Webcam Double Flipper (Side-by-Side)")
|
| 22 |
+
gr.Markdown(
|
| 23 |
+
"*This demo takes your webcam feed from the left, flips it both horizontally and vertically on the server, and streams it back to the right panel in real-time.*"
|
| 24 |
+
)
|
| 25 |
|
| 26 |
with gr.Row():
|
| 27 |
with gr.Column():
|
| 28 |
gr.Markdown("### 1. Your Webcam Feed (Input)")
|
| 29 |
+
# This component is configured only to send video to the server.
|
| 30 |
webcam_input = WebRTC(
|
| 31 |
label="Webcam Input",
|
| 32 |
modality="video",
|
| 33 |
mode="send",
|
| 34 |
+
width=640,
|
| 35 |
+
height=480,
|
| 36 |
rtc_configuration=get_turn_credentials(),
|
| 37 |
)
|
| 38 |
|
| 39 |
with gr.Column():
|
| 40 |
gr.Markdown("### 2. Flipped Video (Output)")
|
| 41 |
+
# This component is configured only to receive video from the server.
|
| 42 |
video_output = WebRTC(
|
| 43 |
label="Flipped Output Stream",
|
| 44 |
modality="video",
|
| 45 |
mode="receive",
|
| 46 |
+
width=640,
|
| 47 |
+
height=480,
|
| 48 |
rtc_configuration=get_turn_credentials(),
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# The `stream` event is triggered by new frames from the `webcam_input` component.
|
| 52 |
webcam_input.stream(
|
| 53 |
fn=flip_frame_handler,
|
| 54 |
+
inputs=[webcam_input], # The input to our function is the webcam feed.
|
| 55 |
+
outputs=[video_output], # The output is sent to the separate 'receive' component.
|
| 56 |
time_limit=60,
|
| 57 |
)
|
| 58 |
|