Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import io
|
| 4 |
import trimesh
|
| 5 |
from trimesh import transformations
|
|
@@ -39,22 +45,24 @@ position_z = st.slider("Position Z", min_value=-5.0, max_value=5.0, value=0.0)
|
|
| 39 |
color = st.color_picker("Choose a Color", "#00ff00")
|
| 40 |
file_format = st.selectbox("Choose Export Format", ["GLB", "OBJ", "STL"])
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if uploaded_file is not None:
|
| 44 |
-
# Display the image
|
| 45 |
-
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 46 |
-
|
| 47 |
-
# Process the image for background removal (using rembg)
|
| 48 |
-
input_data = uploaded_file.read()
|
| 49 |
-
output_data = remove(input_data)
|
| 50 |
-
|
| 51 |
-
# Display the processed image (background removed)
|
| 52 |
-
output_image = Image.open(BytesIO(output_data))
|
| 53 |
-
st.image(output_image, caption="Processed Image (Background Removed)", use_column_width=True)
|
| 54 |
-
|
| 55 |
-
# Create the selected shape and apply transformations
|
| 56 |
try:
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if shape == "Box":
|
| 59 |
mesh = trimesh.creation.box(extents=[1, 1, 1])
|
| 60 |
elif shape == "Sphere":
|
|
@@ -77,19 +85,14 @@ if uploaded_file is not None:
|
|
| 77 |
mesh = trimesh.creation.box(extents=[5, 5, 0.01])
|
| 78 |
else:
|
| 79 |
st.error("Unsupported shape selected.")
|
| 80 |
-
|
| 81 |
-
|
| 82 |
if mesh:
|
| 83 |
-
# Apply
|
| 84 |
mesh.apply_scale([scale_x, scale_y, scale_z])
|
| 85 |
-
|
| 86 |
-
# Apply rotation
|
| 87 |
rotation_matrix = transformations.euler_matrix(
|
| 88 |
np.radians(rotation_x), np.radians(rotation_y), np.radians(rotation_z)
|
| 89 |
)
|
| 90 |
mesh.apply_transform(rotation_matrix)
|
| 91 |
-
|
| 92 |
-
# Apply translation
|
| 93 |
mesh.apply_translation([position_x, position_y, position_z])
|
| 94 |
|
| 95 |
# Apply color
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Attempt to import required libraries and handle missing dependencies
|
| 4 |
+
try:
|
| 5 |
+
from rembg import remove
|
| 6 |
+
except ModuleNotFoundError:
|
| 7 |
+
st.error("The `rembg` module requires `onnxruntime`. Please install it using `pip install onnxruntime` or `pip install onnxruntime-gpu` if you have a GPU.")
|
| 8 |
+
|
| 9 |
import io
|
| 10 |
import trimesh
|
| 11 |
from trimesh import transformations
|
|
|
|
| 45 |
color = st.color_picker("Choose a Color", "#00ff00")
|
| 46 |
file_format = st.selectbox("Choose Export Format", ["GLB", "OBJ", "STL"])
|
| 47 |
|
| 48 |
+
# Process the image and generate 3D asset
|
| 49 |
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
+
# Display the image
|
| 52 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 53 |
+
|
| 54 |
+
# Process the image for background removal (if `rembg` is available)
|
| 55 |
+
if "remove" in globals():
|
| 56 |
+
input_data = uploaded_file.read()
|
| 57 |
+
output_data = remove(input_data)
|
| 58 |
+
# Display the processed image (background removed)
|
| 59 |
+
output_image = Image.open(BytesIO(output_data))
|
| 60 |
+
st.image(output_image, caption="Processed Image (Background Removed)", use_column_width=True)
|
| 61 |
+
else:
|
| 62 |
+
st.warning("Image processing with background removal is unavailable. Please install `onnxruntime` and restart the app.")
|
| 63 |
+
|
| 64 |
+
# Create the selected shape and apply transformations
|
| 65 |
+
mesh = None
|
| 66 |
if shape == "Box":
|
| 67 |
mesh = trimesh.creation.box(extents=[1, 1, 1])
|
| 68 |
elif shape == "Sphere":
|
|
|
|
| 85 |
mesh = trimesh.creation.box(extents=[5, 5, 0.01])
|
| 86 |
else:
|
| 87 |
st.error("Unsupported shape selected.")
|
| 88 |
+
|
|
|
|
| 89 |
if mesh:
|
| 90 |
+
# Apply transformations
|
| 91 |
mesh.apply_scale([scale_x, scale_y, scale_z])
|
|
|
|
|
|
|
| 92 |
rotation_matrix = transformations.euler_matrix(
|
| 93 |
np.radians(rotation_x), np.radians(rotation_y), np.radians(rotation_z)
|
| 94 |
)
|
| 95 |
mesh.apply_transform(rotation_matrix)
|
|
|
|
|
|
|
| 96 |
mesh.apply_translation([position_x, position_y, position_z])
|
| 97 |
|
| 98 |
# Apply color
|