Update app.py
Browse files
app.py
CHANGED
|
@@ -15,26 +15,33 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
|
| 15 |
def process_image(image_path, depth_map_path=None):
|
| 16 |
image_path = Path(image_path)
|
| 17 |
image_raw = Image.open(image_path)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
)
|
|
|
|
| 22 |
|
| 23 |
if depth_map_path:
|
| 24 |
-
#
|
| 25 |
if depth_map_path.endswith(".npy"):
|
| 26 |
depth_image = np.load(depth_map_path)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
else:
|
| 29 |
depth_image_raw = Image.open(depth_map_path)
|
| 30 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 31 |
depth_image = np.array(depth_image)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
else:
|
| 37 |
-
# Generate depth
|
| 38 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 39 |
with torch.no_grad():
|
| 40 |
outputs = model(**encoding)
|
|
@@ -49,27 +56,31 @@ def process_image(image_path, depth_map_path=None):
|
|
| 49 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 50 |
|
| 51 |
# Create inpainted background with OpenCV
|
| 52 |
-
inpainted_bg = inpaint_background(image, np.array(image_raw))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path, inpainted_bg)
|
| 56 |
img = Image.fromarray(depth_image)
|
| 57 |
return [img, gltf_path, gltf_path]
|
| 58 |
except Exception as e:
|
| 59 |
-
print(f"Error: {e}")
|
| 60 |
raise Exception("Error reconstructing 3D model")
|
| 61 |
|
| 62 |
def inpaint_background(image, original_image):
|
| 63 |
-
# Resize image to match
|
| 64 |
resized_image = image.resize(original_image.shape[1::-1]) # Resize to (width, height)
|
| 65 |
|
| 66 |
-
# Convert to grayscale
|
| 67 |
mask = np.array(resized_image.convert("L").point(lambda x: 255 if x == 0 else 0, mode="1")).astype(np.uint8)
|
| 68 |
|
| 69 |
-
# Apply inpainting
|
| 70 |
inpainted_image = cv2.inpaint(original_image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
| 71 |
return inpainted_image
|
| 72 |
-
|
| 73 |
def create_3d_obj(rgb_image, depth_image, image_path, inpainted_bg, depth=12):
|
| 74 |
# Create RGBD image in Open3D format
|
| 75 |
depth_o3d = o3d.geometry.Image(depth_image)
|
|
|
|
| 15 |
def process_image(image_path, depth_map_path=None):
|
| 16 |
image_path = Path(image_path)
|
| 17 |
image_raw = Image.open(image_path)
|
| 18 |
+
|
| 19 |
+
# Resize image to 1600 width while maintaining aspect ratio
|
| 20 |
+
new_width = 1600
|
| 21 |
+
new_height = int(new_width * image_raw.size[1] / image_raw.size[0])
|
| 22 |
+
image = image_raw.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 23 |
|
| 24 |
if depth_map_path:
|
| 25 |
+
# Determine if depth map is npy or image format
|
| 26 |
if depth_map_path.endswith(".npy"):
|
| 27 |
depth_image = np.load(depth_map_path)
|
| 28 |
+
|
| 29 |
+
# Invert depth map for .npy format
|
| 30 |
+
depth_image = np.max(depth_image) - depth_image
|
| 31 |
+
|
| 32 |
+
# Normalize depth values
|
| 33 |
+
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 34 |
+
depth_image = (depth_image * 255).astype('uint8')
|
| 35 |
else:
|
| 36 |
depth_image_raw = Image.open(depth_map_path)
|
| 37 |
depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
|
| 38 |
depth_image = np.array(depth_image)
|
| 39 |
|
| 40 |
+
# Normalize depth if needed
|
| 41 |
+
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 42 |
+
depth_image = (depth_image * 255).astype('uint8')
|
| 43 |
else:
|
| 44 |
+
# Generate depth using DPT if not provided
|
| 45 |
encoding = feature_extractor(image, return_tensors="pt")
|
| 46 |
with torch.no_grad():
|
| 47 |
outputs = model(**encoding)
|
|
|
|
| 56 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 57 |
|
| 58 |
# Create inpainted background with OpenCV
|
| 59 |
+
inpainted_bg = inpaint_background(image, np.array(image_raw.resize((new_width, new_height))))
|
| 60 |
+
|
| 61 |
+
# Ensure depth_image matches final image dimensions
|
| 62 |
+
depth_image = Image.fromarray(depth_image).resize((new_width, new_height), Image.Resampling.NEAREST)
|
| 63 |
+
depth_image = np.array(depth_image)
|
| 64 |
|
| 65 |
try:
|
| 66 |
gltf_path = create_3d_obj(np.array(image), depth_image, image_path, inpainted_bg)
|
| 67 |
img = Image.fromarray(depth_image)
|
| 68 |
return [img, gltf_path, gltf_path]
|
| 69 |
except Exception as e:
|
| 70 |
+
print(f"Error reconstructing 3D model: {e}")
|
| 71 |
raise Exception("Error reconstructing 3D model")
|
| 72 |
|
| 73 |
def inpaint_background(image, original_image):
|
| 74 |
+
# Resize `image` to match `original_image` dimensions
|
| 75 |
resized_image = image.resize(original_image.shape[1::-1]) # Resize to (width, height)
|
| 76 |
|
| 77 |
+
# Convert to grayscale and create the mask in `uint8` format
|
| 78 |
mask = np.array(resized_image.convert("L").point(lambda x: 255 if x == 0 else 0, mode="1")).astype(np.uint8)
|
| 79 |
|
| 80 |
+
# Apply inpainting
|
| 81 |
inpainted_image = cv2.inpaint(original_image, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
| 82 |
return inpainted_image
|
| 83 |
+
|
| 84 |
def create_3d_obj(rgb_image, depth_image, image_path, inpainted_bg, depth=12):
|
| 85 |
# Create RGBD image in Open3D format
|
| 86 |
depth_o3d = o3d.geometry.Image(depth_image)
|