11import argparse
2- import os
3- import pathlib
42
53import cv2
64import numpy as np
@@ -48,8 +46,7 @@ def args_factory() -> argparse.Namespace:
4846
4947def main ():
5048 args = args_factory ()
51- path = pathlib .Path (args .path )
52- image_names = find_files (path .absolute (), args .pattern )
49+ image_files = find_files (args .path , args .pattern )
5350
5451 # detect
5552 detector = OpenCVDetector (
@@ -60,23 +57,21 @@ def main():
6057 # segment
6158 segmentor = Sam2Segmentor (model_id = args .model_id , device = args .device )
6259
63- for image_name in progress .track (image_names , description = "Generating masks..." ):
64- image_stem = pathlib .Path (image_name ).stem
65- image_suffix = pathlib .Path (image_name ).suffix
66- img = cv2 .imread (os .path .join (path .absolute (), image_name ))
60+ for image_file in progress .track (image_files , description = "Generating masks..." ):
61+ img = cv2 .imread (image_file )
6762 annotations = False
6863 if args .pre_annotated :
6964 try :
7065 samples , labels = detector .read (
71- path = os . path . join ( path . absolute (), f"{ image_stem } _samples.csv" )
66+ path = image_file . parent / f"{ image_file . stem } _samples.csv"
7267 )
7368 annotations = True
7469 except FileNotFoundError :
7570 pass
7671 if not annotations :
7772 samples , labels = detector .detect (img )
7873 detector .write (
79- path = os . path . join ( path . absolute (), f"{ image_stem } _samples.csv" ) ,
74+ path = image_file . parent / f"{ image_file . stem } _samples.csv" ,
8075 samples = samples ,
8176 labels = labels ,
8277 )
@@ -86,15 +81,9 @@ def main():
8681 overlay = overlay_mask (img , mask , mode = "g" , scale = 1.0 )
8782
8883 # write probability and mask
89- probability_path = os .path .join (
90- path .absolute (), f"probability_sam2_{ image_stem + image_suffix } "
91- )
92- mask_path = os .path .join (
93- path .absolute (), f"mask_sam2_{ image_stem + image_suffix } "
94- )
95- overlay_path = os .path .join (
96- path .absolute (), f"overlay_sam2_{ image_stem + image_suffix } "
97- )
84+ probability_path = image_file .parent / f"probability_sam2_{ image_file .name } "
85+ mask_path = image_file .parent / f"mask_sam2_{ image_file .name } "
86+ overlay_path = image_file .parent / f"overlay_sam2_{ image_file .name } "
9887 cv2 .imwrite (probability_path , (probability * 255.0 ).astype (np .uint8 ))
9988 cv2 .imwrite (mask_path , mask )
10089 cv2 .imwrite (overlay_path , overlay )
0 commit comments