This commit is contained in:
2026-05-14 17:30:20 +08:00
parent e43171521d
commit 5e6d8046e1
10 changed files with 224 additions and 77 deletions
+2 -1
View File
@@ -24,9 +24,10 @@ def depth(req: DepthRequest):
"""
try:
pil = b64_to_pil_image(req.image_b64).convert("RGB")
w, h = pil.size
predictor = get_depth_predictor()
depth_arr = predictor(pil) # type: ignore[misc]
png_bytes = depth_to_png16_bytes(np.asarray(depth_arr))
png_bytes = depth_to_png16_bytes(np.asarray(depth_arr), target_hw=(h, w))
return Response(content=png_bytes, media_type="image/png")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
+10 -2
View File
@@ -6,8 +6,16 @@ import numpy as np
from PIL import Image
def depth_to_png16_bytes(depth: np.ndarray) -> bytes:
depth = np.asarray(depth, dtype=np.float32)
def depth_to_png16_bytes(depth: np.ndarray, target_hw: tuple[int, int] | None = None) -> bytes:
depth = np.asarray(depth, dtype=np.float32).squeeze()
if depth.ndim != 2:
raise ValueError(f"depth 应为二维数组,实际 shape={depth.shape}")
if target_hw is not None:
th, tw = int(target_hw[0]), int(target_hw[1])
if th > 0 and tw > 0 and (depth.shape[0] != th or depth.shape[1] != tw):
im = Image.fromarray(depth, mode="F")
im = im.resize((tw, th), Image.Resampling.BILINEAR)
depth = np.asarray(im, dtype=np.float32)
dmin = float(depth.min())
dmax = float(depth.max())
if dmax > dmin: