Spaces:
Sleeping
Sleeping
Blair Johnson
commited on
Commit
·
f78c2b3
1
Parent(s):
63d743d
default to open body
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ pillow_heif.register_heif_opener()
|
|
| 13 |
MAX_RESOLUTION = 1.5e6
|
| 14 |
MODEL_ID = "depth-anything/Depth-Anything-V2-Large-hf"
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 17 |
model = AutoModelForDepthEstimation.from_pretrained(MODEL_ID).to(device)
|
| 18 |
print("Model loaded successfully.")
|
| 19 |
|
|
@@ -25,7 +25,8 @@ def create_3d_model(
|
|
| 25 |
x_length: float,
|
| 26 |
do_pca_correction: bool,
|
| 27 |
depth_map_smoothing: int,
|
| 28 |
-
texture_smoothing: int
|
|
|
|
| 29 |
):
|
| 30 |
if input_filepath is None: raise gr.Error("Please upload an image.")
|
| 31 |
if max_z <= min_z: raise gr.Error("Max Z-height must be greater than Min Z-height.")
|
|
@@ -117,11 +118,22 @@ def create_3d_model(
|
|
| 117 |
for j in range(width-1):
|
| 118 |
v1,v2,v3,v4 = vertices[i,j], vertices[i+1,j], vertices[i+1,j+1], vertices[i,j+1]
|
| 119 |
faces.extend([[v1, v2, v3], [v1, v3, v4]])
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
surface = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype))
|
| 124 |
surface.vectors = np.array(faces)
|
|
|
|
| 125 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as tmpfile:
|
| 126 |
surface.save(tmpfile.name)
|
| 127 |
return tmpfile.name, tmpfile.name
|
|
@@ -140,6 +152,7 @@ with gr.Blocks(theme='base') as demo:
|
|
| 140 |
min_z = gr.Number(value=0.5, label="Min Z-Height (units)")
|
| 141 |
max_z = gr.Number(value=5.0, label="Max Z-Height (units)")
|
| 142 |
do_pca = gr.Checkbox(value=True, label="Enable PCA Planar Correction")
|
|
|
|
| 143 |
|
| 144 |
generate_btn = gr.Button("Generate STL", variant="primary")
|
| 145 |
|
|
@@ -152,10 +165,10 @@ with gr.Blocks(theme='base') as demo:
|
|
| 152 |
fn=create_3d_model,
|
| 153 |
inputs=[
|
| 154 |
input_image, texture_strength, max_z, min_z, x_length, do_pca,
|
| 155 |
-
depth_map_smoothing, texture_smoothing
|
| 156 |
],
|
| 157 |
outputs=[output_model, output_file]
|
| 158 |
)
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 13 |
MAX_RESOLUTION = 1.5e6
|
| 14 |
MODEL_ID = "depth-anything/Depth-Anything-V2-Large-hf"
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID, use_fast=True)
|
| 17 |
model = AutoModelForDepthEstimation.from_pretrained(MODEL_ID).to(device)
|
| 18 |
print("Model loaded successfully.")
|
| 19 |
|
|
|
|
| 25 |
x_length: float,
|
| 26 |
do_pca_correction: bool,
|
| 27 |
depth_map_smoothing: int,
|
| 28 |
+
texture_smoothing: int,
|
| 29 |
+
close_body: int
|
| 30 |
):
|
| 31 |
if input_filepath is None: raise gr.Error("Please upload an image.")
|
| 32 |
if max_z <= min_z: raise gr.Error("Max Z-height must be greater than Min Z-height.")
|
|
|
|
| 118 |
for j in range(width-1):
|
| 119 |
v1,v2,v3,v4 = vertices[i,j], vertices[i+1,j], vertices[i+1,j+1], vertices[i,j+1]
|
| 120 |
faces.extend([[v1, v2, v3], [v1, v3, v4]])
|
| 121 |
+
|
| 122 |
+
if close_body:
|
| 123 |
+
v_tl, v_tr, v_bl, v_br = vertices[0,0], vertices[0,width-1], vertices[height-1,0], vertices[height-1,width-1]
|
| 124 |
+
b_tl,b_tr,b_bl,b_br = np.array([v_tl[0],v_tl[1],0]), np.array([v_tr[0],v_tr[1],0]), np.array([v_bl[0],v_bl[1],0]), np.array([v_br[0],v_br[1],0])
|
| 125 |
+
|
| 126 |
+
faces.extend([
|
| 127 |
+
[v_tl, b_tl, b_tr], [v_tl, b_tr, v_tr], #top wall
|
| 128 |
+
[v_br, b_br, b_bl], [v_br, b_bl, v_bl], #bottom wall
|
| 129 |
+
[v_bl, b_bl, b_tl], [v_bl, b_tl, v_tl], #left
|
| 130 |
+
[v_tr, b_tr, b_br], [v_tr, b_br, v_br], #right
|
| 131 |
+
[b_tl, b_br, b_bl], [b_tl, b_tr, b_br] #base
|
| 132 |
+
])
|
| 133 |
+
|
| 134 |
surface = mesh.Mesh(np.zeros(len(faces), dtype=mesh.Mesh.dtype))
|
| 135 |
surface.vectors = np.array(faces)
|
| 136 |
+
|
| 137 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".stl") as tmpfile:
|
| 138 |
surface.save(tmpfile.name)
|
| 139 |
return tmpfile.name, tmpfile.name
|
|
|
|
| 152 |
min_z = gr.Number(value=0.5, label="Min Z-Height (units)")
|
| 153 |
max_z = gr.Number(value=5.0, label="Max Z-Height (units)")
|
| 154 |
do_pca = gr.Checkbox(value=True, label="Enable PCA Planar Correction")
|
| 155 |
+
close_body = gr.Checkbox(value=False, label="Close Body")
|
| 156 |
|
| 157 |
generate_btn = gr.Button("Generate STL", variant="primary")
|
| 158 |
|
|
|
|
| 165 |
fn=create_3d_model,
|
| 166 |
inputs=[
|
| 167 |
input_image, texture_strength, max_z, min_z, x_length, do_pca,
|
| 168 |
+
depth_map_smoothing, texture_smoothing, close_body
|
| 169 |
],
|
| 170 |
outputs=[output_model, output_file]
|
| 171 |
)
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|