Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path).convert("RGB") # Ensure the image is in RGB format
|
| 19 |
image = image_raw.resize(
|
| 20 |
-
(
|
| 21 |
Image.Resampling.LANCZOS,
|
| 22 |
)
|
| 23 |
|
|
@@ -26,18 +26,27 @@ def process_image(image_path, depth_map_path=None):
|
|
| 26 |
foreground = Image.fromarray(np.array(foreground)) # Convert back to PIL Image
|
| 27 |
foreground = foreground.convert("RGB") # Ensure the foreground is in RGB format
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if depth_map_path:
|
| 31 |
depth_image_raw = Image.open(depth_map_path).convert("L") # Convert to grayscale
|
| 32 |
-
depth_image = depth_image_raw.resize(
|
| 33 |
depth_image = np.array(depth_image)
|
| 34 |
-
|
| 35 |
-
# Normalize depth image to match expected format
|
| 36 |
-
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 37 |
-
depth_image = (depth_image * 255).astype('uint8')
|
| 38 |
-
|
| 39 |
else:
|
| 40 |
-
# Generate depth map using DPT model
|
| 41 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 42 |
with torch.no_grad():
|
| 43 |
outputs = model(**encoding)
|
|
@@ -45,29 +54,18 @@ def process_image(image_path, depth_map_path=None):
|
|
| 45 |
|
| 46 |
prediction = torch.nn.functional.interpolate(
|
| 47 |
predicted_depth.unsqueeze(1),
|
| 48 |
-
size=
|
| 49 |
mode="bicubic",
|
| 50 |
align_corners=False,
|
| 51 |
).squeeze()
|
| 52 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path)
|
| 59 |
-
img = Image.fromarray(smoothed_depth_map)
|
| 60 |
-
return [img, gltf_path, gltf_path]
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print("Error with default depth. Retrying with a shallower depth.")
|
| 63 |
-
gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path, depth=8)
|
| 64 |
-
img = Image.fromarray(smoothed_depth_map)
|
| 65 |
-
return [img, gltf_path, gltf_path]
|
| 66 |
-
except:
|
| 67 |
-
print("Error reconstructing 3D model")
|
| 68 |
-
raise Exception("Error reconstructing 3D model")
|
| 69 |
|
| 70 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
|
|
|
| 71 |
depth_o3d = o3d.geometry.Image(depth_image)
|
| 72 |
image_o3d = o3d.geometry.Image(rgb_image)
|
| 73 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
@@ -81,11 +79,8 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
|
| 81 |
|
| 82 |
# Point Cloud and Normals
|
| 83 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
| 84 |
-
pcd.normals = o3d.utility.Vector3dVector(np.zeros((1, 3)))
|
| 85 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30))
|
| 86 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1000.0]))
|
| 87 |
-
pcd.transform([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 1]])
|
| 88 |
-
pcd.transform([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
|
| 89 |
|
| 90 |
# Poisson Surface Reconstruction
|
| 91 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|
|
|
|
| 17 |
image_path = Path(image_path)
|
| 18 |
image_raw = Image.open(image_path).convert("RGB") # Ensure the image is in RGB format
|
| 19 |
image = image_raw.resize(
|
| 20 |
+
(640, int(640 * image_raw.size[1] / image_raw.size[0])), # Resizing for speed
|
| 21 |
Image.Resampling.LANCZOS,
|
| 22 |
)
|
| 23 |
|
|
|
|
| 26 |
foreground = Image.fromarray(np.array(foreground)) # Convert back to PIL Image
|
| 27 |
foreground = foreground.convert("RGB") # Ensure the foreground is in RGB format
|
| 28 |
|
| 29 |
+
# Generate depth map
|
| 30 |
+
depth_image = generate_depth(foreground, depth_map_path, image.size)
|
| 31 |
+
|
| 32 |
+
# Apply Gaussian smoothing on the depth map
|
| 33 |
+
smoothed_depth_map = cv2.GaussianBlur(depth_image, (5, 5), 0)
|
| 34 |
+
|
| 35 |
+
# Create 3D object from RGB and depth images
|
| 36 |
+
try:
|
| 37 |
+
gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path)
|
| 38 |
+
img = Image.fromarray(smoothed_depth_map)
|
| 39 |
+
return [img, gltf_path, gltf_path]
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"Error reconstructing 3D model: {e}")
|
| 42 |
+
raise Exception("Error reconstructing 3D model")
|
| 43 |
+
|
| 44 |
+
def generate_depth(foreground, depth_map_path, size):
|
| 45 |
if depth_map_path:
|
| 46 |
depth_image_raw = Image.open(depth_map_path).convert("L") # Convert to grayscale
|
| 47 |
+
depth_image = depth_image_raw.resize(size, Image.Resampling.NEAREST)
|
| 48 |
depth_image = np.array(depth_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
else:
|
|
|
|
| 50 |
encoding = feature_extractor(foreground, return_tensors="pt")
|
| 51 |
with torch.no_grad():
|
| 52 |
outputs = model(**encoding)
|
|
|
|
| 54 |
|
| 55 |
prediction = torch.nn.functional.interpolate(
|
| 56 |
predicted_depth.unsqueeze(1),
|
| 57 |
+
size=size[::-1],
|
| 58 |
mode="bicubic",
|
| 59 |
align_corners=False,
|
| 60 |
).squeeze()
|
| 61 |
depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
|
| 62 |
|
| 63 |
+
# Normalize depth image to match expected format
|
| 64 |
+
depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
|
| 65 |
+
return (depth_image * 255).astype('uint8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
|
| 68 |
+
# Step 1: Create Open3D images
|
| 69 |
depth_o3d = o3d.geometry.Image(depth_image)
|
| 70 |
image_o3d = o3d.geometry.Image(rgb_image)
|
| 71 |
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(
|
|
|
|
| 79 |
|
| 80 |
# Point Cloud and Normals
|
| 81 |
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
|
|
|
| 82 |
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=30))
|
| 83 |
pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1000.0]))
|
|
|
|
|
|
|
| 84 |
|
| 85 |
# Poisson Surface Reconstruction
|
| 86 |
mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
|