# Depth Anything

Estimar la profundidad a partir de imágenes individuales con Depth Anything.

{% hint style="success" %}
Todos los ejemplos se pueden ejecutar en servidores GPU alquilados a través de [CLORE.AI Marketplace](https://clore.ai/marketplace).
{% endhint %}

## Alquilar en CLORE.AI

1. Visita [CLORE.AI Marketplace](https://clore.ai/marketplace)
2. Filtrar por tipo de GPU, VRAM y precio
3. Elegir **Bajo demanda** (tarifa fija) o **Spot** (precio de puja)
4. Configura tu pedido:
   * Selecciona imagen Docker
   * Establece puertos (TCP para SSH, HTTP para interfaces web)
   * Agrega variables de entorno si es necesario
   * Introduce el comando de inicio
5. Selecciona pago: **CLORE**, **BTC**, o **USDT/USDC**
6. Crea el pedido y espera el despliegue

### Accede a tu servidor

* Encuentra los detalles de conexión en **Mis Pedidos**
* Interfaces web: Usa la URL del puerto HTTP
* SSH: `ssh -p <port> root@<proxy-address>`

## ¿Qué es Depth Anything?

Depth Anything ofrece:

* Estimación de profundidad de última generación
* Funciona con cualquier imagen
* No se necesita cámara estéreo
* Inferencia rápida

## Variantes de modelo

| Modelo               | Tamaño | VRAM  | Velocidad     |
| -------------------- | ------ | ----- | ------------- |
| Depth-Anything-Small | 25M    | 2GB   | El más rápido |
| Depth-Anything-Base  | 98M    | 4GB   | Rápido        |
| Depth-Anything-Large | 335M   | 8GB   | Mejor calidad |
| Depth-Anything-V2    | Varios | 4-8GB | Último        |

## Despliegue rápido

**Imagen Docker:**

```
pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime
```

**Puertos:**

```
22/tcp
7860/http
```

**Comando:**

```bash
pip install transformers torch gradio && \
python depth_anything_app.py
```

## Accediendo a tu servicio

Después del despliegue, encuentra tu `http_pub` URL en **Mis Pedidos**:

1. Ir a **Mis Pedidos** página
2. Haz clic en tu pedido
3. Encuentra la `http_pub` URL (por ejemplo, `abc123.clorecloud.net`)

Usa `https://TU_HTTP_PUB_URL` en lugar de `localhost` en los ejemplos abajo.

## Instalación

```bash
pip install transformers torch
pip install opencv-python pillow
```

## Uso básico

```python
from transformers import pipeline
from PIL import Image

# Cargar la canalización de estimación de profundidad
pipe = pipeline(
    task="depth-estimation",
    model="LiheYoung/depth-anything-large-hf",
    device="cuda"
)

# Estimar profundidad
image = Image.open("photo.jpg")
depth = pipe(image)

# Guardar el mapa de profundidad
depth["depth"].save("depth_map.png")
```

## Depth Anything V2

```python
from transformers import AutoImageProcessor, AutoModelForDepthEstimation
import torch
from PIL import Image
import numpy as np

# Cargar modelo
processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Large-hf")
model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Large-hf")
model.to("cuda")

# Procesar imagen
image = Image.open("photo.jpg")
inputs = processor(images=image, return_tensors="pt").to("cuda")

with torch.no_grad():
    outputs = model(**inputs)
    predicted_depth = outputs.predicted_depth

# Interpolar al tamaño original
prediction = torch.nn.functional.interpolate(
    predicted_depth.unsqueeze(1),
    size=image.size[::-1],
    mode="bicubic",
    align_corners=False,
)

# Convertir a numpy
depth = prediction.squeeze().cpu().numpy()
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
depth = depth.astype(np.uint8)

# Guardar
Image.fromarray(depth).save("depth.png")
```

## Mapa de profundidad coloreado

```python
import cv2
import numpy as np
from PIL import Image

def colorize_depth(depth_array, colormap=cv2.COLORMAP_INFERNO):
    # Normalizar a 0-255
    depth_normalized = cv2.normalize(depth_array, None, 0, 255, cv2.NORM_MINMAX)
    depth_uint8 = depth_normalized.astype(np.uint8)

    # Aplicar mapa de color
    colored = cv2.applyColorMap(depth_uint8, colormap)

    return Image.fromarray(cv2.cvtColor(colored, cv2.COLOR_BGR2RGB))

# Uso
depth_colored = colorize_depth(depth)
depth_colored.save("depth_colored.png")
```

## Procesamiento por lotes

```python
from transformers import pipeline
from PIL import Image
import os

pipe = pipeline(
    task="depth-estimation",
    model="LiheYoung/depth-anything-large-hf",
    device="cuda"
)

input_dir = "./images"
output_dir = "./depth_maps"
os.makedirs(output_dir, exist_ok=True)

for filename in os.listdir(input_dir):
    if filename.endswith(('.jpg', '.png', '.jpeg')):
        image_path = os.path.join(input_dir, filename)
        image = Image.open(image_path)

        # Obtener profundidad
        depth = pipe(image)

        # Guardar
        output_path = os.path.join(output_dir, f"depth_{filename}")
        depth["depth"].save(output_path)
        print(f"Procesado: {filename}")
```

## Interfaz Gradio

```python
import gradio as gr
from transformers import pipeline
import cv2
import numpy as np

pipe = pipeline(
    task="depth-estimation",
    model="LiheYoung/depth-anything-large-hf",
    device="cuda"
)

def estimate_depth(image, colormap):
    # Obtener profundidad
    result = pipe(image)
    depth = np.array(result["depth"])

    # Colorear
    depth_normalized = cv2.normalize(depth, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)

    colormaps = {
        "Inferno": cv2.COLORMAP_INFERNO,
        "Viridis": cv2.COLORMAP_VIRIDIS,
        "Plasma": cv2.COLORMAP_PLASMA,
        "Magma": cv2.COLORMAP_MAGMA,
        "Jet": cv2.COLORMAP_JET
    }

    colored = cv2.applyColorMap(depth_normalized, colormaps[colormap])
    colored = cv2.cvtColor(colored, cv2.COLOR_BGR2RGB)

    return result["depth"], colored

demo = gr.Interface(
    fn=estimate_depth,
    inputs=[
        gr.Image(type="pil", label="Imagen de entrada"),
        gr.Dropdown(
            ["Inferno", "Viridis", "Plasma", "Magma", "Jet"],
            value="Inferno",
            label="Mapa de colores"
        )
    ],
    outputs=[
        gr.Image(label="Mapa de profundidad (Escala de grises)"),
        gr.Image(label="Mapa de profundidad (Coloreado)")
    ],
    title="Depth Anything - Estimación de profundidad"
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## Servidor API

```python
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import Response
from transformers import pipeline
from PIL import Image
import io
import numpy as np
import cv2

app = FastAPI()

pipe = pipeline(
    task="depth-estimation",
    model="LiheYoung/depth-anything-large-hf",
    device="cuda"
)

@app.post("/depth")
async def estimate_depth(image: UploadFile = File(...), colored: bool = True):
    # Cargar imagen
    img = Image.open(io.BytesIO(await image.read()))

    # Estimar profundidad
    result = pipe(img)
    depth = np.array(result["depth"])

    if colored:
        depth_normalized = cv2.normalize(depth, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
        depth_img = cv2.applyColorMap(depth_normalized, cv2.COLORMAP_INFERNO)
        depth_img = cv2.cvtColor(depth_img, cv2.COLOR_BGR2RGB)
    else:
        depth_img = depth

    # Convertir a bytes
    output = Image.fromarray(depth_img)
    buffer = io.BytesIO()
    output.save(buffer, format="PNG")

    return Response(content=buffer.getvalue(), media_type="image/png")

# Ejecutar: uvicorn server:app --host 0.0.0.0 --port 8000
```

## Generación de nube de puntos 3D

```python
import numpy as np
import open3d as o3d
from PIL import Image

def depth_to_pointcloud(rgb_image, depth_map, focal_length=500):
    """Convertir imagen RGB y mapa de profundidad a nube de puntos 3D"""
    rgb = np.array(rgb_image)
    depth = np.array(depth_map)

    # Obtener dimensiones de la imagen
    height, width = depth.shape

    # Crear malla de rejilla
    u = np.arange(width)
    v = np.arange(height)
    u, v = np.meshgrid(u, v)

    # Convertir a coordenadas 3D
    z = depth.astype(float)
    x = (u - width / 2) * z / focal_length
    y = (v - height / 2) * z / focal_length

    # Apilar coordenadas
    points = np.stack([x, y, z], axis=-1).reshape(-1, 3)
    colors = rgb.reshape(-1, 3) / 255.0

    # Crear nube de puntos
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(points)
    pcd.colors = o3d.utility.Vector3dVector(colors)

    return pcd

# Uso
rgb = Image.open("photo.jpg")
depth = pipe(rgb)["depth"]

pcd = depth_to_pointcloud(rgb, depth)
o3d.io.write_point_cloud("output.ply", pcd)
```

## Casos de uso

### Efecto de foto 3D

```python
def create_3d_photo(image, depth, shift=20):
    """Crear efecto de paralaje para fotos 3D"""
    import cv2
    import numpy as np

    img = np.array(image)
    depth_arr = np.array(depth)

    # Normalizar profundidad
    depth_norm = (depth_arr - depth_arr.min()) / (depth_arr.max() - depth_arr.min())

    # Crear versión desplazada
    shifted = np.zeros_like(img)
    for y in range(img.shape[0]):
        for x in range(img.shape[1]):
            offset = int(shift * depth_norm[y, x])
            new_x = min(x + offset, img.shape[1] - 1)
            shifted[y, new_x] = img[y, x]

    return Image.fromarray(shifted)
```

### Desenfoque de fondo (Modo retrato)

```python
def portrait_mode(image, depth, blur_strength=25):
    import cv2
    import numpy as np

    img = np.array(image)
    depth_arr = np.array(depth)

    # Normalizar profundidad
    depth_norm = (depth_arr - depth_arr.min()) / (depth_arr.max() - depth_arr.min())

    # Crear máscara de desenfoque (fondo = gran profundidad = más desenfoque)
    blur_mask = depth_norm

    # Aplicar desenfoque
    blurred = cv2.GaussianBlur(img, (blur_strength, blur_strength), 0)

    # Mezclar según la profundidad
    mask_3d = np.stack([blur_mask] * 3, axis=-1)
    result = (img * (1 - mask_3d) + blurred * mask_3d).astype(np.uint8)

    return Image.fromarray(result)
```

## Rendimiento

| Modelo   | GPU      | Tiempo por imagen |
| -------- | -------- | ----------------- |
| Pequeña  | RTX 3060 | \~50ms            |
| Base     | RTX 3060 | \~100ms           |
| Grande   | RTX 3090 | \~150ms           |
| Grande   | RTX 4090 | \~80ms            |
| V2-Large | RTX 4090 | \~100ms           |

## Solución de problemas

### Mala calidad de profundidad

* Usar variante de modelo más grande
* Asegurar buena calidad de la imagen
* Comprobar superficies reflectantes

### Problemas de memoria

* Usar variante de modelo más pequeña
* Reducir la resolución de la imagen
* Habilitar inferencia fp16

### Procesamiento lento

* Usar un modelo más pequeño
* Procesar en lote si es posible
* Usar inferencia en GPU

## Estimación de costos

Tarifas típicas del marketplace de CLORE.AI (a fecha de 2024):

| GPU       | Tarifa por hora | Tarifa diaria | Sesión de 4 horas |
| --------- | --------------- | ------------- | ----------------- |
| RTX 3060  | \~$0.03         | \~$0.70       | \~$0.12           |
| RTX 3090  | \~$0.06         | \~$1.50       | \~$0.25           |
| RTX 4090  | \~$0.10         | \~$2.30       | \~$0.40           |
| A100 40GB | \~$0.17         | \~$4.00       | \~$0.70           |
| A100 80GB | \~$0.25         | \~$6.00       | \~$1.00           |

*Los precios varían según el proveedor y la demanda. Consulta* [*CLORE.AI Marketplace*](https://clore.ai/marketplace) *para las tarifas actuales.*

**Ahorra dinero:**

* Usa **Spot** market para cargas de trabajo flexibles (a menudo 30-50% más barato)
* Paga con **CLORE** tokens
* Compara precios entre diferentes proveedores

## Próximos pasos

* [ControlNet](/guides/guides_v2-es/procesamiento-de-imagenes/controlnet-advanced.md) - Usar la profundidad para control
* [Segmentar cualquier cosa](/guides/guides_v2-es/procesamiento-de-imagenes/segment-anything.md) - Segmentación de objetos
* [Generación 3D](/guides/guides_v2-es/generacion-3d/triposr.md) - Profundidad de vídeo


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-es/procesamiento-de-imagenes/depth-anything.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
