Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import trimesh
|
5 |
+
import tempfile
|
6 |
+
import rembg
|
7 |
+
|
8 |
+
def remove_background(input_image):
|
9 |
+
input_image = Image.fromarray(input_image)
|
10 |
+
output = rembg.remove(input_image)
|
11 |
+
return np.array(output)
|
12 |
+
|
13 |
+
def create_3d_model(input_image, thickness=0.05, size=3):
|
14 |
+
# Convert numpy array to PIL Image
|
15 |
+
image = Image.fromarray(input_image)
|
16 |
+
|
17 |
+
# Get image dimensions
|
18 |
+
width, height = image.size
|
19 |
+
aspect_ratio = width / height
|
20 |
+
|
21 |
+
# Create front and back planes
|
22 |
+
vertices = np.array([
|
23 |
+
[-size, -size/aspect_ratio, thickness/2],
|
24 |
+
[size, -size/aspect_ratio, thickness/2],
|
25 |
+
[size, size/aspect_ratio, thickness/2],
|
26 |
+
[-size, size/aspect_ratio, thickness/2],
|
27 |
+
[-size, -size/aspect_ratio, -thickness/2],
|
28 |
+
[size, -size/aspect_ratio, -thickness/2],
|
29 |
+
[size, size/aspect_ratio, -thickness/2],
|
30 |
+
[-size, size/aspect_ratio, -thickness/2]
|
31 |
+
])
|
32 |
+
|
33 |
+
faces = np.array([
|
34 |
+
[0, 1, 2], [0, 2, 3], # front
|
35 |
+
[4, 6, 5], [4, 7, 6], # back
|
36 |
+
[0, 4, 1], [1, 4, 5], # bottom
|
37 |
+
[2, 6, 3], [3, 6, 7], # top
|
38 |
+
[0, 3, 4], [3, 7, 4], # left
|
39 |
+
[1, 5, 2], [2, 5, 6] # right
|
40 |
+
])
|
41 |
+
|
42 |
+
# Create texture coordinates (front and back are now identical)
|
43 |
+
uvs = np.array([
|
44 |
+
[0, 0], [1, 0], [1, 1], [0, 1], # front
|
45 |
+
[0, 0], [1, 0], [1, 1], [0, 1], # back (not flipped)
|
46 |
+
[0, 1], [1, 1], [1, 0], [0, 0], # other sides
|
47 |
+
[0, 1], [1, 1], [1, 0], [0, 0],
|
48 |
+
[0, 1], [1, 1], [1, 0], [0, 0],
|
49 |
+
[0, 1], [1, 1], [1, 0], [0, 0]
|
50 |
+
])
|
51 |
+
|
52 |
+
# Create the mesh with transparent material
|
53 |
+
mesh = trimesh.Trimesh(
|
54 |
+
vertices=vertices,
|
55 |
+
faces=faces,
|
56 |
+
visual=trimesh.visual.TextureVisuals(
|
57 |
+
uv=uvs,
|
58 |
+
material=trimesh.visual.material.PBRMaterial(
|
59 |
+
baseColorTexture=trimesh.visual.TextureVisuals(image=image).material.image,
|
60 |
+
alphaMode='BLEND',
|
61 |
+
doubleSided=True
|
62 |
+
)
|
63 |
+
)
|
64 |
+
)
|
65 |
+
|
66 |
+
# Create a scene with our textured mesh
|
67 |
+
scene = trimesh.Scene(mesh)
|
68 |
+
|
69 |
+
scene.ambient = [0.95, 0.95, 0.95]
|
70 |
+
# Export as GLB
|
71 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.glb') as temp_file:
|
72 |
+
scene.export(temp_file.name, file_type='glb')
|
73 |
+
|
74 |
+
return temp_file.name
|
75 |
+
|
76 |
+
def process_and_create_3d(input_image, thickness, size):
|
77 |
+
# Remove background
|
78 |
+
processed_image = remove_background(input_image)
|
79 |
+
|
80 |
+
# Create 3D model
|
81 |
+
model_path = create_3d_model(processed_image, thickness, size)
|
82 |
+
|
83 |
+
return processed_image, model_path
|
84 |
+
|
85 |
+
# Define Gradio interface
|
86 |
+
iface = gr.Interface(
|
87 |
+
fn=process_and_create_3d,
|
88 |
+
inputs=[
|
89 |
+
gr.Image(label="Input Image"),
|
90 |
+
gr.Slider(minimum=0.001, maximum=0.2, step=0.001, value=0.01, label="Thickness"),
|
91 |
+
gr.Slider(minimum=1, maximum=5, step=0.1, value=3, label="Size")
|
92 |
+
],
|
93 |
+
outputs=[
|
94 |
+
gr.Image(label="Processed Image (Background Removed)"),
|
95 |
+
gr.Model3D(label="3D Model (GLB)" ,height=640)
|
96 |
+
],
|
97 |
+
title="Image to 3D Model Converter",
|
98 |
+
description="Upload an image to remove its background and convert it to a transparent 3D model. The model will have identical front and back sides with the same image orientation."
|
99 |
+
)
|
100 |
+
|
101 |
+
# Launch the interface
|
102 |
+
iface.launch()
|