Diskussion

Erstelle einen lokalen Depth-Video-Konverter mit Gradio

Von Wikiprompt, der freien Prompt-Enzyklopädie

Larus Canus
Beigetragen vonLarus CanusXQuelle

20. Juli 2026

Erstelle einen lokalen Depth-Video-Konverter mit Gradio Ein detaillierter Prompt zur Erstellung einer lokalen Depth-Video-Konverter-Anwendung mit Gradio und Depth Anything V2, mit plattformübergreifender Beschleunigungsunterstützung und mehreren Anpassungsoptionen.

Prompt-InhaltSpeichern

🌐
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 transformers import AutoModelForDepthEstimation, AutoProcessor import shutil import platform import json from pathlib import Path # ============================================================ # Configuration and Device Detection # ============================================================ def detect_device(): """Detect best available device for depth estimation.""" if torch.cuda.is_available(): return "cuda", "NVIDIA CUDA" elif platform.system() == "Darwin" and torch.backends.mps.is_available(): return "mps", "Apple Silicon (MPS)" else: return "cpu", "CPU" DEVICE, DEVICE_NAME = detect_device() # Model configurations MODEL_CONFIGS = { "small": { "model_id": "depth-anything/Depth-Anything-V2-Small-hf", "description": "Fastest, lower quality" }, "base": { "model_id": "depth-anything/Depth-Anything-V2-Base-hf", "description": "Balanced speed/quality" }, "large": { "model_id": "depth-anything/Depth-Anything-V2-Large-hf", "description": "Best quality, slower" } } # ============================================================ # Depth Estimation Pipeline # ============================================================ class DepthEstimator: def __init__(self, model_size="base"): self.model_size = model_size self.model = None self.processor = None self.load_model() def load_model(self): """Load the depth estimation model.""" config = MODEL_CONFIGS[self.model_size] try: self.processor = AutoProcessor.from_pretrained(config["model_id"]) self.model = AutoModelForDepthEstimation.from_pretrained(config["model_id"]) self.model.to(DEVICE) self.model.eval() except Exception as e: raise RuntimeError(f"Failed to load model: {str(e)}") def estimate_depth(self, frame): """Estimate depth for a single frame.""" # Convert BGR to RGB rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) pil_image = Image.fromarray(rgb_frame) # Process image inputs = self.processor(images=pil_image, return_tensors="pt").to(DEVICE) with torch.no_grad(): outputs = self.model(**inputs) predicted_depth = outputs.predicted_depth # Interpolate to original size prediction = torch.nn.functional.interpolate( predicted_depth.unsqueeze(1), size=pil_image.size[::-1], mode="bicubic", align_corners=False, ) # Convert to numpy and normalize depth = prediction.squeeze().cpu().numpy() depth = (depth - depth.min()) / (depth.max() - depth.min()) depth = (depth * 255).astype(np.uint8) return depth # ============================================================ # Video Processing Functions # ============================================================ def temporal_smoothing(depth_frames, window_size=3): """Apply temporal smoothing to reduce flicker.""" if window_size <= 1: return depth_frames smoothed = [] half_window = window_size // 2 for i in range(len(depth_frames)): start = max(0, i - half_window) end = min(len(depth_frames), i + half_window + 1) window = depth_frames[start:end] smoothed.append(np.mean(window, axis=0).astype(np.uint8)) return smoothed def process_video(input_path, model_size, output_resolution, invert_depth, temporal_smoothing_enabled, preserve_audio, progress_callback=None): """Process video file to depth video.""" # Initialize depth estimator depth_estimator = DepthEstimator(model_size) # Open video cap = cv2.VideoCapture(input_path) if not cap.isOpened(): raise ValueError("Could not open video file") # Get video properties fps = cap.get(cv2.CAP_PROP_FPS) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # Calculate output dimensions if output_resolution == "original": width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) else: # Parse resolution like "1280x720" w, h = output_resolution.lower().split("x") width, height = int(w), int(h) # Create temporary directory for frames temp_dir = tempfile.mkdtemp() try: # Process frames depth_frames = [] frame_count = 0 while True: ret, frame = cap.read() if not ret: break # Estimate depth depth = depth_estimator.estimate_depth(frame) # Invert if requested if invert_depth: depth = 255 - depth # Resize if needed if (width, height) != (depth.shape[1], depth.shape[0]): depth = cv2.resize(depth, (width, height), interpolation=cv2.INTER_LINEAR) depth_frames.append(depth) frame_count += 1 if progress_callback: progress_callback(frame_count / total_frames) cap.release() # Apply temporal smoothing if enabled if temporal_smoothing_enabled: depth_frames = temporal_smoothing(depth_frames) # Write output video output_path = os.path.join(temp_dir, "depth_video.mp4") fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_path, fourcc, fps, (width, height), False) for frame in depth_frames: out.write(frame) out.release() # Handle audio preservation if preserve_audio: final_output = os.path.join(temp_dir, "final_output.mp4") cmd = [ "ffmpeg", "-y", "-i", output_path, "-i", input_path, "-c:v", "copy", "-c:a", "aac", "-map", "0:v:0", "-map", "1:a:0", "-shortest", final_output ] subprocess.run(cmd, capture_output=True, check=True) return final_output return output_path finally: # Cleanup shutil.rmtree(temp_dir, ignore_errors=True) # ============================================================ # Gradio Interface # ============================================================ def create_interface(): """Create the Gradio web interface.""" def process_video_wrapper(video_file, model_size, output_resolution, invert_depth, smoothing, preserve_audio, progress=gr.Progress()): """Wrapper function for Gradio interface.""" if video_file is None: raise gr.Error("Please upload a video file") # Convert progress to callback def progress_callback(progress_value): progress(progress_value, desc="Processing frames...") try: output_path = process_video( video_file, model_size, output_resolution, invert_depth, smoothing, preserve_audio, progress_callback ) return output_path except Exception as e: raise gr.Error(f"Processing failed: {str(e)}") with gr.Blocks(title="Depth Video Converter") as interface: gr.Markdown(""" # Depth Video Converter Convert regular videos to depth videos using Depth Anything V2. **Device:** {device_name} """.format(device_name=DEVICE_NAME)) with gr.Row(): with gr.Column(scale=1): video_input = gr.Video( label="Input Video", sources=["upload"], format="mp4" ) model_size = gr.Dropdown( choices=list(MODEL_CONFIGS.keys()), value="base", label="Model Size", info="Small: Fastest | Base: Balanced | Large: Best quality" ) output_resolution = gr.Dropdown( choices=["original", "640x480", "1280x720", "1920x1080"], value="original", label="Output Resolution" ) invert_depth = gr.Checkbox( label="Invert Depth (black/white)", value=False ) smoothing = gr.Checkbox( label="Temporal Smoothing (reduce flicker)", value=True ) preserve_audio = gr.Checkbox( label="Preserve Original Audio", value=True ) process_button = gr.Button("Convert to Depth Video", variant="primary") with gr.Column(scale=1): video_output = gr.Video( label="Depth Video Output", format="mp4" ) process_button.click( fn=process_video_wrapper, inputs=[ video_input, model_size, output_resolution, invert_depth, smoothing, preserve_audio ], outputs=video_output ) return interface # ============================================================ # Main Entry Point # ============================================================ if __name__ == "__main__": print(f"Using device: {DEVICE_NAME}") print("Starting Depth Video Converter...") interface = create_interface() interface.launch( server_name="127.0.0.1", server_port=7860, share=False ) ``` ```text # requirements.txt torch>=2.0.0 torchvision>=0.15.0 transformers>=4.36.0 gradio>=4.0.0 opencv-python>=4.8.0 numpy>=1.24.0 Pillow>=10.0.0 ffmpeg-python>=0.2.0 ``` ```markdown # Depth Video Converter Convert regular videos to depth videos using Depth Anything V2 with a simple web interface. ## Features - **Web UI** - Simple Gradio interface for easy video conversion - **Multiple Models** - Choose between Small, Base, and Large Depth Anything V2 models - **Hardware Acceleration** - Automatic detection and use of: - NVIDIA CUDA on Windows - Apple Silicon (MPS) on macOS - CPU fallback when no GPU is available - **Output Controls**: - Resolution selection (original, 640x480, 1280x720, 1920x1080) - Black/white inversion - Temporal smoothing to reduce flicker - Audio preservation using ffmpeg - **Format Support** - MP4 and MOV input files, MP4 output ## Installation ### Prerequisites - Python 3.8 or higher - FFmpeg installed on your system - **Windows**: Download from [ffmpeg.org](https://ffmpeg.org/download.html) and add to PATH - **macOS**: `brew install ffmpeg` ### Setup 1. Clone or download this repository 2. Create a virtual environment (recommended): ```bash # Windows python -m venv venv venv\Scripts\activate # macOS python3 -m venv venv source venv/bin/activate ``` 3. Install dependencies: ```bash pip install -r requirements.txt ``` ## Usage ### Launch the application ```bash # Windows python depth_video_converter.py # macOS python3 depth_video_converter.py ``` ### Using the Web Interface 1. Open your browser and go to `http://127.0.0.1:7860` 2. Upload your video file (MP4 or MOV) 3. Configure settings: - **Model Size**: Choose between Small (fastest), Base (balanced), or Large (best quality) - **Output Resolution**: Select desired output resolution - **Invert Depth**: Toggle black/white inversion - **Temporal Smoothing**: Enable to reduce flicker between frames - **Preserve Audio**: Keep original audio track 4. Click "Convert to Depth Video" 5. Download the processed video ## Hardware Acceleration The application automatically detects and uses the best available hardware: - **Windows with NVIDIA GPU**: Uses CUDA acceleration - **macOS with Apple Silicon**: Uses MPS (Metal Performance Shaders) acceleration - **CPU only**: Falls back to CPU processing (slower but works everywhere) ## Model Information | Model | Size | Speed | Quality | Use Case | |-------|------|-------|---------|----------| | Small | ~25MB | Fastest | Lower | Quick previews, real-time processing | | Base | ~90MB | Balanced | Good | General purpose | | Large | ~300MB | Slowest | Best | High-quality output | ## Troubleshooting ### Common Issues 1. **CUDA not detected on Windows** - Ensure NVIDIA drivers are up to date - Install PyTorch with CUDA support: `pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118` 2. **MPS not detected on macOS** - Requires macOS 12.3+ with Apple Silicon - Install PyTorch with MPS support: `pip install torch torchvision` 3. **FFmpeg not found** - Install FFmpeg and ensure it's in your system PATH - Windows: Download from [ffmpeg.org](https://ffmpeg.org/download.html) - macOS: `brew install ffmpeg` 4. **Out of memory** - Use smaller model size - Reduce output resolution - Close other applications ## Performance Tips - Use the "Small" model for faster processing - Reduce output resolution for quicker processing - Disable temporal smoothing for faster processing - Enable audio preservation only when needed ## License This project uses: - Depth Anything V2 (Apache 2.0) - Gradio (Apache 2.0) - PyTorch (BSD-style) - OpenCV (Apache 2.0) ## Support For issues or questions, please open an issue in the repository.

Melde dich an, um den vollständigen Prompt zu sehen

Weiter mit:

Mit der Anmeldung akzeptierst du unsere Nutzungsbedingungen und Datenschutz

Verwendung

Dieser Prompt ist für die Verwendung mit coding gedacht. Kopiere den Inhalt oben und füge ihn in dein bevorzugtes KI-Tool ein.

Für beste Ergebnisse passe die Platzhalter (eckige Klammern oder Großbuchstaben) an deine Anforderungen an.

Referenzen

Kategorien:coding| twitter| python| gradio

Diskussion