Spaces:
Sleeping
Sleeping
File size: 11,008 Bytes
463afdd 4473137 463afdd 191a797 463afdd 191a797 463afdd 191a797 463afdd 4473137 463afdd 191a797 4473137 463afdd 191a797 463afdd 4473137 463afdd 191a797 463afdd 4473137 463afdd 4473137 463afdd 4473137 191a797 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 463afdd 4473137 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
"""
DimensioDepth - Add Dimension to Everything
Advanced AI Depth Estimation with 3D Visualization
Powered by Depth-Anything V2 | Runs on Hugging Face Spaces
"""
import gradio as gr
import numpy as np
import cv2
from PIL import Image
from pathlib import Path
import sys
# Add backend to path
sys.path.append(str(Path(__file__).parent / "backend"))
# Import backend utilities
from backend.utils.image_processing import (
depth_to_colormap,
create_side_by_side
)
# Try to import REAL AI model
try:
from backend.utils.transformers_depth import TransformersDepthEstimator
print("[*] Loading REAL AI Depth-Anything V2 model...")
depth_estimator = TransformersDepthEstimator(model_size="small")
print("[+] REAL AI MODE ACTIVE!")
USE_REAL_AI = True
except Exception as e:
print(f"[!] Could not load AI models: {e}")
print("[*] Falling back to DEMO MODE")
from backend.utils.demo_depth import generate_smart_depth
USE_REAL_AI = False
def estimate_depth(image, quality_mode="Fast (Preview)", colormap_style="Inferno"):
"""
Estimate depth from an input image using REAL AI or DEMO MODE
"""
try:
# Convert PIL to numpy if needed
if isinstance(image, Image.Image):
image = np.array(image)
# Generate depth map
if USE_REAL_AI:
depth = depth_estimator.predict(image)
mode_text = "REAL AI (Depth-Anything V2)"
else:
depth = generate_smart_depth(image)
mode_text = "DEMO MODE (Synthetic)"
# Convert colormap style to cv2 constant
colormap_dict = {
"Inferno": cv2.COLORMAP_INFERNO,
"Viridis": cv2.COLORMAP_VIRIDIS,
"Plasma": cv2.COLORMAP_PLASMA,
"Turbo": cv2.COLORMAP_TURBO,
"Magma": cv2.COLORMAP_MAGMA,
"Hot": cv2.COLORMAP_HOT,
"Ocean": cv2.COLORMAP_OCEAN,
"Rainbow": cv2.COLORMAP_RAINBOW
}
# Create colored depth map
depth_colored = depth_to_colormap(depth, colormap_dict[colormap_style])
# Create grayscale depth map
depth_gray = (depth * 255).astype(np.uint8)
depth_gray = cv2.cvtColor(depth_gray, cv2.COLOR_GRAY2RGB)
# Processing info
info = f"""
### β
Depth Estimation Complete!
**Mode**: {mode_text}
**Input Size**: {image.shape[1]}x{image.shape[0]}
**Output Size**: {depth.shape[1]}x{depth.shape[0]}
**Colormap**: {colormap_style}
{f"**Powered by**: Depth-Anything V2 SMALL (97MB)" if USE_REAL_AI else "**Processing**: Ultra-fast (<50ms) synthetic depth"}
"""
return depth_colored, depth_gray, info
except Exception as e:
error_msg = f"### β Error\n\n{str(e)}"
print(f"Error during depth estimation: {e}")
return None, None, error_msg
def create_side_by_side_comparison(image, quality_mode="Fast (Preview)", colormap_style="Inferno"):
"""Create side-by-side comparison of original and depth map"""
try:
if isinstance(image, Image.Image):
image = np.array(image)
# Get depth estimation
if USE_REAL_AI:
depth = depth_estimator.predict(image)
else:
depth = generate_smart_depth(image)
# Convert colormap
colormap_dict = {
"Inferno": cv2.COLORMAP_INFERNO,
"Viridis": cv2.COLORMAP_VIRIDIS,
"Plasma": cv2.COLORMAP_PLASMA,
"Turbo": cv2.COLORMAP_TURBO,
"Magma": cv2.COLORMAP_MAGMA,
"Hot": cv2.COLORMAP_HOT,
"Ocean": cv2.COLORMAP_OCEAN,
"Rainbow": cv2.COLORMAP_RAINBOW
}
# Create side-by-side
comparison = create_side_by_side(image, depth, colormap=colormap_dict[colormap_style])
return comparison
except Exception as e:
print(f"Error creating comparison: {e}")
return None
def create_3d_visualization(image, depth_map, parallax_strength=0.5):
"""Create a simple 3D displacement visualization"""
try:
if image is None:
return None
if isinstance(image, Image.Image):
image = np.array(image)
if depth_map is None:
# Generate depth if not provided
if USE_REAL_AI:
depth_map = depth_estimator.predict(image)
else:
depth_map = generate_smart_depth(image)
depth_map = (depth_map * 255).astype(np.uint8)
elif isinstance(depth_map, Image.Image):
depth_map = np.array(depth_map)
# Convert depth to grayscale if colored
if len(depth_map.shape) == 3:
depth_map = cv2.cvtColor(depth_map, cv2.COLOR_RGB2GRAY)
# Normalize depth
depth_norm = depth_map.astype(float) / 255.0
# Create parallax effect (simple x-shift based on depth)
h, w = image.shape[:2]
result = image.copy()
# Apply horizontal shift based on depth
shift_amount = int(w * parallax_strength * 0.05)
for y in range(h):
for x in range(w):
depth_val = depth_norm[y, x]
shift = int(shift_amount * depth_val)
new_x = min(max(x + shift, 0), w - 1)
result[y, new_x] = image[y, x]
return result
except Exception as e:
print(f"Error creating 3D viz: {e}")
return image if image is not None else None
# Create Gradio interface
with gr.Blocks(
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple"),
title="DimensioDepth - Add Dimension to Everything"
) as demo:
gr.Markdown("""
# π¨ DimensioDepth - Add Dimension to Everything
### Transform 2D images into stunning 3D depth visualizations
**Running in DEMO MODE** - Ultra-fast synthetic depth estimation (no AI models needed!)
---
""")
with gr.Tabs():
# Tab 1: Main Depth Estimation
with gr.Tab("π― Depth Estimation"):
with gr.Row():
with gr.Column(scale=1):
input_image = gr.Image(
label="Upload Your Image",
type="pil",
height=400
)
colormap_style = gr.Dropdown(
choices=["Inferno", "Viridis", "Plasma", "Turbo", "Magma", "Hot", "Ocean", "Rainbow"],
value="Inferno",
label="Colormap Style",
info="Choose your depth visualization color scheme"
)
estimate_btn = gr.Button("π Generate Depth Map", variant="primary", size="lg")
with gr.Column(scale=1):
depth_colored = gr.Image(label="Depth Map (Colored)", height=400)
depth_gray = gr.Image(label="Depth Map (Grayscale)", height=400)
processing_info = gr.Markdown()
estimate_btn.click(
fn=estimate_depth,
inputs=[input_image, gr.State("Fast"), colormap_style],
outputs=[depth_colored, depth_gray, processing_info]
)
# Tab 2: Side-by-Side Comparison
with gr.Tab("π Side-by-Side Comparison"):
gr.Markdown("""
### Compare Original Image with Depth Map
Perfect for analyzing depth estimation quality and understanding 3D structure.
""")
with gr.Row():
with gr.Column(scale=1):
compare_input = gr.Image(label="Upload Image", type="pil", height=400)
compare_colormap = gr.Dropdown(
choices=["Inferno", "Viridis", "Plasma", "Turbo", "Magma", "Hot", "Ocean", "Rainbow"],
value="Turbo",
label="Colormap"
)
compare_btn = gr.Button("π¬ Create Comparison", variant="primary")
with gr.Column(scale=1):
comparison_output = gr.Image(label="Side-by-Side Comparison", height=500)
compare_btn.click(
fn=create_side_by_side_comparison,
inputs=[compare_input, gr.State("Fast"), compare_colormap],
outputs=comparison_output
)
# Tab 3: 3D Parallax Effect
with gr.Tab("π 3D Parallax Effect"):
gr.Markdown("""
### Create 3D Depth Displacement Effect
Generate a parallax effect to visualize the 3D structure of your image.
""")
with gr.Row():
with gr.Column(scale=1):
parallax_input = gr.Image(label="Original Image", type="pil")
parallax_depth = gr.Image(label="Depth Map (optional)", type="pil")
parallax_strength = gr.Slider(
minimum=0, maximum=2, value=0.5, step=0.1,
label="Parallax Strength",
info="Control the 3D displacement effect intensity"
)
parallax_btn = gr.Button("β¨ Generate 3D Effect", variant="primary")
with gr.Column(scale=1):
parallax_output = gr.Image(label="3D Parallax Result", height=500)
parallax_btn.click(
fn=create_3d_visualization,
inputs=[parallax_input, parallax_depth, parallax_strength],
outputs=parallax_output
)
# Info section
gr.Markdown("---")
gr.Markdown("""
## π‘ About This Demo
### π¨ Demo Mode Features:
- β
**Ultra-fast processing** (<50ms per image)
- β
**No model downloads** required
- β
**Advanced edge detection** + intensity analysis
- β
**Surprisingly good quality** for most use cases
- β
**Perfect for testing** and prototyping
### π How It Works:
Demo Mode uses sophisticated computer vision techniques:
1. **Edge Detection** - Find object boundaries
2. **Intensity Analysis** - Analyze brightness patterns
3. **Gaussian Smoothing** - Create smooth depth transitions
4. **Normalization** - Convert to depth values
### π‘ Tips for Best Results:
- **Image Quality**: Higher resolution = better depth detail
- **Lighting**: Well-lit images produce clearer depth maps
- **Contrast**: Good contrast shows better depth separation
- **Colormap**: Inferno for general use, Viridis for scientific viz
---
### π Use Cases
- π¨ **Creative & Artistic**: Depth-enhanced photos, 3D effects
- π¬ **VFX & Film**: Depth map generation for compositing
- π¬ **Research**: Computer vision, depth perception studies
- π± **Content Creation**: Engaging 3D effects for social media
---
**Tech Stack**: Advanced CV Algorithms, OpenCV, NumPy, Gradio
Made with β€οΈ for the AI community
""")
# Launch the app
if __name__ == "__main__":
demo.launch()
|