Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from run import extract_frame, remove_subtitles
|
|
|
|
| 4 |
|
| 5 |
with gr.Blocks() as demo:
|
| 6 |
-
gr.Markdown("### 🎬 三视图字幕去除工具(2+1 布局)")
|
| 7 |
|
| 8 |
with gr.Row():
|
| 9 |
with gr.Column():
|
| 10 |
video_input = gr.Video(label="上传视频")
|
| 11 |
with gr.Column():
|
|
|
|
| 12 |
frame_output = gr.Image(label="参考帧")
|
| 13 |
mask_editor = gr.ImageEditor(label="框选字幕区域", type="pil")
|
| 14 |
|
| 15 |
with gr.Row():
|
| 16 |
result_video = gr.Video(label="去除字幕后的视频")
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return Image.fromarray(frame) if frame is not None else None
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
frame_output.change(lambda img: img, inputs=frame_output, outputs=mask_editor)
|
| 24 |
|
|
|
|
| 25 |
run_btn = gr.Button("去除字幕")
|
| 26 |
run_btn.click(fn=remove_subtitles, inputs=[video_input, mask_editor], outputs=result_video)
|
| 27 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
from run import extract_frame, remove_subtitles
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
with gr.Blocks() as demo:
|
| 7 |
+
gr.Markdown("### 🎬 三视图字幕去除工具(2+1 布局 + 滑块选择参考帧)")
|
| 8 |
|
| 9 |
with gr.Row():
|
| 10 |
with gr.Column():
|
| 11 |
video_input = gr.Video(label="上传视频")
|
| 12 |
with gr.Column():
|
| 13 |
+
frame_slider = gr.Slider(minimum=0, maximum=200, step=1, value=10, label="选择参考帧编号")
|
| 14 |
frame_output = gr.Image(label="参考帧")
|
| 15 |
mask_editor = gr.ImageEditor(label="框选字幕区域", type="pil")
|
| 16 |
|
| 17 |
with gr.Row():
|
| 18 |
result_video = gr.Video(label="去除字幕后的视频")
|
| 19 |
|
| 20 |
+
# 获取视频总帧数,动态更新滑块范围
|
| 21 |
+
def get_total_frames(video):
|
| 22 |
+
cap = cv2.VideoCapture(video)
|
| 23 |
+
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 24 |
+
cap.release()
|
| 25 |
+
return gr.update(maximum=max(1, total-1))
|
| 26 |
+
|
| 27 |
+
# 根据滑块选择的帧号提取参考帧
|
| 28 |
+
def get_frame(video, frame_idx):
|
| 29 |
+
frame = extract_frame(video, frame_idx)
|
| 30 |
return Image.fromarray(frame) if frame is not None else None
|
| 31 |
|
| 32 |
+
# 上传视频时更新滑块范围并显示默认帧
|
| 33 |
+
video_input.change(fn=get_total_frames, inputs=video_input, outputs=frame_slider)
|
| 34 |
+
video_input.change(fn=lambda v: get_frame(v, 10), inputs=video_input, outputs=frame_output)
|
| 35 |
+
|
| 36 |
+
# 滑块改变时更新参考帧
|
| 37 |
+
frame_slider.change(fn=get_frame, inputs=[video_input, frame_slider], outputs=frame_output)
|
| 38 |
+
|
| 39 |
+
# 将参考帧传给编辑器
|
| 40 |
frame_output.change(lambda img: img, inputs=frame_output, outputs=mask_editor)
|
| 41 |
|
| 42 |
+
# 点击按钮执行去字幕
|
| 43 |
run_btn = gr.Button("去除字幕")
|
| 44 |
run_btn.click(fn=remove_subtitles, inputs=[video_input, mask_editor], outputs=result_video)
|
| 45 |
|