Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files12345678910111213141516171819202122239 unmodified lines3334353637383911 unmodified lines51525354555657586 unmodified lines6566676869707172731 unmodified line757677787980813 unmodified lines85868788899091929394959697989910010110 unmodified lines1121131141151161171182 unmodified lines12112212312412512612720 unmodified lines148149150151152153154155156import cv2import gradio as grimport numpy as npimport torchfrom PIL import Imagefrom transformers import pipelinedevice = 0 if torch.cuda.is_available() else -1# Load free open-weights modelsseg_pipe = pipeline("image-segmentation",model="nvidia/segformer-b2-finetuned-ade-512-512",device=device,)depth_pipe = pipeline("depth-estimation",model="depth-anything/Depth-Anything-V2-Small-hf",device=device,)def fit_structural_lines(edge_map, min_len=60):9 unmodified linesdef line_intersection(l1, l2):# Homogeneous cross product: p = l1 x l2x1, y1, x2, y2 = l1x3, y3, x4, y4 = l2p1 = np.cross([x1, y1, 1.0], [x2, y2, 1.0])11 unmodified linesimg_pil = Image.fromarray(input_img).convert("RGB")h, w, _ = input_img.shape# 1. Semantic segmentation (Wall, Ceiling, Floor)seg_results = seg_pipe(img_pil)masks = {"wall": np.zeros((h, w), np.uint8),"ceiling": np.zeros((h, w), np.uint8),6 unmodified linesif k in lbl:masks[k] = cv2.bitwise_or(masks[k], m)# 2. Monocular depth & spatial gradient cuesdepth_raw = np.array(depth_pipe(img_pil)["depth"].resize((w, h)), dtype=np.float32)depth_norm = cv2.normalize(depth_raw, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint81 unmodified linedepth_colored = cv2.applyColorMap(depth_norm, cv2.COLORMAP_INFERNO)depth_colored = cv2.cvtColor(depth_colored, cv2.COLOR_BGR2RGB)# 3. Junction boundary masks (dilate & intersect adjacent planes)kernel = np.ones((9, 9), np.uint8)wall_d = cv2.dilate(masks["wall"], kernel)ceil_d = cv2.dilate(masks["ceiling"], kernel)3 unmodified lineswall_floor_edge = cv2.bitwise_and(wall_d, floor_d)depth_edges = cv2.Canny(cv2.GaussianBlur(depth_norm, (5, 5), 0), 35, 110)# Combine structural boundary candidatesboundary_candidates = cv2.bitwise_or(wall_ceil_edge, wall_floor_edge)boundary_candidates = cv2.bitwise_or(boundary_candidates, cv2.bitwise_and(depth_edges, wall_d))# 4. Fit straight lines & solve homogeneous intersections for cornerslines = fit_structural_lines(boundary_candidates, min_len=int(min(h, w) * 0.1))overlay = input_img.copy()# Tint masksoverlay[masks["wall"] > 0] = (0.72 * overlay[masks["wall"] > 0] + 0.28 * np.array([65, 135, 255])).astype(np.uint8)10 unmodified linesclean_lines.append((x1, y1, x2, y2))cv2.line(overlay, (x1, y1), (x2, y2), (0, 255, 255), 2)# Find valid corner intersections inside image boundscorners = []for i in range(len(clean_lines)):for j in range(i + 1, len(clean_lines)):2 unmodified linesif wall_d[pt[1], pt[0]] > 0:corners.append(pt)# Cluster nearby intersectionsmerged_corners = []for pt in corners:if all(np.hypot(pt[0] - c[0], pt[1] - c[1]) > 35 for c in merged_corners):20 unmodified linesgr.Image(label="Monocular Depth Map"),gr.Textbox(label="Geometric Breakdown", lines=5),],title="Interior Spatial & Corner Analyzer",)if __name__ == "__main__":demo.launch()1234567891011121314151617181920212223242526272829303132333435363738399 unmodified lines49505152535411 unmodified lines66676869707172736 unmodified lines80818283848586871 unmodified line8990919293943 unmodified lines989910010110210310410510610710810911011110 unmodified lines1221231241251261272 unmodified lines13013113213313413520 unmodified lines156157158159160161162163import cv2import gradio as grimport numpy as npimport spacesimport torchfrom PIL import Imagefrom transformers import pipelineseg_pipe = Nonedepth_pipe = Nonedef get_models():global seg_pipe, depth_pipedevice = 0 if torch.cuda.is_available() else -1if seg_pipe is None:seg_pipe = pipeline("image-segmentation",model="nvidia/segformer-b2-finetuned-ade-512-512",device=device,)if depth_pipe is None:depth_pipe = pipeline("depth-estimation",model="depth-anything/Depth-Anything-V2-Small-hf",device=device,)return seg_pipe, depth_pipe@spaces.GPU(duration=45)def run_models(img_pil):s_pipe, d_pipe = get_models()seg_results = s_pipe(img_pil)depth_img = d_pipe(img_pil)["depth"]return seg_results, depth_imgdef fit_structural_lines(edge_map, min_len=60):9 unmodified linesdef line_intersection(l1, l2):x1, y1, x2, y2 = l1x3, y3, x4, y4 = l2p1 = np.cross([x1, y1, 1.0], [x2, y2, 1.0])11 unmodified linesimg_pil = Image.fromarray(input_img).convert("RGB")h, w, _ = input_img.shapeseg_results, depth_img = run_models(img_pil)masks = {"wall": np.zeros((h, w), np.uint8),"ceiling": np.zeros((h, w), np.uint8),6 unmodified linesif k in lbl:masks[k] = cv2.bitwise_or(masks[k], m)depth_raw = np.array(depth_img.resize((w, h)), dtype=np.float32)depth_norm = cv2.normalize(depth_raw, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint81 unmodified linedepth_colored = cv2.applyColorMap(depth_norm, cv2.COLORMAP_INFERNO)depth_colored = cv2.cvtColor(depth_colored, cv2.COLOR_BGR2RGB)kernel = np.ones((9, 9), np.uint8)wall_d = cv2.dilate(masks["wall"], kernel)ceil_d = cv2.dilate(masks["ceiling"], kernel)3 unmodified lineswall_floor_edge = cv2.bitwise_and(wall_d, floor_d)depth_edges = cv2.Canny(cv2.GaussianBlur(depth_norm, (5, 5), 0), 35, 110)boundary_candidates = cv2.bitwise_or(wall_ceil_edge, wall_floor_edge)boundary_candidates = cv2.bitwise_or(boundary_candidates, cv2.bitwise_and(depth_edges, wall_d))lines = fit_structural_lines(boundary_candidates, min_len=int(min(h, w) * 0.1))overlay = input_img.copy()overlay[masks["wall"] > 0] = (0.72 * overlay[masks["wall"] > 0] + 0.28 * np.array([65, 135, 255])).astype(np.uint8)10 unmodified linesclean_lines.append((x1, y1, x2, y2))cv2.line(overlay, (x1, y1), (x2, y2), (0, 255, 255), 2)corners = []for i in range(len(clean_lines)):for j in range(i + 1, len(clean_lines)):2 unmodified linesif wall_d[pt[1], pt[0]] > 0:corners.append(pt)merged_corners = []for pt in corners:if all(np.hypot(pt[0] - c[0], pt[1] - c[1]) > 35 for c in merged_corners):20 unmodified linesgr.Image(label="Monocular Depth Map"),gr.Textbox(label="Geometric Breakdown", lines=5),],title="Smart Area — Interior Spatial & Corner Analyzer",)if __name__ == "__main__":demo.launch()
app.py+33-26