Spaces:
Sleeping
Sleeping
| """ | |
| Demo depth generator for testing without ONNX models | |
| Creates synthetic depth maps for demonstration | |
| """ | |
| import numpy as np | |
| import cv2 | |
| def generate_demo_depth(image: np.ndarray, method: str = "gradient") -> np.ndarray: | |
| """ | |
| Generate a synthetic depth map for demo purposes | |
| Args: | |
| image: Input RGB image | |
| method: Method to use ('gradient', 'center', 'edges') | |
| Returns: | |
| Synthetic depth map (0-1 range) | |
| """ | |
| h, w = image.shape[:2] | |
| if method == "gradient": | |
| # Simple vertical gradient (top is far, bottom is near) | |
| depth = np.linspace(0, 1, h) | |
| depth = np.tile(depth[:, np.newaxis], (1, w)) | |
| elif method == "center": | |
| # Radial gradient from center | |
| y, x = np.ogrid[:h, :w] | |
| cy, cx = h // 2, w // 2 | |
| distance = np.sqrt((x - cx)**2 + (y - cy)**2) | |
| depth = distance / distance.max() | |
| depth = 1 - depth # Invert so center is near | |
| elif method == "edges": | |
| # Use edge detection as depth approximation | |
| gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
| edges = cv2.Canny(gray, 50, 150) | |
| edges = edges.astype(float) / 255.0 | |
| # Blur edges to create depth-like effect | |
| depth = cv2.GaussianBlur(edges, (21, 21), 0) | |
| depth = 1 - depth # Invert | |
| else: | |
| # Random depth for testing | |
| depth = np.random.rand(h, w) | |
| # Normalize to 0-1 range | |
| depth = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8) | |
| return depth.astype(np.float32) | |
| def generate_smart_depth(image: np.ndarray) -> np.ndarray: | |
| """ | |
| Generate a smarter synthetic depth using image analysis | |
| Better than simple gradients but still demo quality | |
| """ | |
| h, w = image.shape[:2] | |
| # Convert to grayscale | |
| gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) | |
| # Use intensity as rough depth proxy (darker = farther) | |
| depth_from_intensity = 1 - (gray.astype(float) / 255.0) | |
| # Get edge information | |
| edges = cv2.Canny(gray, 50, 150).astype(float) / 255.0 | |
| edges_blur = cv2.GaussianBlur(edges, (15, 15), 0) | |
| # Combine intensity and edge info | |
| depth = 0.6 * depth_from_intensity + 0.4 * (1 - edges_blur) | |
| # Apply smoothing | |
| depth = cv2.GaussianBlur(depth, (31, 31), 0) | |
| # Add some central bias (center tends to be closer) | |
| y, x = np.ogrid[:h, :w] | |
| cy, cx = h // 2, w // 2 | |
| distance = np.sqrt((x - cx)**2 + (y - cy)**2) | |
| radial = distance / distance.max() | |
| radial = 1 - radial | |
| depth = 0.7 * depth + 0.3 * radial | |
| # Normalize | |
| depth = (depth - depth.min()) / (depth.max() - depth.min() + 1e-8) | |
| return depth.astype(np.float32) | |