Update processing/segmentation/hair_segmentation.py
Browse files
processing/segmentation/hair_segmentation.py
CHANGED
|
@@ -262,7 +262,7 @@ def segment(self, frame: np.ndarray) -> np.ndarray:
|
|
| 262 |
# Combine masks
|
| 263 |
combined_mask = cv2.bitwise_or(hair_mask_hsv, hair_mask_lab)
|
| 264 |
|
| 265 |
-
# Morphological operations
|
| 266 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
|
| 267 |
combined_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_CLOSE, kernel)
|
| 268 |
combined_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_OPEN, kernel)
|
|
@@ -472,14 +472,16 @@ def _try_segment_with_model(self, frame: np.ndarray, model_name: str) -> Tuple[O
|
|
| 472 |
return None, model_name, error_msg
|
| 473 |
|
| 474 |
def _calculate_confidence(self, mask: np.ndarray) -> float:
|
| 475 |
-
"""Calculate mask confidence"""
|
| 476 |
# Edge sharpness
|
| 477 |
edges = cv2.Canny((mask * 255).astype(np.uint8), 50, 150)
|
| 478 |
edge_ratio = np.sum(edges > 0) / mask.size
|
| 479 |
|
| 480 |
-
# Mask smoothness
|
| 481 |
-
|
| 482 |
-
|
|
|
|
|
|
|
| 483 |
|
| 484 |
return min(edge_ratio * 0.3 + smoothness * 0.7, 1.0)
|
| 485 |
|
|
|
|
| 262 |
# Combine masks
|
| 263 |
combined_mask = cv2.bitwise_or(hair_mask_hsv, hair_mask_lab)
|
| 264 |
|
| 265 |
+
# Morphological operations (using OpenCV instead of skimage)
|
| 266 |
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))
|
| 267 |
combined_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_CLOSE, kernel)
|
| 268 |
combined_mask = cv2.morphologyEx(combined_mask, cv2.MORPH_OPEN, kernel)
|
|
|
|
| 472 |
return None, model_name, error_msg
|
| 473 |
|
| 474 |
def _calculate_confidence(self, mask: np.ndarray) -> float:
|
| 475 |
+
"""Calculate mask confidence using OpenCV instead of skimage"""
|
| 476 |
# Edge sharpness
|
| 477 |
edges = cv2.Canny((mask * 255).astype(np.uint8), 50, 150)
|
| 478 |
edge_ratio = np.sum(edges > 0) / mask.size
|
| 479 |
|
| 480 |
+
# Mask smoothness using OpenCV Sobel instead of skimage gradient
|
| 481 |
+
grad_x = cv2.Sobel(mask, cv2.CV_64F, 1, 0, ksize=3)
|
| 482 |
+
grad_y = cv2.Sobel(mask, cv2.CV_64F, 0, 1, ksize=3)
|
| 483 |
+
gradient_magnitude = np.sqrt(grad_x**2 + grad_y**2)
|
| 484 |
+
smoothness = 1.0 / (1.0 + np.std(gradient_magnitude))
|
| 485 |
|
| 486 |
return min(edge_ratio * 0.3 + smoothness * 0.7, 1.0)
|
| 487 |
|