Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
from image_processor import ImageProcessor
|
| 7 |
+
from model_generator import ModelGenerator
|
| 8 |
+
from utils import create_output_directory, allowed_file
|
| 9 |
+
|
| 10 |
+
def main():
|
| 11 |
+
st.title("Image to 3D Converter")
|
| 12 |
+
st.write("Upload an image to convert it to a 3D model")
|
| 13 |
+
|
| 14 |
+
# Initialize processors
|
| 15 |
+
image_processor = ImageProcessor()
|
| 16 |
+
model_generator = ModelGenerator()
|
| 17 |
+
|
| 18 |
+
# File uploader
|
| 19 |
+
uploaded_file = st.file_uploader("Choose an image file", type=['png', 'jpg', 'jpeg'])
|
| 20 |
+
|
| 21 |
+
if uploaded_file is not None:
|
| 22 |
+
if allowed_file(uploaded_file.name):
|
| 23 |
+
# Read and display the uploaded image
|
| 24 |
+
image = Image.open(uploaded_file)
|
| 25 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 26 |
+
|
| 27 |
+
# Convert PIL Image to numpy array
|
| 28 |
+
image_np = np.array(image)
|
| 29 |
+
|
| 30 |
+
# Process button
|
| 31 |
+
if st.button("Convert to 3D"):
|
| 32 |
+
with st.spinner("Processing..."):
|
| 33 |
+
# Generate depth map
|
| 34 |
+
depth_map = image_processor.generate_depth_map(image_np)
|
| 35 |
+
|
| 36 |
+
# Display depth map
|
| 37 |
+
st.image(depth_map, caption="Generated Depth Map", use_column_width=True)
|
| 38 |
+
|
| 39 |
+
# Generate 3D model
|
| 40 |
+
points = model_generator.depth_to_points(depth_map, scale=50.0)
|
| 41 |
+
mesh = model_generator.generate_mesh(points)
|
| 42 |
+
|
| 43 |
+
# Create output directory
|
| 44 |
+
output_dir = create_output_directory()
|
| 45 |
+
output_file = os.path.join(output_dir, "output_model.obj")
|
| 46 |
+
|
| 47 |
+
# Save the mesh
|
| 48 |
+
model_generator.save_mesh(mesh, output_file)
|
| 49 |
+
|
| 50 |
+
# Provide download link
|
| 51 |
+
with open(output_file, "rb") as file:
|
| 52 |
+
st.download_button(
|
| 53 |
+
label="Download 3D Model",
|
| 54 |
+
data=file,
|
| 55 |
+
file_name="3d_model.obj",
|
| 56 |
+
mime="application/octet-stream"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
st.success("3D model generated successfully!")
|
| 60 |
+
else:
|
| 61 |
+
st.error("Please upload an image file (PNG, JPG, or JPEG)")
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
main()
|
| 65 |
+
|