Update app.py
Browse files
app.py
CHANGED
|
@@ -20,31 +20,23 @@ import torch
|
|
| 20 |
def process_image(image_path, depth_map_path=None):
|
| 21 |
image_path = Path(image_path)
|
| 22 |
image_raw = Image.open(image_path)
|
| 23 |
-
|
| 24 |
-
# Resize the input image
|
| 25 |
image = image_raw.resize(
|
| 26 |
(1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
|
| 27 |
Image.Resampling.LANCZOS,
|
| 28 |
)
|
| 29 |
|
| 30 |
-
# Separate the main subject from the background
|
| 31 |
-
main_subject = remove(image_raw.convert("RGBA")) # Convert to RGBA for transparency
|
| 32 |
-
background = image_raw.convert("RGBA").copy()
|
| 33 |
-
background.paste((255, 255, 255, 0), main_subject.getbbox()) # Make background mostly transparent
|
| 34 |
-
|
| 35 |
-
# Generate or load depth map
|
| 36 |
if depth_map_path:
|
| 37 |
-
# Load user-provided depth map
|
| 38 |
depth_image_raw = Image.open(depth_map_path)
|
| 39 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 40 |
depth_image = np.array(depth_image)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 44 |
depth_image = (depth_image * 255).astype('uint8')
|
| 45 |
|
| 46 |
else:
|
| 47 |
-
# Automatically generate the depth map using the
|
| 48 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 49 |
with torch.no_grad():
|
| 50 |
outputs = model(**encoding)
|
|
@@ -59,18 +51,27 @@ def process_image(image_path, depth_map_path=None):
|
|
| 59 |
).squeeze()
|
| 60 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
subject_depth =
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# Combine
|
| 67 |
-
depth_image_combined = np.where(subject_depth > 0,
|
| 68 |
|
| 69 |
-
# Continue with
|
| 70 |
try:
|
| 71 |
gltf_path = create_3d_obj(np.array(image), depth_image_combined, image_path)
|
| 72 |
img = Image.fromarray(depth_image_combined)
|
| 73 |
return [img, gltf_path, gltf_path]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error reconstructing 3D model: {e}")
|
| 76 |
raise
|
|
|
|
| 20 |
def process_image(image_path, depth_map_path=None):
|
| 21 |
image_path = Path(image_path)
|
| 22 |
image_raw = Image.open(image_path)
|
|
|
|
|
|
|
| 23 |
image = image_raw.resize(
|
| 24 |
(1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
|
| 25 |
Image.Resampling.LANCZOS,
|
| 26 |
)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if depth_map_path:
|
| 29 |
+
# Load and resize the user-provided depth map to match the RGB image size
|
| 30 |
depth_image_raw = Image.open(depth_map_path)
|
| 31 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 32 |
depth_image = np.array(depth_image)
|
| 33 |
|
| 34 |
+
# Adjust the depth map based on the Depth-Anything-V2 format
|
| 35 |
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 36 |
depth_image = (depth_image * 255).astype('uint8')
|
| 37 |
|
| 38 |
else:
|
| 39 |
+
# Automatically generate the depth map using the model
|
| 40 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 41 |
with torch.no_grad():
|
| 42 |
outputs = model(**encoding)
|
|
|
|
| 51 |
).squeeze()
|
| 52 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 53 |
|
| 54 |
+
# Assuming subject_depth is generated from the input depth image
|
| 55 |
+
subject_depth = depth_image # Example, replace with actual subject depth calculation
|
| 56 |
+
|
| 57 |
+
# Here, you should retrieve or create your background depth image
|
| 58 |
+
depth_image_background = np.zeros_like(subject_depth) # Replace this with actual background depth processing
|
| 59 |
+
|
| 60 |
+
# Resize background depth to match subject depth
|
| 61 |
+
depth_image_background = np.array(Image.fromarray(depth_image_background).resize(subject_depth.shape[::-1], Image.Resampling.NEAREST))
|
| 62 |
|
| 63 |
+
# Combine depth images
|
| 64 |
+
depth_image_combined = np.where(subject_depth > 0, subject_depth, depth_image_background)
|
| 65 |
|
| 66 |
+
# Continue with the rest of your process
|
| 67 |
try:
|
| 68 |
gltf_path = create_3d_obj(np.array(image), depth_image_combined, image_path)
|
| 69 |
img = Image.fromarray(depth_image_combined)
|
| 70 |
return [img, gltf_path, gltf_path]
|
| 71 |
+
except Exception as e:
|
| 72 |
+
gltf_path = create_3d_obj(np.array(image), depth_image_combined, image_path, depth=8)
|
| 73 |
+
img = Image.fromarray(depth_image_combined)
|
| 74 |
+
return [img, gltf_path, gltf_path]
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error reconstructing 3D model: {e}")
|
| 77 |
raise
|