Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- README.md +8 -8
- requirements.txt +3 -0
- run.ipynb +1 -0
- run.py +101 -0
- video/compliment_bot_screen_recording_3x.mp4 +0 -0
README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
-
app_file:
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
|
2 |
---
|
3 |
+
title: stream_video_out_main
|
4 |
+
emoji: 🔥
|
5 |
+
colorFrom: indigo
|
6 |
+
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
+
sdk_version: 5.0.0
|
9 |
+
app_file: run.py
|
10 |
pinned: false
|
11 |
+
hf_oauth: true
|
12 |
---
|
|
|
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
gradio-client @ git+https://github.com/gradio-app/gradio@bbf9ba7e997022960c621f72baa891185bd03732#subdirectory=client/python
|
2 |
+
https://gradio-pypi-previews.s3.amazonaws.com/bbf9ba7e997022960c621f72baa891185bd03732/gradio-5.0.0-py3-none-any.whl
|
3 |
+
opencv-python
|
run.ipynb
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: stream_video_out"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio opencv-python"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "os.mkdir('video')\n", "!wget -q -O video/compliment_bot_screen_recording_3x.mp4 https://github.com/gradio-app/gradio/raw/main/demo/stream_video_out/video/compliment_bot_screen_recording_3x.mp4"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import cv2\n", "import os\n", "from pathlib import Path\n", "import atexit\n", "\n", "current_dir = Path(__file__).resolve().parent\n", "\n", "\n", "def delete_files():\n", " for p in Path(current_dir).glob(\"*.ts\"):\n", " p.unlink()\n", " for p in Path(current_dir).glob(\"*.mp4\"):\n", " p.unlink()\n", "\n", "\n", "atexit.register(delete_files)\n", "\n", "\n", "def process_video(input_video, stream_as_mp4):\n", " cap = cv2.VideoCapture(input_video)\n", "\n", " video_codec = cv2.VideoWriter_fourcc(*\"mp4v\") if stream_as_mp4 else cv2.VideoWriter_fourcc(*\"x264\") # type: ignore\n", " fps = int(cap.get(cv2.CAP_PROP_FPS))\n", " width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n", " height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", "\n", " iterating, frame = cap.read()\n", "\n", " n_frames = 0\n", " n_chunks = 0\n", " name = str(current_dir / f\"output_{n_chunks}{'.mp4' if stream_as_mp4 else '.ts'}\")\n", " segment_file = cv2.VideoWriter(name, video_codec, fps, (width, height)) # type: ignore\n", "\n", " while iterating:\n", "\n", " # flip frame vertically\n", " frame = cv2.flip(frame, 0)\n", " display_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n", " segment_file.write(display_frame)\n", " n_frames += 1\n", " if n_frames == 3 * fps:\n", " n_chunks += 1\n", " segment_file.release()\n", " n_frames = 0\n", " yield name\n", " name = str(\n", " current_dir / f\"output_{n_chunks}{'.mp4' if stream_as_mp4 else '.ts'}\"\n", " )\n", " segment_file = cv2.VideoWriter(name, video_codec, fps, (width, height)) # type: ignore\n", "\n", " iterating, frame = cap.read()\n", "\n", " segment_file.release()\n", " yield name\n", "\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Video Streaming Out \ud83d\udcf9\")\n", " with gr.Row():\n", " with gr.Column():\n", " input_video = gr.Video(label=\"input\")\n", " checkbox = gr.Checkbox(label=\"Stream as MP4 file?\", value=False)\n", " with gr.Column():\n", " processed_frames = gr.Video(\n", " label=\"stream\",\n", " streaming=True,\n", " autoplay=True,\n", " elem_id=\"stream_video_output\",\n", " )\n", " with gr.Row():\n", " process_video_btn = gr.Button(\"process video\")\n", "\n", " process_video_btn.click(process_video, [input_video, checkbox], [processed_frames])\n", "\n", " gr.Examples(\n", " [\n", " [\n", " os.path.join(\n", " os.path.abspath(''),\n", " \"video/compliment_bot_screen_recording_3x.mp4\",\n", " ),\n", " False,\n", " ],\n", " [\n", " os.path.join(\n", " os.path.abspath(''),\n", " \"video/compliment_bot_screen_recording_3x.mp4\",\n", " ),\n", " True,\n", " ],\n", " ],\n", " [input_video, checkbox],\n", " fn=process_video,\n", " outputs=processed_frames,\n", " cache_examples=False,\n", " )\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import os
|
4 |
+
from pathlib import Path
|
5 |
+
import atexit
|
6 |
+
|
7 |
+
current_dir = Path(__file__).resolve().parent
|
8 |
+
|
9 |
+
|
10 |
+
def delete_files():
|
11 |
+
for p in Path(current_dir).glob("*.ts"):
|
12 |
+
p.unlink()
|
13 |
+
for p in Path(current_dir).glob("*.mp4"):
|
14 |
+
p.unlink()
|
15 |
+
|
16 |
+
|
17 |
+
atexit.register(delete_files)
|
18 |
+
|
19 |
+
|
20 |
+
def process_video(input_video, stream_as_mp4):
|
21 |
+
cap = cv2.VideoCapture(input_video)
|
22 |
+
|
23 |
+
video_codec = cv2.VideoWriter_fourcc(*"mp4v") if stream_as_mp4 else cv2.VideoWriter_fourcc(*"x264") # type: ignore
|
24 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
25 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
26 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
27 |
+
|
28 |
+
iterating, frame = cap.read()
|
29 |
+
|
30 |
+
n_frames = 0
|
31 |
+
n_chunks = 0
|
32 |
+
name = str(current_dir / f"output_{n_chunks}{'.mp4' if stream_as_mp4 else '.ts'}")
|
33 |
+
segment_file = cv2.VideoWriter(name, video_codec, fps, (width, height)) # type: ignore
|
34 |
+
|
35 |
+
while iterating:
|
36 |
+
|
37 |
+
# flip frame vertically
|
38 |
+
frame = cv2.flip(frame, 0)
|
39 |
+
display_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
40 |
+
segment_file.write(display_frame)
|
41 |
+
n_frames += 1
|
42 |
+
if n_frames == 3 * fps:
|
43 |
+
n_chunks += 1
|
44 |
+
segment_file.release()
|
45 |
+
n_frames = 0
|
46 |
+
yield name
|
47 |
+
name = str(
|
48 |
+
current_dir / f"output_{n_chunks}{'.mp4' if stream_as_mp4 else '.ts'}"
|
49 |
+
)
|
50 |
+
segment_file = cv2.VideoWriter(name, video_codec, fps, (width, height)) # type: ignore
|
51 |
+
|
52 |
+
iterating, frame = cap.read()
|
53 |
+
|
54 |
+
segment_file.release()
|
55 |
+
yield name
|
56 |
+
|
57 |
+
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("# Video Streaming Out 📹")
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
input_video = gr.Video(label="input")
|
63 |
+
checkbox = gr.Checkbox(label="Stream as MP4 file?", value=False)
|
64 |
+
with gr.Column():
|
65 |
+
processed_frames = gr.Video(
|
66 |
+
label="stream",
|
67 |
+
streaming=True,
|
68 |
+
autoplay=True,
|
69 |
+
elem_id="stream_video_output",
|
70 |
+
)
|
71 |
+
with gr.Row():
|
72 |
+
process_video_btn = gr.Button("process video")
|
73 |
+
|
74 |
+
process_video_btn.click(process_video, [input_video, checkbox], [processed_frames])
|
75 |
+
|
76 |
+
gr.Examples(
|
77 |
+
[
|
78 |
+
[
|
79 |
+
os.path.join(
|
80 |
+
os.path.dirname(__file__),
|
81 |
+
"video/compliment_bot_screen_recording_3x.mp4",
|
82 |
+
),
|
83 |
+
False,
|
84 |
+
],
|
85 |
+
[
|
86 |
+
os.path.join(
|
87 |
+
os.path.dirname(__file__),
|
88 |
+
"video/compliment_bot_screen_recording_3x.mp4",
|
89 |
+
),
|
90 |
+
True,
|
91 |
+
],
|
92 |
+
],
|
93 |
+
[input_video, checkbox],
|
94 |
+
fn=process_video,
|
95 |
+
outputs=processed_frames,
|
96 |
+
cache_examples=False,
|
97 |
+
)
|
98 |
+
|
99 |
+
|
100 |
+
if __name__ == "__main__":
|
101 |
+
demo.launch()
|
video/compliment_bot_screen_recording_3x.mp4
ADDED
Binary file (223 kB). View file
|
|