构建一个使用Gradio的本地深度视频转换器
来自 Wikiprompt,自由的提示词百科全书
构建一个使用Gradio的本地深度视频转换器 一个使用Gradio和Depth Anything V2创建本地深度视频转换器应用的详细提示,支持跨平台加速和多种自定义选项。
提示词内容收藏
🌐
# depth_video_converter.py
import os
import sys
import tempfile
import subprocess
import numpy as np
import cv2
import torch
import gradio as gr
from PIL import Image
from transformers import pipeline
from pathlib import Path
import shutil
import json
# ========== Configuration ==========
MODEL_SIZES = {
"small": "depth-anything/Depth-Anything-V2-Small",
"base": "depth-anything/Depth-Anything-V2-Base",
"large": "depth-anything/Depth-Anything-V2-Large",
"giant": "depth-anything/Depth-Anything-V2-Giant"
}
DEFAULT_OUTPUT_RES = 512
TEMP_DIR = tempfile.mkdtemp(prefix="depth_video_")
# ========== Device Detection ==========
def get_device():
"""Detect best available device (CUDA > MPS > CPU)"""
if torch.cuda.is_available():
return "cuda"
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return "mps"
else:
return "cpu"
DEVICE = get_device()
print(f"Using device: {DEVICE}")
# ========== Depth Model ==========
def load_depth_model(model_size):
"""Load Depth Anything V2 model via HuggingFace pipeline"""
model_id = MODEL_SIZES.get(model_size, MODEL_SIZES["small"])
print(f"Loading model: {model_id}")
pipe = pipeline(
task="depth-estimation",
model=model_id,
device=DEVICE if DEVICE != "mps" else -1 # MPS fallback to CPU for pipeline
)
return pipe
# ========== Video Processing ==========
def extract_frames(video_path, target_fps=None):
"""Extract frames from video using OpenCV"""
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
raise ValueError(f"Cannot open video: {video_path}")
fps = cap.get(cv2.CAP_PROP_FPS)
if target_fps and target_fps < fps:
frame_interval = int(fps / target_fps)
else:
frame_interval = 1
frames = []
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
if frame_count % frame_interval == 0:
frames.append(frame)
frame_count += 1
cap.release()
return frames, fps
def process_frames_with_depth(frames, model, output_res, invert, temporal_smoothing):
"""Convert frames to depth maps with optional temporal smoothing"""
depth_frames = []
prev_depth = None
for i, frame in enumerate(frames):
# Convert BGR to RGB for model
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_frame)
# Get depth prediction
result = model(pil_image)
depth = result["depth"]
# Convert to numpy array
if isinstance(depth, Image.Image):
depth_np = np.array(depth)
else:
depth_np = np.array(depth)
# Resize to output resolution
depth_np = cv2.resize(depth_np, (output_res, output_res), interpolation=cv2.INTER_LINEAR)
# Normalize to 0-255
depth_np = cv2.normalize(depth_np, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
# Invert if requested
if invert:
depth_np = 255 - depth_np
# Temporal smoothing (exponential moving average)
if temporal_smoothing > 0 and prev_depth is not None:
alpha = temporal_smoothing
depth_np = (alpha * depth_np + (1 - alpha) * prev_depth).astype(np.uint8)
prev_depth = depth_np
depth_frames.append(depth_np)
# Progress update
if (i + 1) % 10 == 0:
print(f"Processed {i + 1}/{len(frames)} frames")
return depth_frames
def create_video_from_frames(frames, output_path, fps, audio_path=None):
"""Create video from frames, optionally adding audio"""
height, width = frames[0].shape[:2]
# Write video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height), isColor=False)
for frame in frames:
out.write(frame)
out.release()
# Add audio if provided
if audio_path and os.path.exists(audio_path):
temp_video = output_path + ".temp.mp4"
os.rename(output_path, temp_video)
cmd = [
"ffmpeg", "-y",
"-i", temp_video,
"-i", audio_path,
"-c:v", "libx264",
"-c:a", "aac",
"-shortest",
output_path
]
subprocess.run(cmd, capture_output=True)
os.remove(temp_video)
# ========== Main Processing Function ==========
def convert_video(
input_video,
model_size="small",
output_res=DEFAULT_OUTPUT_RES,
invert=False,
temporal_smoothing=0.5,
preserve_audio=True,
progress=gr.Progress()
):
"""Main conversion function for Gradio UI"""
try:
progress(0, desc="Initializing...")
# Validate input
if not input_video:
return None, "Please upload a video file."
# Load model
progress(0.1, desc="Loading depth model...")
model = load_depth_model(model_size)
# Extract frames
progress(0.2, desc="Extracting frames...")
frames, original_fps = extract_frames(input_video)
if len(frames) == 0:
return None, "No frames extracted from video."
# Process frames
progress(0.3, desc="Processing depth maps...")
depth_frames = process_frames_with_depth(
frames, model, output_res, invert, temporal_smoothing
)
# Create output video
progress(0.9, desc="Creating output video...")
output_path = os.path.join(TEMP_DIR, "depth_output.mp4")
# Extract audio if needed
audio_path = None
if preserve_audio:
audio_path = os.path.join(TEMP_DIR, "temp_audio.aac")
cmd = [
"ffmpeg", "-y",
"-i", input_video,
"-vn", "-acodec", "copy",
audio_path
]
result = subprocess.run(cmd, capture_output=True)
if result.returncode != 0:
audio_path = None # Audio extraction failed, continue without
create_video_from_frames(depth_frames, output_path, original_fps, audio_path)
progress(1.0, desc="Done!")
return output_path, "Conversion successful!"
except Exception as e:
return None, f"Error: {str(e)}"
# ========== Gradio UI ==========
def create_ui():
"""Create Gradio interface"""
with gr.Blocks(title="Depth Video Converter") as demo:
gr.Markdown("""
# Depth Video Converter
Convert regular videos to grayscale depth videos using Depth Anything V2.
""")
with gr.Row():
with gr.Column():
input_video = gr.Video(
label="Input Video (MP4/MOV)",
format="mp4"
)
model_size = gr.Dropdown(
choices=list(MODEL_SIZES.keys()),
value="small",
label="Model Size"
)
output_res = gr.Slider(
minimum=256,
maximum=1024,
value=DEFAULT_OUTPUT_RES,
step=64,
label="Output Resolution"
)
invert = gr.Checkbox(
label="Invert Depth (black/white)",
value=False
)
temporal_smoothing = gr.Slider(
minimum=0.0,
maximum=0.9,
value=0.5,
step=0.1,
label="Temporal Smoothing (reduce flicker)"
)
preserve_audio = gr.Checkbox(
label="Preserve Original Audio",
value=True
)
convert_btn = gr.Button("Convert to Depth Video", variant="primary")
with gr.Column():
output_video = gr.Video(label="Output Depth Video")
status = gr.Textbox(label="Status", interactive=False)
convert_btn.click(
fn=convert_video,
inputs=[
input_video,
model_size,
output_res,
invert,
temporal_smoothing,
preserve_audio
],
outputs=[output_video, status]
)
return demo
# ========== Main Entry ==========
if __name__ == "__main__":
print("=" * 50)
print("Depth Video Converter")
print(f"Device: {DEVICE}")
print("=" * 50)
demo = create_ui()
demo.launch(
server_name="127.0.0.1",
server_port=7860,
share=False
)
```
```text
# requirements.txt
gradio>=4.0.0
opencv-python>=4.8.0
torch>=2.0.0
transformers>=4.35.0
Pillow>=10.0.0
numpy>=1.24.0
ffmpeg-python>=0.2.0
```
```markdown
# Depth Video Converter
Convert regular videos to grayscale depth videos using Depth Anything V2, running locally on Windows and macOS.
## Features
- **Web UI** via Gradio (simple browser interface)
- **MP4/MOV** video upload support
- **Depth Anything V2** model for depth estimation
- **Hardware acceleration**:
- NVIDIA CUDA on Windows (when available)
- Apple Silicon (MPS) on macOS (when available)
- CPU fallback
- **Model size selection**: small, base, large, giant
- **Output resolution** control (256-1024px)
- **Black/white inversion** option
- **Temporal smoothing** to reduce flicker
- **Audio preservation** with ffmpeg
- **MP4 export** compatible with most players
## Installation
### Prerequisites
- Python 3.8 or higher
- ffmpeg installed on your system
### Windows
```bash
# 1. Install Python from python.org (check "Add to PATH")
# 2. Install ffmpeg
# Download from https://ffmpeg.org/download.html
# Add ffmpeg.exe to your PATH
# 3. Create and activate virtual environment
python -m venv venv
venv\Scripts\activate
# 4. Install dependencies
pip install -r requirements.txt
# 5. (Optional) Install CUDA support for NVIDIA GPUs
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
```
### macOS
```bash
# 1. Install Homebrew if not installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 2. Install Python and ffmpeg
brew install python ffmpeg
# 3. Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate
# 4. Install dependencies
pip install -r requirements.txt
# 5. (Optional) Install PyTorch with MPS support (Apple Silicon)
pip install torch torchvision
```
## Usage
### Launch the application
```bash
# Windows
python depth_video_converter.py
# macOS
python3 depth_video_converter.py
```
### Using the Web UI
1. Open your browser to `http://127.0.0.1:7860`
2. Upload an MP4 or MOV video file
3. Configure settings:
- **Model Size**: Choose between small (fast) and giant (accurate)
- **Output Resolution**: Higher = better quality but slower
- **Invert Depth**: Swap black/white for different visual styles
- **Temporal Smoothing**: Higher values reduce flicker but may blur motion
- **Preserve Audio**: Keep original audio track
4. Click "Convert to Depth Video"
5. Download the result
## Performance Tips
- **Small model + 256px resolution**: Fastest, good for previews
- **Large model + 512px resolution**: Balanced quality/speed
- **Giant model + 1024px resolution**: Best quality, requires powerful GPU
- Use CUDA on Windows for 2-5x speedup
- Use MPS on Apple Silicon for 1.5-3x speedup
## Troubleshooting
### "No module named 'torch'"
Run: `pip install torch torchvision`
### ffmpeg not found
- Windows: Download and add to PATH
- macOS: `brew install ffmpeg`
### CUDA not detected
- Ensure NVIDIA drivers are installed
- Install CUDA version of PyTorch (see installation)
### MPS not detected on Mac
- Requires Apple Silicon (M1/M2/M3)
- Update macOS and PyTorch
## License
This project uses Depth Anything V2 (Apache 2.0) and is for educational/research purposes.
```
```text
# Installation and Launch Commands
## Windows (PowerShell or CMD)
```bash
# One-time setup
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
# Optional: CUDA support for NVIDIA GPUs
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
# Launch
python depth_video_converter.py
```
## macOS (Terminal)
```bash
# One-time setup
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Optional: MPS support for Apple Silicon
pip install torch torchvision
# Launch
python3 depth_video_converter.py
```
## Quick Start (both platforms)
```bash
# After installation, just run:
python depth_video_converter.py
# or
python3 depth_video_converter.py
```
Then open http://127.0.0.1:7860 in your browser.
用法
此提示词专为 coding 设计。复制上方内容并粘贴到你常用的 AI 工具中。
为获得最佳效果,可将占位符(方括号或大写字母标示)替换为你的具体需求。
讨论
0 条评论