Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files31 unmodified lines32333435363738394044 unmodified lines858687888990919293945 unmodified lines10010110210310410510610710810911011111211311411511611711811912031 unmodified linesdef run_models(img_pil):s_pipe, d_pipe = get_models()seg_results = s_pipe(img_pil)depth_out = d_pipe(img_pil)depth_img = depth_out["depth"] if isinstance(depth_out, dict) else depth_outreturn seg_results, depth_imgdef fit_structural_lines(edge_map, min_len=60):44 unmodified linesif arr.ndim == 3:arr = arr[..., 0]if arr.shape != (h, w):arr = cv2.resize(arr.astype(np.float32), (w, h), interpolation=cv2.INTER_NEAREST)return (arr > 0).astype(np.uint8) * 255def analyze_room(input_img):if input_img is None:return None, None, "Please upload an interior photo."5 unmodified lineswork_np = np.array(img_pil)h, w, _ = work_np.shapeseg_results, depth_img = run_models(img_pil)masks = {"wall": np.zeros((h, w), np.uint8),"ceiling": np.zeros((h, w), np.uint8),"floor": np.zeros((h, w), np.uint8),}for item in seg_results or []:lbl = str(item.get("label", "")).lower()m = to_mask_2d(item.get("mask"), w, h)if m is None:continuefor k in masks:if k in lbl:masks[k] = cv2.bitwise_or(masks[k], m)if hasattr(depth_img, "resize"):depth_resized = depth_img.convert("F").resize((w, h))31 unmodified lines323334353637383940414244 unmodified lines878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381395 unmodified lines14514614714814915015115231 unmodified linesdef run_models(img_pil):s_pipe, d_pipe = get_models()seg_results = s_pipe(img_pil)id2label = getattr(getattr(s_pipe, "model", None), "config", None)id2label = getattr(id2label, "id2label", {}) or {}depth_out = d_pipe(img_pil)depth_img = depth_out["depth"] if isinstance(depth_out, dict) else depth_outreturn seg_results, id2label, depth_imgdef fit_structural_lines(edge_map, min_len=60):44 unmodified linesif arr.ndim == 3:arr = arr[..., 0]if arr.shape != (h, w):arr = cv2.resize(arr.astype(np.float32), (w, h), interpolation=cv2.INTER_NEAREST)return (arr > 0).astype(np.uint8) * 255def extract_structural_masks(seg_results, id2label, w, h):masks = {"wall": np.zeros((h, w), np.uint8),"ceiling": np.zeros((h, w), np.uint8),"floor": np.zeros((h, w), np.uint8),}if seg_results is None:return masks# Case 1: List/iterable of dicts with 'label' and 'mask'if isinstance(seg_results, (list, tuple)):for item in seg_results:if not isinstance(item, dict):continuelbl = str(item.get("label", "")).lower()m = to_mask_2d(item.get("mask"), w, h)if m is None:continuefor k in masks:if k in lbl:masks[k] = cv2.bitwise_or(masks[k], m)return masks# Case 2: Array or tensor of class IDsarr = seg_results.detach().cpu().numpy() if hasattr(seg_results, "detach") else np.asarray(seg_results)arr = np.squeeze(arr)if arr.ndim == 3:arr = arr[..., 0]if arr.ndim == 2:if arr.shape != (h, w):arr = cv2.resize(arr.astype(np.float32), (w, h), interpolation=cv2.INTER_NEAREST).astype(np.int64)for cls_id in np.unique(arr):lbl = str(id2label.get(int(cls_id), id2label.get(str(int(cls_id)), ""))).lower()m = (arr == cls_id).astype(np.uint8) * 255for k in masks:if k in lbl:masks[k] = cv2.bitwise_or(masks[k], m)return masksdef analyze_room(input_img):if input_img is None:return None, None, "Please upload an interior photo."5 unmodified lineswork_np = np.array(img_pil)h, w, _ = work_np.shapeseg_results, id2label, depth_img = run_models(img_pil)masks = extract_structural_masks(seg_results, id2label, w, h)if hasattr(depth_img, "resize"):depth_resized = depth_img.convert("F").resize((w, h))
app.py+49-17