remove caany
Browse files
app.py
CHANGED
|
@@ -16,6 +16,8 @@ from rembg import remove
|
|
| 16 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 17 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 18 |
|
|
|
|
|
|
|
| 19 |
def process_image(image_path, depth_map_path=None):
|
| 20 |
image_path = Path(image_path)
|
| 21 |
image_raw = Image.open(image_path).convert("RGB")
|
|
@@ -77,15 +79,9 @@ def process_image(image_path, depth_map_path=None):
|
|
| 77 |
# Step 5: Resize the depth map to match the foreground size
|
| 78 |
depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4) # High-quality interpolation for resizing
|
| 79 |
|
| 80 |
-
# Step 6:
|
| 81 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1) # Create a mask where the foreground is not black
|
| 82 |
-
|
| 83 |
-
edges = cv2.Canny(img_as_ubyte(mask), threshold1=15, threshold2=100) # Adjust thresholds for edges
|
| 84 |
-
edges_dilated = cv2.dilate(edges, np.ones((9, 9), np.uint8), iterations=4) # Larger kernel, fewer gaps
|
| 85 |
-
edges_eroded = cv2.erode(edges_dilated, np.ones((9, 9), np.uint8), iterations=2)
|
| 86 |
-
|
| 87 |
-
# Use the refined edge mask to clean up the depth map
|
| 88 |
-
depth_subject_only = np.where(edges_eroded == 0, depth_image * mask, 0)
|
| 89 |
depth_subject_only[~mask] = 0 # Ensure background areas are zeroed out
|
| 90 |
|
| 91 |
# Generate the final depth image with reduced blur artifacts
|
|
@@ -101,7 +97,7 @@ def process_image(image_path, depth_map_path=None):
|
|
| 101 |
gltf_path = create_3d_obj(np.array(foreground), depth_subject_only, image_path, depth=9)
|
| 102 |
img = Image.fromarray(depth_display)
|
| 103 |
return [img, gltf_path, gltf_path]
|
| 104 |
-
|
| 105 |
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
| 106 |
"""
|
| 107 |
Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
|
|
|
|
| 16 |
feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
|
| 17 |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
|
| 18 |
|
| 19 |
+
# Other imports and initialization remain unchanged
|
| 20 |
+
|
| 21 |
def process_image(image_path, depth_map_path=None):
|
| 22 |
image_path = Path(image_path)
|
| 23 |
image_raw = Image.open(image_path).convert("RGB")
|
|
|
|
| 79 |
# Step 5: Resize the depth map to match the foreground size
|
| 80 |
depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4) # High-quality interpolation for resizing
|
| 81 |
|
| 82 |
+
# Step 6: Clean up depth map using mask
|
| 83 |
mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1) # Create a mask where the foreground is not black
|
| 84 |
+
depth_subject_only = depth_image * mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
depth_subject_only[~mask] = 0 # Ensure background areas are zeroed out
|
| 86 |
|
| 87 |
# Generate the final depth image with reduced blur artifacts
|
|
|
|
| 97 |
gltf_path = create_3d_obj(np.array(foreground), depth_subject_only, image_path, depth=9)
|
| 98 |
img = Image.fromarray(depth_display)
|
| 99 |
return [img, gltf_path, gltf_path]
|
| 100 |
+
|
| 101 |
def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
|
| 102 |
"""
|
| 103 |
Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
|