Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
|
|
| 1 |
from gradio_client import Client
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import open3d as o3d
|
| 7 |
from pathlib import Path
|
| 8 |
import os
|
| 9 |
-
import gradio as gr
|
|
|
|
| 10 |
|
| 11 |
# Initialize Gradio client for background removal
|
| 12 |
client = Client("not-lain/background-removal")
|
|
@@ -23,23 +25,21 @@ def process_image(image_path, depth_map_path=None):
|
|
| 23 |
Image.Resampling.LANCZOS,
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Handle background removal via Gradio Client using file
|
| 27 |
-
result = client.predict(str(image_path), api_name="/png")
|
| 28 |
|
| 29 |
# Check the result to determine how to handle it
|
| 30 |
if isinstance(result, list) and len(result) > 0:
|
| 31 |
-
# If the first element is a path or a URL, handle it accordingly
|
| 32 |
if isinstance(result[0], str):
|
| 33 |
-
subject_image = Image.open(result[0]) #
|
| 34 |
else:
|
| 35 |
-
# If it's not a string, assume it's binary data
|
| 36 |
subject_image = Image.open(BytesIO(result[0])) # Use BytesIO to open image data
|
| 37 |
else:
|
| 38 |
print("Unexpected result format:", result)
|
| 39 |
-
|
| 40 |
|
|
|
|
| 41 |
if depth_map_path:
|
| 42 |
-
# Load and resize the user-provided depth map to match the RGB image size
|
| 43 |
depth_image_raw = Image.open(depth_map_path)
|
| 44 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 45 |
depth_image = np.array(depth_image)
|
|
@@ -48,13 +48,11 @@ def process_image(image_path, depth_map_path=None):
|
|
| 48 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 49 |
depth_image = (depth_image * 255).astype('uint8')
|
| 50 |
else:
|
| 51 |
-
# Generate depth map using the DPT model
|
| 52 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 53 |
with torch.no_grad():
|
| 54 |
outputs = model(**encoding)
|
| 55 |
predicted_depth = outputs.predicted_depth
|
| 56 |
|
| 57 |
-
# Interpolate to original size
|
| 58 |
prediction = torch.nn.functional.interpolate(
|
| 59 |
predicted_depth.unsqueeze(1),
|
| 60 |
size=image.size[::-1],
|
|
@@ -66,13 +64,10 @@ def process_image(image_path, depth_map_path=None):
|
|
| 66 |
try:
|
| 67 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
| 68 |
img = Image.fromarray(depth_image)
|
| 69 |
-
return [img, gltf_path, gltf_path]
|
| 70 |
except Exception as e:
|
| 71 |
print(f"Error: {e}")
|
| 72 |
-
|
| 73 |
-
img = Image.fromarray(depth_image)
|
| 74 |
-
return [img, gltf_path, gltf_path]
|
| 75 |
-
|
| 76 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
| 77 |
depth_o3d = o3d.geometry.Image(depth_image)
|
| 78 |
image_o3d = o3d.geometry.Image(rgb_image)
|
|
|
|
| 1 |
+
import base64
|
| 2 |
from gradio_client import Client
|
| 3 |
+
from transformers import DPTFeatureExtractor, DPTForDepthEstimation
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image
|
| 7 |
import open3d as o3d
|
| 8 |
from pathlib import Path
|
| 9 |
import os
|
| 10 |
+
import gradio as gr # Add this import
|
| 11 |
+
from io import BytesIO # Add import for BytesIO
|
| 12 |
|
| 13 |
# Initialize Gradio client for background removal
|
| 14 |
client = Client("not-lain/background-removal")
|
|
|
|
| 25 |
Image.Resampling.LANCZOS,
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Handle background removal via Gradio Client using the file method
|
| 29 |
+
result = client.predict(gradio_client.file(str(image_path)), api_name="/png")
|
| 30 |
|
| 31 |
# Check the result to determine how to handle it
|
| 32 |
if isinstance(result, list) and len(result) > 0:
|
|
|
|
| 33 |
if isinstance(result[0], str):
|
| 34 |
+
subject_image = Image.open(result[0]) # Open as file path
|
| 35 |
else:
|
|
|
|
| 36 |
subject_image = Image.open(BytesIO(result[0])) # Use BytesIO to open image data
|
| 37 |
else:
|
| 38 |
print("Unexpected result format:", result)
|
| 39 |
+
subject_image = None # Ensure subject_image is None if result is unexpected
|
| 40 |
|
| 41 |
+
# Generate or load the depth map
|
| 42 |
if depth_map_path:
|
|
|
|
| 43 |
depth_image_raw = Image.open(depth_map_path)
|
| 44 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 45 |
depth_image = np.array(depth_image)
|
|
|
|
| 48 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 49 |
depth_image = (depth_image * 255).astype('uint8')
|
| 50 |
else:
|
|
|
|
| 51 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 52 |
with torch.no_grad():
|
| 53 |
outputs = model(**encoding)
|
| 54 |
predicted_depth = outputs.predicted_depth
|
| 55 |
|
|
|
|
| 56 |
prediction = torch.nn.functional.interpolate(
|
| 57 |
predicted_depth.unsqueeze(1),
|
| 58 |
size=image.size[::-1],
|
|
|
|
| 64 |
try:
|
| 65 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
| 66 |
img = Image.fromarray(depth_image)
|
| 67 |
+
return [img, gltf_path, gltf_path] # Ensure three return values
|
| 68 |
except Exception as e:
|
| 69 |
print(f"Error: {e}")
|
| 70 |
+
return [None, None, None] # Ensure we return three values, even on error
|
|
|
|
|
|
|
|
|
|
| 71 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
| 72 |
depth_o3d = o3d.geometry.Image(depth_image)
|
| 73 |
image_o3d = o3d.geometry.Image(rgb_image)
|