Update app.py
Browse files
app.py
CHANGED
|
@@ -17,46 +17,49 @@ def process_image(image_path, depth_map_path=None):
|
|
| 17 |
Image.Resampling.LANCZOS,
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
if depth_map_path:
|
| 21 |
-
#
|
| 22 |
depth_image_raw = Image.open(depth_map_path)
|
| 23 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 24 |
depth_image = np.array(depth_image)
|
| 25 |
-
|
| 26 |
-
# Adjust the depth map based on the Depth-Anything-V2 format
|
| 27 |
-
# Normalize depth if needed (assuming values are not scaled to 0-255)
|
| 28 |
-
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 29 |
-
depth_image = (depth_image * 255).astype('uint8')
|
| 30 |
-
|
| 31 |
-
# Optionally invert the depth map if it's inverse (like MiDaS)
|
| 32 |
-
# depth_image = np.max(depth_image) - depth_image
|
| 33 |
-
|
| 34 |
else:
|
| 35 |
-
# Automatically generate
|
| 36 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 37 |
with torch.no_grad():
|
| 38 |
outputs = model(**encoding)
|
| 39 |
predicted_depth = outputs.predicted_depth
|
| 40 |
|
| 41 |
-
# Interpolate to original size
|
| 42 |
-
|
| 43 |
predicted_depth.unsqueeze(1),
|
| 44 |
size=image.size[::-1],
|
| 45 |
mode="bicubic",
|
| 46 |
align_corners=False,
|
| 47 |
-
).squeeze()
|
| 48 |
-
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
-
gltf_path = create_3d_obj(np.array(image),
|
| 52 |
-
img = Image.fromarray(
|
| 53 |
return [img, gltf_path, gltf_path]
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
-
img = Image.fromarray(depth_image)
|
| 57 |
-
return [img, gltf_path, gltf_path]
|
| 58 |
-
except:
|
| 59 |
-
print("Error reconstructing 3D model")
|
| 60 |
raise Exception("Error reconstructing 3D model")
|
| 61 |
|
| 62 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
|
|
|
| 17 |
Image.Resampling.LANCZOS,
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Separate foreground (subject) and background using background removal
|
| 21 |
+
result = client.predict(f=handle_file(str(image_path)), api_name="/png")
|
| 22 |
+
subject_image = Image.open(result[0]) # Result is a PNG with transparent background
|
| 23 |
+
|
| 24 |
if depth_map_path:
|
| 25 |
+
# Use user-provided depth map
|
| 26 |
depth_image_raw = Image.open(depth_map_path)
|
| 27 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 28 |
depth_image = np.array(depth_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
else:
|
| 30 |
+
# Automatically generate depth map
|
| 31 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model(**encoding)
|
| 34 |
predicted_depth = outputs.predicted_depth
|
| 35 |
|
| 36 |
+
# Interpolate depth map to original size
|
| 37 |
+
depth_image = torch.nn.functional.interpolate(
|
| 38 |
predicted_depth.unsqueeze(1),
|
| 39 |
size=image.size[::-1],
|
| 40 |
mode="bicubic",
|
| 41 |
align_corners=False,
|
| 42 |
+
).squeeze().cpu().numpy()
|
|
|
|
| 43 |
|
| 44 |
+
# Normalize depth image
|
| 45 |
+
depth_image = (depth_image * 255 / np.max(depth_image)).astype('uint8')
|
| 46 |
+
|
| 47 |
+
# Adjust subject depth separately
|
| 48 |
+
subject_depth_map = process_subject_depth(subject_image, depth_image)
|
| 49 |
+
|
| 50 |
+
# Create slightly 3D background effect (you could slightly shift the depth map here)
|
| 51 |
+
bg_depth_map = process_background_depth(image, depth_image)
|
| 52 |
+
|
| 53 |
+
# Merge depth maps back together
|
| 54 |
+
combined_depth_map = merge_depth_maps(subject_depth_map, bg_depth_map)
|
| 55 |
+
|
| 56 |
+
# 3D reconstruction process
|
| 57 |
try:
|
| 58 |
+
gltf_path = create_3d_obj(np.array(image), combined_depth_map, image_path)
|
| 59 |
+
img = Image.fromarray(combined_depth_map)
|
| 60 |
return [img, gltf_path, gltf_path]
|
| 61 |
except Exception as e:
|
| 62 |
+
print(f"Error reconstructing 3D model: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
raise Exception("Error reconstructing 3D model")
|
| 64 |
|
| 65 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|