| import streamlit as st |
| import requests |
| from PIL import Image |
| import torch |
| from transformers import DepthProImageProcessorFast, DepthProForDepthEstimation |
| import numpy as np |
| import io |
|
|
| |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
| |
| image_processor = DepthProImageProcessorFast.from_pretrained("apple/DepthPro-hf") |
| model = DepthProForDepthEstimation.from_pretrained("apple/DepthPro-hf").to(device) |
|
|
| |
| st.title("Interactive Depth-based AR Painting App") |
|
|
| |
| uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"]) |
|
|
| if uploaded_file is not None: |
| image = Image.open(uploaded_file) |
| st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
| |
| if st.button("Generate"): |
| |
| inputs = image_processor(images=image, return_tensors="pt").to(device) |
| with torch.no_grad(): |
| outputs = model(**inputs) |
|
|
| |
| post_processed_output = image_processor.post_process_depth_estimation( |
| outputs, target_sizes=[(image.height, image.width)], |
| ) |
|
|
| depth = post_processed_output[0]["predicted_depth"] |
| depth = (depth - depth.min()) / (depth.max() - depth.min()) |
| depth = depth * 255. |
| depth = depth.detach().cpu().numpy() |
| depth_image = Image.fromarray(depth.astype("uint8")) |
|
|
| st.subheader("Depth Map") |
| st.image(depth_image, caption="Estimated Depth Map", use_column_width=True) |
|
|
| |
| colormap = depth_image.convert("RGB") |
| st.subheader("Colorized Depth Map") |
| st.image(colormap, caption="Colorized Depth Map", use_column_width=True) |
|
|
| |
| if st.button('Save Depth Image'): |
| depth_image.save('depth_image.png') |
| st.success("Depth image saved successfully!") |
|
|
| |
| st.subheader("Interactive Depth-based Painting") |
|
|
| |
| canvas = st.canvas( |
| width=colormap.width, |
| height=colormap.height, |
| drawing_mode="freedraw", |
| initial_drawing=colormap, |
| key="painting_canvas" |
| ) |
|
|
| if canvas.image_data is not None: |
| |
| painted_image = Image.fromarray(canvas.image_data.astype(np.uint8)) |
|
|
| |
| st.subheader("Canvas with Painting") |
| st.image(painted_image, caption="Painting on Depth Map", use_column_width=True) |
|
|
| |
| if st.button('Save Painted Image'): |
| painted_image.save('painted_image.png') |
| st.success("Painted image saved successfully!") |
| else: |
| st.write("Draw on the canvas to interact with depth!") |