Update app.py
Browse files
app.py
CHANGED
|
@@ -14,10 +14,11 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
| 14 |
def process_image(image_path, depth_map_path=None):
|
| 15 |
image_path = Path(image_path)
|
| 16 |
image_raw = Image.open(image_path)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
|
|
|
| 21 |
|
| 22 |
if depth_map_path:
|
| 23 |
# Determine if depth map is npy or image format
|
|
@@ -47,10 +48,10 @@ def process_image(image_path, depth_map_path=None):
|
|
| 47 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 48 |
|
| 49 |
# Create inpainted background with OpenCV
|
| 50 |
-
inpainted_bg = inpaint_background(image, np.array(image_raw))
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
depth_image = Image.fromarray(depth_image).resize(
|
| 54 |
depth_image = np.array(depth_image)
|
| 55 |
|
| 56 |
try:
|
|
@@ -58,11 +59,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 58 |
img = Image.fromarray(depth_image)
|
| 59 |
return [img, gltf_path, gltf_path]
|
| 60 |
except Exception as e:
|
| 61 |
-
|
| 62 |
-
img = Image.fromarray(depth_image)
|
| 63 |
-
return [img, gltf_path, gltf_path]
|
| 64 |
-
except:
|
| 65 |
-
print("Error reconstructing 3D model")
|
| 66 |
raise Exception("Error reconstructing 3D model")
|
| 67 |
|
| 68 |
def inpaint_background(image, original_image):
|
|
|
|
| 14 |
def process_image(image_path, depth_map_path=None):
|
| 15 |
image_path = Path(image_path)
|
| 16 |
image_raw = Image.open(image_path)
|
| 17 |
+
|
| 18 |
+
# Resize image to 1600 width while maintaining aspect ratio
|
| 19 |
+
new_width = 1600
|
| 20 |
+
new_height = int(new_width * image_raw.size[1] / image_raw.size[0])
|
| 21 |
+
image = image_raw.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 22 |
|
| 23 |
if depth_map_path:
|
| 24 |
# Determine if depth map is npy or image format
|
|
|
|
| 48 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 49 |
|
| 50 |
# Create inpainted background with OpenCV
|
| 51 |
+
inpainted_bg = inpaint_background(image, np.array(image_raw.resize((new_width, new_height))))
|
| 52 |
|
| 53 |
+
# Ensure depth_image matches final image dimensions
|
| 54 |
+
depth_image = Image.fromarray(depth_image).resize((new_width, new_height), Image.Resampling.NEAREST)
|
| 55 |
depth_image = np.array(depth_image)
|
| 56 |
|
| 57 |
try:
|
|
|
|
| 59 |
img = Image.fromarray(depth_image)
|
| 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 inpaint_background(image, original_image):
|