Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files23 unmodified lines24252627272829303132333034353637383940418 unmodified lines50515245535455495657585953606162631 unmodified line65666761686970717273747576656677787980817082837273848586878823 unmodified lines11211311411511611711810410511912012112212312412512611111211311411512712812913013113213313413513613713813912114014114214316 unmodified lines1601611621441631641651666 unmodified lines17317417515715815916016117617717817918012 unmodified lines19319419519619719819923 unmodified lines device = "cuda" if torch.cuda.is_available() else "cpu" if seg_model is None: seg_processor = AutoImageProcessor.from_pretrained(SEG_MODEL_ID) seg_model = AutoModelForSemanticSegmentation.from_pretrained(SEG_MODEL_ID).to(device).eval() seg_model = ( AutoModelForSemanticSegmentation.from_pretrained(SEG_MODEL_ID) .to(device) .eval() ) if depth_model is None: depth_processor = AutoImageProcessor.from_pretrained(DEPTH_MODEL_ID) depth_model = AutoModelForDepthEstimation.from_pretrained(DEPTH_MODEL_ID).to(device).eval() depth_model = ( AutoModelForDepthEstimation.from_pretrained(DEPTH_MODEL_ID) .to(device) .eval() ) return seg_processor, seg_model, depth_processor, depth_model
8 unmodified lines with torch.inference_mode(): s_logits = s_mod(**s_inputs).logits pred_map = s_logits.argmax(dim=1).squeeze(0).detach().cpu().numpy()
d_pred = d_mod(**d_inputs).predicted_depth.squeeze(0).detach().cpu().numpy()
id2label = getattr(s_mod.config, "id2label", {}) or {} return pred_map, id2label, d_pred return np.asarray(pred_map), id2label, np.asarray(d_pred, dtype=np.float32)
def fit_structural_lines(edge_map, min_len=60): lines = cv2.HoughLinesP( raw = cv2.HoughLinesP( edge_map, rho=1, theta=np.pi / 180,1 unmodified line minLineLength=max(20, int(min_len)), maxLineGap=20, ) return lines if lines is not None else [] if raw is None: return [] arr = np.asarray(raw) if arr.size == 0 or arr.ndim < 3: return [] return arr.reshape(-1, 4).tolist()
def line_intersection(l1, l2): x1, y1, x2, y2 = [float(v) for v in l1] x3, y3, x4, y4 = [float(v) for v in l2] x1, y1, x2, y2 = float(l1[0]), float(l1[1]), float(l1[2]), float(l1[3]) x3, y3, x4, y4 = float(l2[0]), float(l2[1]), float(l2[2]), float(l2[3]) p1 = np.cross([x1, y1, 1.0], [x2, y2, 1.0]) p2 = np.cross([x3, y3, 1.0], [x4, y4, 1.0]) pt = np.cross(p1, p2) if not np.all(np.isfinite(pt)) or abs(pt[2]) < 1e-6: if not np.all(np.isfinite(pt)) or abs(float(pt[2])) < 1e-6: return None ix = pt[0] / pt[2] iy = pt[1] / pt[2] ix = float(pt[0]) / float(pt[2]) iy = float(pt[1]) / float(pt[2]) if not (np.isfinite(ix) and np.isfinite(iy)) or abs(ix) > 1e6 or abs(iy) > 1e6: return None return (int(round(ix)), int(round(iy)))23 unmodified lines
pred_map, id2label, d_pred = run_models(img_pil)
pred_2d = np.squeeze(pred_map) if pred_2d.ndim == 3: pred_2d = pred_2d[0] pred_resized = cv2.resize( pred_map.astype(np.int32), (w, h), interpolation=cv2.INTER_NEAREST ) pred_2d.astype(np.float32), (w, h), interpolation=cv2.INTER_NEAREST ).astype(np.int32)
masks = { "wall": np.zeros((h, w), np.uint8), "ceiling": np.zeros((h, w), np.uint8), "floor": np.zeros((h, w), np.uint8), } for cls_id in np.unique(pred_resized): lbl = str( id2label.get(int(cls_id), id2label.get(str(int(cls_id)), "")) ).lower() m = (pred_resized == cls_id).astype(np.uint8) * 255 unique_ids = np.unique(pred_resized).tolist() for cls_id in unique_ids: cid = int(cls_id) lbl = str(id2label.get(cid, id2label.get(str(cid), ""))).lower() m = (pred_resized == cid).astype(np.uint8) * 255 for k in masks: if k in lbl: masks[k] = cv2.bitwise_or(masks[k], m)
d_2d = np.squeeze(d_pred) if d_2d.ndim == 3: d_2d = d_2d[0] depth_raw = cv2.resize( np.asarray(d_pred, dtype=np.float32), (w, h), interpolation=cv2.INTER_CUBIC d_2d.astype(np.float32), (w, h), interpolation=cv2.INTER_CUBIC ) depth_raw = np.nan_to_num(depth_raw, nan=0.0, posinf=0.0, neginf=0.0) depth_norm = cv2.normalize(depth_raw, None, 0, 255, cv2.NORM_MINMAX).astype(16 unmodified lines boundary_candidates, cv2.bitwise_and(depth_edges, wall_d) )
lines = fit_structural_lines(boundary_candidates, min_len=min(h, w) * 0.1) clean_lines = fit_structural_lines(boundary_candidates, min_len=min(h, w) * 0.1) overlay = work_np.copy()
overlay[masks["wall"] > 0] = (6 unmodified lines 0.72 * overlay[masks["ceiling"] > 0] + 0.28 * np.array([255, 195, 70]) ).astype(np.uint8)
clean_lines = [] for l in lines: x1, y1, x2, y2 = [int(v) for v in l[0]] clean_lines.append((x1, y1, x2, y2)) cv2.line(overlay, (x1, y1), (x2, y2), (0, 255, 255), 2) for x1, y1, x2, y2 in clean_lines: cv2.line(overlay, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 255), 2)
corners = [] for i in range(len(clean_lines)):12 unmodified lines
total_px = h * w report = ( f"Pipeline v3 — Direct Logits\n" f"Wall Coverage: {np.count_nonzero(masks['wall']) / total_px * 100:.1f}%\n" f"Ceiling Coverage: {np.count_nonzero(masks['ceiling']) / total_px * 100:.1f}%\n" f"Floor Coverage: {np.count_nonzero(masks['floor']) / total_px * 100:.1f}%\n"app.py+42-25