Discussion

Créez un convertisseur vidéo de profondeur local avec Gradio

De Wikiprompt, l’encyclopédie libre de prompts

Larus Canus
Contribué parLarus CanusXSource

20 juil. 2026

Créez un convertisseur vidéo de profondeur local avec Gradio Invite détaillé pour créer une application locale de conversion vidéo en profondeur utilisant Gradio et Depth Anything V2, avec prise en charge de l'accélération multiplateforme et de multiples options de personnalisation.

Contenu du PromptEnregistrer

🌐
import os 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 # ========== CONFIG ========== MODEL_IDS = { "small": "depth-anything/Depth-Anything-V2-Small-hf", "base": "depth-anything/Depth-Anything-V2-Base-hf", "large": "depth-anything/Depth-Anything-V2-Large-hf", } DEVICE = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32 # ========== DEPTH ESTIMATION ========== class DepthEstimator: def __init__(self, model_size="small"): self.model_id = MODEL_IDS[model_size] self.pipe = pipeline( task="depth-estimation", model=self.model_id, device=DEVICE, torch_dtype=DTYPE, ) def estimate(self, image: np.ndarray) -> np.ndarray: """Returns grayscale depth map (0-255 uint8)""" pil_img = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) result = self.pipe(pil_img) depth = result["depth"] depth_np = np.array(depth) # Normalize to 0-255 depth_np = cv2.normalize(depth_np, None, 0, 255, cv2.NORM_MINMAX) return depth_np.astype(np.uint8) # ========== TEMPORAL SMOOTHING ========== class TemporalSmoother: def __init__(self, strength=0.5): self.strength = strength self.prev = None def smooth(self, frame: np.ndarray) -> np.ndarray: if self.prev is None: self.prev = frame.astype(np.float32) return frame blended = self.strength * frame.astype(np.float32) + (1 - self.strength) * self.prev self.prev = blended return blended.astype(np.uint8) def reset(self): self.prev = None # ========== VIDEO PROCESSING ========== def process_video( input_path: str, model_size: str = "small", output_resolution: str = "original", invert: bool = False, temporal_smoothing: float = 0.0, preserve_audio: bool = True, progress=gr.Progress(track_tqdm=True), ) -> str: """Main video conversion function""" # Validate input if not os.path.exists(input_path): raise FileNotFoundError(f"Input file not found: {input_path}") # Setup paths temp_dir = tempfile.mkdtemp(prefix="depth_video_") output_path = os.path.join(temp_dir, "output.mp4") try: # Open video cap = cv2.VideoCapture(input_path) if not cap.isOpened(): raise RuntimeError("Could not open video file") fps = cap.get(cv2.CAP_PROP_FPS) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Determine output resolution if output_resolution == "original": out_w, out_h = width, height else: scale = float(output_resolution) / max(width, height) out_w = int(width * scale) out_h = int(height * scale) # Ensure even dimensions out_w = out_w if out_w % 2 == 0 else out_w - 1 out_h = out_h if out_h % 2 == 0 else out_h - 1 # Initialize components estimator = DepthEstimator(model_size) smoother = TemporalSmoother(temporal_smoothing) if temporal_smoothing > 0 else None # Video writer fourcc = cv2.VideoWriter_fourcc(*'mp4v') writer = cv2.VideoWriter(output_path, fourcc, fps, (out_w, out_h), isColor=False) # Process frames frame_idx = 0 progress(0, desc="Processing frames...") while True: ret, frame = cap.read() if not ret: break # Estimate depth depth = estimator.estimate(frame) # Temporal smoothing if smoother: depth = smoother.smooth(depth) # Invert if requested if invert: depth = 255 - depth # Resize if (out_w, out_h) != (width, height): depth = cv2.resize(depth, (out_w, out_h), interpolation=cv2.INTER_LINEAR) # Write frame writer.write(depth) frame_idx += 1 if frame_idx % 10 == 0: progress(frame_idx / total_frames, desc=f"Frame {frame_idx}/{total_frames}") cap.release() writer.release() # Handle audio if preserve_audio: try: audio_temp = os.path.join(temp_dir, "audio.m4a") subprocess.run( ["ffmpeg", "-y", "-i", input_path, "-vn", "-acodec", "copy", audio_temp], check=True, capture_output=True, ) final_output = os.path.join(temp_dir, "final_output.mp4") subprocess.run( ["ffmpeg", "-y", "-i", output_path, "-i", audio_temp, "-c:v", "libx264", "-c:a", "aac", "-shortest", final_output], check=True, capture_output=True, ) output_path = final_output except (subprocess.CalledProcessError, FileNotFoundError): print("Warning: Could not preserve audio, exporting video only") return output_path except Exception as e: raise RuntimeError(f"Video processing failed: {str(e)}") finally: # Cleanup temp files (keep output) for f in os.listdir(temp_dir): if f != os.path.basename(output_path): try: os.remove(os.path.join(temp_dir, f)) except: pass # ========== GRADIO UI ========== def create_ui(): 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_IDS.keys()), value="small", label="Model Size", info="Small = faster, Large = more accurate" ) output_resolution = gr.Dropdown( choices=["original", "480", "720", "1080"], value="original", label="Output Resolution" ) invert = gr.Checkbox(label="Invert Depth (black/white)", value=False) temporal_smoothing = gr.Slider( minimum=0.0, maximum=1.0, value=0.0, step=0.1, label="Temporal Smoothing", info="Higher = less flicker but more lag" ) preserve_audio = gr.Checkbox(label="Preserve Original Audio", value=True) convert_btn = gr.Button("Convert Video", variant="primary") with gr.Column(): output_video = gr.Video(label="Output Depth Video") convert_btn.click( fn=process_video, inputs=[ input_video, model_size, output_resolution, invert, temporal_smoothing, preserve_audio, ], outputs=output_video, ) return demo # ========== MAIN ========== if __name__ == "__main__": demo = create_ui() demo.launch(server_name="127.0.0.1", server_port=7860) ``` ```text # requirements.txt gradio>=4.0.0 torch>=2.0.0 torchvision>=0.15.0 transformers>=4.30.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 grayscale depth videos using Depth Anything V2, running locally on your machine. ## Features - **Local Processing**: All computation runs on your machine - no cloud services - **Hardware Acceleration**: Automatically uses NVIDIA CUDA on Windows, Apple Silicon (MPS) on Mac, falls back to CPU - **Model Selection**: Choose between Small (fast), Base (balanced), or Large (accurate) models - **Flexible Output**: Adjustable resolution, black/white inversion, temporal smoothing - **Audio Preservation**: Optionally keep original audio track using ffmpeg - **Simple Web UI**: Built with Gradio, accessible from your browser ## Installation ### Prerequisites - Python 3.9 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 app.py # macOS python3 app.py ``` The web interface will open at `http://127.0.0.1:7860` ### Using the interface 1. Upload an MP4 or MOV video file 2. Select model size (Small is fastest, Large is most accurate) 3. Choose output resolution (original or scaled) 4. Optionally invert depth colors or enable temporal smoothing 5. Choose whether to preserve original audio 6. Click "Convert Video" and wait for processing ## Hardware Acceleration The script automatically detects and uses available hardware: - **Windows with NVIDIA GPU**: Uses CUDA acceleration - **macOS with Apple Silicon (M1/M2/M3)**: Uses Metal Performance Shaders (MPS) - **CPU only**: Falls back to CPU processing (slower but works everywhere) ## Output Format The output is an MP4 video with: - H.264 video codec - AAC audio (if preserved) - Grayscale depth visualization - Same frame rate as input ## Troubleshooting ### CUDA not detected on Windows - Ensure you have NVIDIA drivers installed - Install PyTorch with CUDA support: `pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118` ### MPS not detected on Mac - Requires macOS 12.3+ with Apple Silicon - Install PyTorch with MPS support: `pip install torch torchvision` ### ffmpeg not found - Install ffmpeg and ensure it's in your system PATH - Windows: Add the ffmpeg bin folder to PATH - macOS: `brew install ffmpeg` ### Out of memory errors - Use the "small" model - Reduce output resolution - Close other applications ## 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 on the repository. ``` ```bash # Windows Installation Commands python -m venv venv venv\Scripts\activate pip install -r requirements.txt python app.py # macOS Installation Commands python3 -m venv venv source venv/bin/activate pip install -r requirements.txt python3 app.py

Connectez-vous pour voir le prompt complet

Continuer avec:

En vous connectant, vous acceptez nos Conditions et Confidentialité

Utilisation

Ce prompt est conçu pour être utilisé avec coding. Copiez le contenu ci-dessus et collez-le dans votre outil d’IA préféré.

Pour de meilleurs résultats, personnalisez les espaces réservés (indiqués par des crochets ou des majuscules) selon vos besoins.

Références

Catégories :coding| twitter| python| gradio

Discussion