Update app.py
Browse files
app.py
CHANGED
|
@@ -16,45 +16,50 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
| 16 |
def process_image(image_path, depth_map_path=None):
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path).convert("RGB")
|
| 19 |
-
# Resizing image to ensure background duplicates match the full size
|
| 20 |
image = image_raw.resize(
|
| 21 |
-
(
|
| 22 |
Image.Resampling.LANCZOS,
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
foreground = remove(image_raw)
|
| 27 |
-
foreground = Image.fromarray(np.array(foreground)).convert("RGBA") # Convert to RGBA for transparency support
|
| 28 |
-
|
| 29 |
-
# Create a duplicate background and apply small depth adjustments
|
| 30 |
-
bg_copy = image.convert("RGBA")
|
| 31 |
-
bg_copy_no_depth = image.convert("RGBA") # Background without depth for flat appearance
|
| 32 |
-
|
| 33 |
-
# Composite the foreground over the background copy
|
| 34 |
-
bg_with_depth = Image.alpha_composite(bg_copy, foreground) # First background with slight depth
|
| 35 |
-
|
| 36 |
-
# Normalize depth and convert to high quality texture
|
| 37 |
if depth_map_path:
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
else:
|
| 40 |
-
# Generate depth map
|
| 41 |
-
encoding = feature_extractor(
|
| 42 |
with torch.no_grad():
|
| 43 |
outputs = model(**encoding)
|
| 44 |
predicted_depth = outputs.predicted_depth
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=8):
|
| 60 |
# Resize and normalize the depth image
|
|
|
|
| 16 |
def process_image(image_path, depth_map_path=None):
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path).convert("RGB")
|
|
|
|
| 19 |
image = image_raw.resize(
|
| 20 |
+
(1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
|
| 21 |
Image.Resampling.LANCZOS,
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Generate or load depth map
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if depth_map_path:
|
| 26 |
+
# Load and resize depth map to match the RGB image size
|
| 27 |
+
depth_image_raw = Image.open(depth_map_path)
|
| 28 |
+
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 29 |
+
depth_image = np.array(depth_image)
|
| 30 |
+
|
| 31 |
+
# Normalize depth map if needed
|
| 32 |
+
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 33 |
+
depth_image = (depth_image * 255).astype('uint8')
|
| 34 |
else:
|
| 35 |
+
# Generate depth map using DPT model if not provided
|
| 36 |
+
encoding = feature_extractor(image, return_tensors="pt")
|
| 37 |
with torch.no_grad():
|
| 38 |
outputs = model(**encoding)
|
| 39 |
predicted_depth = outputs.predicted_depth
|
| 40 |
+
prediction = torch.nn.functional.interpolate(
|
| 41 |
+
predicted_depth.unsqueeze(1),
|
| 42 |
+
size=image.size[::-1],
|
| 43 |
+
mode="bicubic",
|
| 44 |
+
align_corners=False,
|
| 45 |
+
).squeeze()
|
| 46 |
+
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 47 |
+
|
| 48 |
+
# Make sure depth image matches dimensions and mode of RGB image for compositing
|
| 49 |
+
depth_rgba = Image.fromarray(depth_image).convert("RGBA").resize(image.size)
|
| 50 |
+
image_rgba = image.convert("RGBA")
|
| 51 |
+
|
| 52 |
+
# Composite depth onto the RGB image background without purple tint
|
| 53 |
+
bg_with_depth = Image.alpha_composite(image_rgba, depth_rgba)
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
# Create 3D object from depth and RGB image
|
| 57 |
+
gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
|
| 58 |
+
return [Image.fromarray(depth_image), gltf_path, gltf_path]
|
| 59 |
+
except Exception as e:
|
| 60 |
+
# Optionally, handle specific reconstruction issues
|
| 61 |
+
print("Error reconstructing 3D model:", e)
|
| 62 |
+
raise Exception("Error reconstructing 3D model")
|
| 63 |
|
| 64 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=8):
|
| 65 |
# Resize and normalize the depth image
|