File size: 8,223 Bytes
4109acb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
#!/usr/bin/env python3
"""
Water Surface Segmentation Inference Script
This script performs inference on beach images to segment water surfaces using YOLOv11n.
"""
import argparse
import os
import sys
from pathlib import Path
import cv2
import numpy as np
from ultralytics import YOLO
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import matplotlib.pyplot as plt
from typing import Optional, Tuple, List
def parse_arguments() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description="Perform water surface segmentation on beach images",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--image",
type=str,
required=True,
help="Path to input image file"
)
parser.add_argument(
"--weights",
type=str,
default="model/nwsd-v2.pt",
help="Path to model weights file"
)
parser.add_argument(
"--output",
type=str,
default=None,
help="Output directory for results (default: same as input image)"
)
parser.add_argument(
"--conf",
type=float,
default=0.25,
help="Confidence threshold for segmentation"
)
parser.add_argument(
"--iou",
type=float,
default=0.45,
help="IoU threshold for NMS"
)
parser.add_argument(
"--save-overlay",
action="store_true",
help="Save overlay visualization"
)
parser.add_argument(
"--save-mask",
action="store_true",
help="Save binary mask"
)
parser.add_argument(
"--save-results",
action="store_true",
help="Save results visualization plot"
)
parser.add_argument(
"--device",
type=str,
default="cpu",
help="Device to use for inference (cpu, cuda, mps)"
)
return parser.parse_args()
def validate_inputs(args: argparse.Namespace) -> None:
"""Validate input arguments."""
if not os.path.exists(args.image):
raise FileNotFoundError(f"Input image not found: {args.image}")
if not os.path.exists(args.weights):
raise FileNotFoundError(f"Model weights not found: {args.weights}")
valid_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.tif'}
image_ext = Path(args.image).suffix.lower()
if image_ext not in valid_extensions:
raise ValueError(f"Unsupported image format: {image_ext}")
def load_model(weights_path: str, device: str = "cpu") -> YOLO:
"""Load YOLO model."""
try:
model = YOLO(weights_path)
model.to(device)
print(f"Model loaded successfully from: {weights_path}")
print(f"Using device: {device}")
return model
except Exception as e:
raise RuntimeError(f"Failed to load model: {str(e)}")
def preprocess_image(image_path: str) -> Tuple[np.ndarray, Tuple[int, int]]:
"""Load and preprocess image."""
image = cv2.imread(image_path)
if image is None:
raise ValueError(f"Could not read image: {image_path}")
original_shape = image.shape[:2] # (height, width)
return image, original_shape
def postprocess_results(results, original_shape: Tuple[int, int]) -> Tuple[np.ndarray, np.ndarray]:
"""Extract masks and create binary mask."""
if len(results) == 0 or results[0].masks is None:
print("No water surface detected in the image")
return None, None
result = results[0]
masks = result.masks.data.cpu().numpy() # Shape: (N, H, W)
binary_mask = np.zeros(original_shape, dtype=np.uint8)
if len(masks) > 0:
resized_masks = []
for mask in masks:
resized_mask = cv2.resize(mask, (original_shape[1], original_shape[0]))
resized_masks.append(resized_mask)
combined_mask = np.max(resized_masks, axis=0)
binary_mask = (combined_mask > 0.5).astype(np.uint8) * 255
return binary_mask, masks
def create_overlay(image: np.ndarray, binary_mask: np.ndarray, alpha: float = 0.3) -> np.ndarray:
"""Create overlay visualization."""
overlay = image.copy()
colored_mask = np.zeros_like(image)
colored_mask[binary_mask > 0] = [255, 0, 0]
overlay = cv2.addWeighted(overlay, 1 - alpha, colored_mask, alpha, 0)
return overlay
def save_results(
image: np.ndarray,
binary_mask: Optional[np.ndarray],
overlay: Optional[np.ndarray],
output_dir: str,
base_name: str,
save_mask: bool = False,
save_overlay: bool = False
) -> None:
"""Save results to output directory."""
os.makedirs(output_dir, exist_ok=True)
if save_mask and binary_mask is not None:
mask_path = os.path.join(output_dir, f"{base_name}_mask.png")
cv2.imwrite(mask_path, binary_mask)
print(f"Binary mask saved to: {mask_path}")
if save_overlay and overlay is not None:
overlay_path = os.path.join(output_dir, f"{base_name}_overlay.png")
cv2.imwrite(overlay_path, overlay)
print(f"Overlay visualization saved to: {overlay_path}")
def display_results(
image: np.ndarray,
binary_mask: Optional[np.ndarray],
overlay: Optional[np.ndarray],
output_dir: str = ".",
base_name: str = "result"
) -> None:
"""Display results using matplotlib."""
num_plots = 1 + (binary_mask is not None) + (overlay is not None)
plt.figure(figsize=(5 * num_plots, 5))
plot_idx = 1
plt.subplot(1, num_plots, plot_idx)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Original Image")
plt.axis('off')
plot_idx += 1
if binary_mask is not None:
plt.subplot(1, num_plots, plot_idx)
plt.imshow(binary_mask, cmap='gray')
plt.title("Water Surface Mask")
plt.axis('off')
plot_idx += 1
if overlay is not None:
plt.subplot(1, num_plots, plot_idx)
plt.imshow(cv2.cvtColor(overlay, cv2.COLOR_BGR2RGB))
plt.title("Overlay Visualization")
plt.axis('off')
plt.tight_layout()
plot_path = os.path.join(output_dir, f"{base_name}_results.png")
plt.savefig(plot_path, dpi=150, bbox_inches='tight')
print(f"Results visualization saved to: {plot_path}")
plt.close()
def calculate_water_percentage(binary_mask: np.ndarray) -> float:
"""Calculate percentage of water surface in the image."""
if binary_mask is None:
return 0.0
total_pixels = binary_mask.shape[0] * binary_mask.shape[1]
water_pixels = np.sum(binary_mask > 0)
return (water_pixels / total_pixels) * 100
def main():
"""Main inference function."""
args = parse_arguments()
try:
validate_inputs(args)
if args.output is None:
output_dir = os.path.dirname(args.image)
if not output_dir:
output_dir = "."
else:
output_dir = args.output
base_name = Path(args.image).stem
model = load_model(args.weights, args.device)
image, original_shape = preprocess_image(args.image)
print(f"Processing image: {args.image}")
print(f"Image shape: {image.shape}")
results = model(
args.image,
conf=args.conf,
iou=args.iou,
verbose=False
)
binary_mask, masks = postprocess_results(results, original_shape)
overlay = None
if binary_mask is not None:
overlay = create_overlay(image, binary_mask)
water_percentage = calculate_water_percentage(binary_mask)
print(f"Water surface coverage: {water_percentage:.2f}%")
save_results(
image,
binary_mask,
overlay,
output_dir,
base_name,
save_mask=args.save_mask,
save_overlay=args.save_overlay
)
if args.save_results:
display_results(image, binary_mask, overlay, output_dir, base_name)
print("Inference completed successfully!")
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()
|