1
1
import numpy as np
2
2
import cv2
3
- from segment_anything import SamAutomaticMaskGenerator , sam_model_registry
4
3
import pandas as pd
5
4
import matplotlib .pyplot as plt
6
5
import torch
@@ -26,7 +25,7 @@ class ParticleAnalyzer:
26
25
>>> analyzer = ParticleAnalyzer(model_type="vit_h")
27
26
>>>
28
27
>>> # 2. Load image and run the analysis
29
- >>> image = np.load(path_to_your_image )
28
+ >>> image = np.load(IMAGE_PATH )
30
29
>>> result = analyzer.analyze(image)
31
30
>>>
32
31
>>> # 3. Print summary and visualize results
@@ -96,6 +95,15 @@ def _download_model_if_needed(self, checkpoint_path, model_type):
96
95
97
96
def _load_model (self , checkpoint_path , model_type ):
98
97
"""Loads the SAM model from a checkpoint and moves it to the device."""
98
+ try :
99
+ from segment_anything import sam_model_registry
100
+ except ImportError :
101
+ raise ImportError (
102
+ "The 'segment-anything' package is required to use this feature.\n "
103
+ "Please install it directly from the official repository:\n \n "
104
+ "pip install git+https://github.com/facebookresearch/segment-anything.git"
105
+ )
106
+
99
107
try :
100
108
sam = sam_model_registry [model_type ](checkpoint = checkpoint_path )
101
109
sam .to (device = self .device )
@@ -179,6 +187,15 @@ def _preprocess_image(self, image_array, use_clahe):
179
187
180
188
def _run_sam (self , image_rgb , preset_name ):
181
189
"""Initializes and runs the SAM mask generator based on a preset."""
190
+ try :
191
+ from segment_anything import SamAutomaticMaskGenerator
192
+ except ImportError :
193
+ raise ImportError (
194
+ "The 'segment-anything' package is required to use this feature.\n "
195
+ "Please install it directly from the official repository:\n \n "
196
+ "pip install git+https://github.com/facebookresearch/segment-anything.git"
197
+ )
198
+
182
199
sam_param_presets = {
183
200
"default" : {},
184
201
"sensitive" : {
0 commit comments