Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,22 +3,30 @@ import gradio as gr
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
import open3d as o3d
|
6 |
-
|
7 |
|
8 |
-
class
|
9 |
def __init__(self):
|
10 |
-
# Use
|
11 |
-
self.
|
12 |
-
"depth-estimation",
|
13 |
-
model="facebook/dpt-small" # Lighter model for CPU
|
14 |
-
)
|
15 |
|
16 |
def estimate_depth(self, image):
|
17 |
"""
|
18 |
-
Estimate depth using
|
19 |
"""
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
return depth_map
|
23 |
|
24 |
def create_point_cloud(self, image, depth_map):
|
@@ -31,13 +39,17 @@ class CPUFriendlyImageTo3DConverter:
|
|
31 |
# Downsample to reduce computational load
|
32 |
step = 5 # Reduce resolution
|
33 |
points = []
|
|
|
|
|
34 |
for y in range(0, height, step):
|
35 |
for x in range(0, width, step):
|
36 |
-
z = depth_map[y, x]
|
37 |
-
points.append([x, y, z])
|
|
|
38 |
|
39 |
pcd = o3d.geometry.PointCloud()
|
40 |
pcd.points = o3d.utility.Vector3dVector(points)
|
|
|
41 |
|
42 |
return pcd
|
43 |
|
@@ -46,12 +58,16 @@ class CPUFriendlyImageTo3DConverter:
|
|
46 |
Simplified mesh reconstruction for CPU
|
47 |
"""
|
48 |
point_cloud.estimate_normals()
|
|
|
49 |
|
50 |
# Use simpler mesh reconstruction with lower depth
|
51 |
mesh, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
52 |
point_cloud, depth=6 # Reduced depth for faster processing
|
53 |
)
|
54 |
|
|
|
|
|
|
|
55 |
return mesh
|
56 |
|
57 |
def process_image(self, input_image):
|
@@ -74,7 +90,7 @@ class CPUFriendlyImageTo3DConverter:
|
|
74 |
return output_path
|
75 |
|
76 |
def create_huggingface_space():
|
77 |
-
converter =
|
78 |
|
79 |
def convert_image(input_image):
|
80 |
try:
|
@@ -82,6 +98,10 @@ def create_huggingface_space():
|
|
82 |
if not isinstance(input_image, Image.Image):
|
83 |
input_image = Image.fromarray(input_image)
|
84 |
|
|
|
|
|
|
|
|
|
85 |
# Process image
|
86 |
output_model = converter.process_image(input_image)
|
87 |
return output_model
|
@@ -96,8 +116,8 @@ def create_huggingface_space():
|
|
96 |
gr.File(label="3D Model (OBJ)"),
|
97 |
gr.Textbox(label="Conversion Status")
|
98 |
],
|
99 |
-
title="
|
100 |
-
description="Convert images to 3D models using
|
101 |
)
|
102 |
|
103 |
return iface
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
import open3d as o3d
|
6 |
+
import cv2
|
7 |
|
8 |
+
class RobustImageTo3DConverter:
|
9 |
def __init__(self):
|
10 |
+
# Use OpenCV for depth estimation
|
11 |
+
self.use_midas = False
|
|
|
|
|
|
|
12 |
|
13 |
def estimate_depth(self, image):
|
14 |
"""
|
15 |
+
Estimate depth using OpenCV's stereo algorithms
|
16 |
"""
|
17 |
+
# Convert PIL Image to OpenCV format
|
18 |
+
img_array = np.array(image)
|
19 |
+
|
20 |
+
# Convert to grayscale
|
21 |
+
gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
22 |
+
|
23 |
+
# Create simple depth estimation using edge detection and blurring
|
24 |
+
edges = cv2.Canny(gray, 100, 200)
|
25 |
+
depth_map = cv2.distanceTransform(255 - edges, cv2.DIST_L2, 5)
|
26 |
+
|
27 |
+
# Normalize depth map
|
28 |
+
depth_map = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX)
|
29 |
+
|
30 |
return depth_map
|
31 |
|
32 |
def create_point_cloud(self, image, depth_map):
|
|
|
39 |
# Downsample to reduce computational load
|
40 |
step = 5 # Reduce resolution
|
41 |
points = []
|
42 |
+
colors = []
|
43 |
+
|
44 |
for y in range(0, height, step):
|
45 |
for x in range(0, width, step):
|
46 |
+
z = depth_map[y, x] / 255.0 # Normalize depth
|
47 |
+
points.append([x, y, z * 10]) # Scale depth
|
48 |
+
colors.append(img_array[y, x] / 255.0) # Normalize colors
|
49 |
|
50 |
pcd = o3d.geometry.PointCloud()
|
51 |
pcd.points = o3d.utility.Vector3dVector(points)
|
52 |
+
pcd.colors = o3d.utility.Vector3dVector(colors)
|
53 |
|
54 |
return pcd
|
55 |
|
|
|
58 |
Simplified mesh reconstruction for CPU
|
59 |
"""
|
60 |
point_cloud.estimate_normals()
|
61 |
+
point_cloud.orient_normals_consistent_tangent_plane(100)
|
62 |
|
63 |
# Use simpler mesh reconstruction with lower depth
|
64 |
mesh, _ = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
65 |
point_cloud, depth=6 # Reduced depth for faster processing
|
66 |
)
|
67 |
|
68 |
+
# Color the mesh
|
69 |
+
mesh.paint_uniform_color([0.7, 0.7, 0.7])
|
70 |
+
|
71 |
return mesh
|
72 |
|
73 |
def process_image(self, input_image):
|
|
|
90 |
return output_path
|
91 |
|
92 |
def create_huggingface_space():
|
93 |
+
converter = RobustImageTo3DConverter()
|
94 |
|
95 |
def convert_image(input_image):
|
96 |
try:
|
|
|
98 |
if not isinstance(input_image, Image.Image):
|
99 |
input_image = Image.fromarray(input_image)
|
100 |
|
101 |
+
# Resize image if too large
|
102 |
+
max_size = (800, 800)
|
103 |
+
input_image.thumbnail(max_size, Image.LANCZOS)
|
104 |
+
|
105 |
# Process image
|
106 |
output_model = converter.process_image(input_image)
|
107 |
return output_model
|
|
|
116 |
gr.File(label="3D Model (OBJ)"),
|
117 |
gr.Textbox(label="Conversion Status")
|
118 |
],
|
119 |
+
title="Robust Image to 3D Model Converter",
|
120 |
+
description="Convert images to 3D models using OpenCV depth estimation and point cloud reconstruction."
|
121 |
)
|
122 |
|
123 |
return iface
|