File size: 1,620 Bytes
c794c9c
 
 
b16b8cb
8aac058
7b30620
98550e9
c794c9c
 
 
 
 
 
 
 
 
b16b8cb
c794c9c
 
7b30620
c794c9c
 
 
7b30620
c794c9c
 
7b30620
c794c9c
 
 
7b30620
c794c9c
 
 
 
 
 
7b30620
c794c9c
 
 
b16b8cb
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import diffusers
import torch
from fastapi import FastAPI, UploadFile, HTTPException
from PIL import Image

app = FastAPI()

# Inicializa el pipeline al arrancar el servidor
@app.on_event("startup")
async def startup_event():
    global pipe
    print("[DEBUG] Cargando modelo Marigold...")
    pipe = diffusers.MarigoldDepthPipeline.from_pretrained(
        "prs-eth/marigold-depth-lcm-v1-0", variant="fp16", torch_dtype=torch.float16
    ).to("cuda")
    print("[DEBUG] Modelo Marigold cargado exitosamente.")

@app.post("/predict-depth/")
async def predict_depth(file: UploadFile):
    try:
        # Verifica si el archivo es una imagen v谩lida
        if not file.content_type.startswith("image/"):
            raise HTTPException(status_code=400, detail="El archivo subido no es una imagen.")

        # Carga la imagen desde el archivo subido
        image = Image.open(file.file).convert("RGB")

        # Realiza la predicci贸n de profundidad
        print("[DEBUG] Realizando predicci贸n de profundidad...")
        depth = pipe(image)

        # Visualiza la profundidad
        vis = pipe.image_processor.visualize_depth(depth.prediction)
        output_path = "predicted_depth.png"
        vis[0].save(output_path)

        return {"message": "Predicci贸n completada", "output_file": output_path}
    except Exception as e:
        print(f"[ERROR] {str(e)}")
        raise HTTPException(status_code=500, detail="Error procesando la imagen.")

    
@app.get("/")
async def root():
    return {"message": "API de generaci贸n de mapas de profundidad con Marigold"}