A user-friendly graphical interface for the Segment Anything Model (SAM) that enables interactive image segmentation with real-time visualization and multiple operation modes.

python test_gui.py- Click "选择模型文件夹" (Choose Model Folder)
- Navigate to your SAM model weights directory
- The tool will automatically detect and match model files:
sam_vit_b_*.pth→ ViT-B modelsam_vit_l_*.pth→ ViT-L modelsam_vit_h_*.pth→ ViT-H model
- Select your preferred model architecture and click "加载模型" (Load Model)
- Click "加载图像" (Load Image)
- Select your target image (supports JPG, PNG, BMP, TIFF)
- The image will appear in the main canvas
Choose from three operation modes and start segmenting!
Best for: Precise object segmentation with user guidance
How to use:
- Select "点击模式" (Point Mode)
- Choose point type:
- 前景点 (Foreground): Click on the object you want to segment
- 背景点 (Background): Click on areas you want to exclude
- Click on the image to add points
- Click "生成mask" (Generate Mask) to create segmentation
Tips:
- Red circles = Foreground points
- Blue circles = Background points
- Use "撤销" (Undo) to remove the last point
- Multiple points improve accuracy
Best for: Quick segmentation of rectangular regions
How to use:
- Select "框选模式" (Box Mode)
- Click and drag to draw a bounding box around your target object
- Release mouse button - the mask will be generated automatically
Tips:
- Yellow rectangle shows your selection
- Drag from corner to corner for best results
- The mask will be generated immediately upon release
Best for: Discovering all objects in an image
How to use:
- Select "自动分割" (Auto Mode)
- Click "生成mask" (Generate Mask)
- Wait for processing (may take 10-30 seconds)
- The largest detected object will be displayed
Tips:
- Processes the entire image automatically
- Shows the largest segmented region by default
- Great for exploration and object discovery
- Model Architecture: Choose between ViT-B (fastest), ViT-L (balanced), ViT-H (best quality)
- Model Files: Automatic detection and loading of SAM weights
- Status Indicator: Real-time model loading status with color codes:
- 🟠 Orange: Loading/Ready to load
- 🟢 Green: Successfully loaded
- 🔴 Red: Error occurred
- 加载图像 (Load Image): Import images for segmentation
- 保存mask (Save Mask): Export binary mask as PNG
- 保存叠加图 (Save Overlay): Export image with colored mask overlay
- 清除 (Clear): Remove all annotations and masks
- 生成mask (Generate Mask): Create segmentation based on current inputs
- 撤销 (Undo): Remove the last point in Point Mode
- Live Logging: View detailed operation progress
- Auto-scroll: Automatically scroll to latest messages
- Log Controls:
- 清空日志 (Clear Log): Remove all log entries
- 保存日志 (Save Log): Export log to text file
- Green overlay: Segmented regions (70% image + 30% green)
- Real-time preview: Instant visual feedback during interaction
- Red circles: Foreground points (include in mask)
- Blue circles: Background points (exclude from mask)
- White borders: Enhanced visibility on any background
- Yellow rectangle: Current selection area in Box Mode
- Start with 1-2 foreground points on your target object
- Add background points near object boundaries for precision
- Use multiple foreground points for complex shapes
- Background points help separate similar-colored objects
- Use high-resolution images when possible
- Ensure good contrast between objects
- Avoid heavily compressed or blurry images
- ViT-B: Choose for real-time interaction (fastest)
- ViT-L: Good balance of speed and quality
- ViT-H: Best quality for final results (slower)
| Status | Color | Meaning |
|---|---|---|
| 检查模块中... | 🟠 Orange | Checking SAM modules |
| 模块已加载 | 🟠 Orange | Modules loaded, ready for model |
| 已加载 | 🟢 Green | Model successfully loaded |
| 加载失败 | 🔴 Red | Model loading failed |
- JPEG (.jpg, .jpeg)
- PNG (.png)
- BMP (.bmp)
- TIFF (.tiff, .tif)
- Masks: PNG binary images (black/white)
- Overlays: PNG/JPEG with colored mask overlay
- Logs: UTF-8 text files
- Ctrl+O: Load image (when available)
- Ctrl+S: Save current mask (when available)
- Escape: Clear current annotations
- Space: Generate mask (when inputs are ready)
"请先加载模型" (Please load model first)
- Ensure you've selected a model folder with valid .pth files
- Check that the model architecture matches your files
"SAM模块不可用" (SAM modules unavailable)
- Install required dependencies:
pip install torch opencv-python pillow - Ensure SAM model files are in the correct directory
Slow performance
- Try ViT-B model for faster processing
- Reduce image resolution if very large
- Close other applications to free memory
Auto segmentation fails
- Check if your model supports automatic mask generation
- Try Point or Box mode instead
- Ensure sufficient system memory
- Check the log panel for detailed error messages
- Verify model files are correctly named and not corrupted
- Try different operation modes if one isn't working
- Restart the application if interface becomes unresponsive
| 操作 | 步骤 |
|---|---|
| 基础分割 | 加载模型 → 加载图像 → 点击模式 → 点击对象 → 生成mask |
| 快速框选 | 加载模型 → 加载图像 → 框选模式 → 拖拽矩形框 |
| 自动发现 | 加载模型 → 加载图像 → 自动分割 → 生成mask |
| 保存结果 | 生成mask → 保存mask(二进制)或 保存叠加图(可视化) |
| 重新开始 | 点击清除 → 添加新的点/框 → 生成mask |
💡 专业提示: 日志信息面板会显示后台具体操作过程 - 如果遇到问题请检查日志信息!