# Test ilosciowy: czy plaszczyzny wglebione (komora skrzyni) da sie odciac # glebia wzgledna wewnatrz maski mebla (matte BiRefNet + depth DAv2). import cv2 import numpy as np import torch from PIL import Image from transformers import AutoImageProcessor, AutoModelForDepthEstimation MODEL_ID = "depth-anything/Depth-Anything-V2-Small-hf" IMG = r"C:\xampp\htdocs\vilmax\vilmal\tmp\recolor-svc-storage-granat-v4.png" # 3024x4032 MATTE = r"C:\xampp\htdocs\vilmax\vilmal\tmp\birefnet-matte-6413.png" proc = AutoImageProcessor.from_pretrained(MODEL_ID) model = AutoModelForDepthEstimation.from_pretrained(MODEL_ID).to("cuda").eval() img = Image.open(IMG).convert("RGB") W, H = img.size inputs = proc(images=img, return_tensors="pt").to("cuda") with torch.no_grad(): d = model(**inputs).predicted_depth[0].cpu().numpy() # wieksze = blizej d = cv2.resize(d, (W, H), interpolation=cv2.INTER_LINEAR) d = (d - d.min()) / max(1e-6, d.max() - d.min()) matte = np.asarray(Image.open(MATTE).convert("L").resize((W, H), Image.BILINEAR), np.float32) / 255.0 mask = (matte > 0.5).astype(np.uint8) print("mask px:", int(mask.sum())) dm = d[mask > 0] med = float(np.median(dm)) p10, p25, p75 = (float(np.percentile(dm, q)) for q in (10, 25, 75)) print(f"depth w masce: p10={p10:.3f} med={med:.3f} p75={p75:.3f}") # kandydat: piksel maski znacznie glebiej niz mediana obrysu for margin in (0.08, 0.12, 0.16): rec = ((mask > 0) & (d < med - margin)).astype(np.uint8) n, lab, stats, _ = cv2.connectedComponentsWithStats(rec, 8) big = [(int(s[cv2.CC_STAT_AREA]), (int(s[cv2.CC_STAT_LEFT]), int(s[cv2.CC_STAT_TOP]), int(s[cv2.CC_STAT_WIDTH]), int(s[cv2.CC_STAT_HEIGHT]))) for s in stats[1:] if s[cv2.CC_STAT_AREA] > 5000] big.sort(reverse=True) print(f"margin={margin}: rec_px={int(rec.sum())} ({rec.sum()/max(1,mask.sum()):.1%} maski) komponenty>5k: {big[:5]}") # wizualizacja kandydata margin=0.12 rec = ((mask > 0) & (d < med - 0.12)).astype(np.uint8) out = np.asarray(img, np.uint8).copy() out[rec > 0] = (out[rec > 0] * 0.35 + np.array([255, 60, 60]) * 0.65).astype(np.uint8) Image.fromarray(out).resize((W // 3, H // 3)).save(r"C:\xampp\htdocs\vilmax\vilmal\tmp\depth-cavity-overlay.png") print("overlay zapisany")