File size: 2,655 Bytes
463afdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
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)