Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import open3d as o3d
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
class CPUFriendlyImageTo3DConverter:
|
9 |
+
def __init__(self):
|
10 |
+
# Use lighter depth estimation model
|
11 |
+
self.depth_estimator = pipeline(
|
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 a lightweight model
|
19 |
+
"""
|
20 |
+
depth_result = self.depth_estimator(image)
|
21 |
+
depth_map = np.array(depth_result['depth'])
|
22 |
+
return depth_map
|
23 |
+
|
24 |
+
def create_point_cloud(self, image, depth_map):
|
25 |
+
"""
|
26 |
+
Convert depth map to 3D point cloud with reduced resolution
|
27 |
+
"""
|
28 |
+
img_array = np.array(image)
|
29 |
+
height, width = img_array.shape[:2]
|
30 |
+
|
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 |
+
|
44 |
+
def convert_to_mesh(self, point_cloud):
|
45 |
+
"""
|
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):
|
58 |
+
"""
|
59 |
+
CPU-friendly full pipeline for 3D conversion
|
60 |
+
"""
|
61 |
+
# Estimate depth
|
62 |
+
depth_map = self.estimate_depth(input_image)
|
63 |
+
|
64 |
+
# Create point cloud
|
65 |
+
point_cloud = self.create_point_cloud(input_image, depth_map)
|
66 |
+
|
67 |
+
# Convert to mesh
|
68 |
+
mesh = self.convert_to_mesh(point_cloud)
|
69 |
+
|
70 |
+
# Save mesh
|
71 |
+
output_path = "/tmp/converted_3d_model.obj"
|
72 |
+
o3d.io.write_triangle_mesh(output_path, mesh)
|
73 |
+
|
74 |
+
return output_path
|
75 |
+
|
76 |
+
def create_huggingface_space():
|
77 |
+
converter = CPUFriendlyImageTo3DConverter()
|
78 |
+
|
79 |
+
def convert_image(input_image):
|
80 |
+
try:
|
81 |
+
# Ensure image is in PIL format
|
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
|
88 |
+
except Exception as e:
|
89 |
+
return f"Error during conversion: {str(e)}"
|
90 |
+
|
91 |
+
# Gradio Interface
|
92 |
+
iface = gr.Interface(
|
93 |
+
fn=convert_image,
|
94 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
95 |
+
outputs=[
|
96 |
+
gr.File(label="3D Model (OBJ)"),
|
97 |
+
gr.Textbox(label="Conversion Status")
|
98 |
+
],
|
99 |
+
title="CPU-Friendly Image to 3D Model Converter",
|
100 |
+
description="Convert images to 3D models using lightweight depth estimation and point cloud reconstruction."
|
101 |
+
)
|
102 |
+
|
103 |
+
return iface
|
104 |
+
|
105 |
+
# Launch the Gradio interface
|
106 |
+
demo = create_huggingface_space()
|
107 |
+
demo.launch()
|