Segments sky from an image using an ONNX model. Thanks for the great model provided by https://github.com/xiongzhu666/Sky-Segmentation-and-Post-processing Args: image_path: Path to input image onnx_session: ONNX runtime session with loaded model mask_filename: P
(image_path, onnx_session, mask_filename=None)
| 616 | |
| 617 | |
| 618 | def segment_sky(image_path, onnx_session, mask_filename=None): |
| 619 | """ |
| 620 | Segments sky from an image using an ONNX model. |
| 621 | Thanks for the great model provided by https://github.com/xiongzhu666/Sky-Segmentation-and-Post-processing |
| 622 | |
| 623 | Args: |
| 624 | image_path: Path to input image |
| 625 | onnx_session: ONNX runtime session with loaded model |
| 626 | mask_filename: Path to save the output mask |
| 627 | |
| 628 | Returns: |
| 629 | np.ndarray: Binary mask where 255 indicates non-sky regions |
| 630 | """ |
| 631 | |
| 632 | assert mask_filename is not None |
| 633 | image = cv2.imread(image_path) |
| 634 | |
| 635 | result_map = run_skyseg(onnx_session, [320, 320], image) |
| 636 | # resize the result_map to the original image size |
| 637 | result_map_original = cv2.resize(result_map, (image.shape[1], image.shape[0])) |
| 638 | |
| 639 | # Fix: Invert the mask so that 255 = non-sky, 0 = sky |
| 640 | # The model outputs low values for sky, high values for non-sky |
| 641 | output_mask = np.zeros_like(result_map_original) |
| 642 | output_mask[result_map_original < 32] = 255 # Use threshold of 32 |
| 643 | |
| 644 | os.makedirs(os.path.dirname(mask_filename), exist_ok=True) |
| 645 | cv2.imwrite(mask_filename, output_mask) |
| 646 | return output_mask |
| 647 | |
| 648 | |
| 649 | def run_skyseg(onnx_session, input_size, image): |
no test coverage detected