Skip to content

Vision-based angle measurement using Meta's SAM for industrial press brake applications

License

Notifications You must be signed in to change notification settings

Radar105/sam-angle-detection

Repository files navigation

SAM Angle Measurement System - Jetson Edition

Vision-based angle measurement system using Meta's Segment Anything Model (SAM), refactored for modular deployment on NVIDIA Jetson Orin Nano.

Overview

Measures bend angles from images using AI-powered segmentation and computer vision. Originally designed for press brake angle measurement (Accurpress), now modularized for flexible deployment.

Architecture

Modular Design:

sam_angle_project/
├── config.py              # Configuration and constants
├── sam_model.py           # SAM model wrapper
├── image_processor.py     # Mask processing and edge detection
├── angle_calculator.py    # Angle computation with failsafe
├── main.py                # Entry point and pipeline demo
├── requirements.txt       # Dependencies
├── original_sam_angle.py  # Original monolithic version (reference)
├── models/                # SAM model checkpoint
├── data/                  # Test images
└── logs/                  # Logging output

Features

  • AI Segmentation: Meta's SAM for robust object detection
  • Edge Detection: OpenCV-based edge extraction
  • Angle Calculation: Geometric angle measurement from line segments
  • Failsafe Logic: Automatic symmetry correction for asymmetric detections
  • CUDA Acceleration: Optimized for NVIDIA GPUs
  • Modular Design: Clean separation of concerns for testing and reuse

Hardware

Target Platform: NVIDIA Jetson Orin Nano Super

  • GPU: 1024-core Ampere (67 TOPS)
  • Memory: 8GB unified (CUDA-optimized)
  • Storage: NVMe M.2 SSD
  • Power: 7-15W TDP

Ollama Optimizations:

  • Flash Attention: Enabled
  • KV Cache: Q8 quantization
  • Swap: 8GB on NVMe

Installation

1. Clone/Setup

cd ~/sam_angle_project
source venv/bin/activate

2. Install Dependencies

pip install --upgrade pip
pip install -r requirements.txt

3. Download SAM Model

# Download SAM ViT-H model (2.4GB)
cd models/
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
cd ..

4. Verify Installation

python config.py  # Check configuration
python main.py    # Initialize system

Usage

Python API

from main import measure_angle_from_image

# Measure angle from image
result = measure_angle_from_image(
    image_path="data/sample.jpg",
    prompt_point=(512, 384)  # Click point on V-shaped object
)

print(f"Angle: {result['angle']:.2f}°")
print(f"Failsafe: {result['failsafe_triggered']}")

Modular Components

from sam_model import SAMAngleModel
from image_processor import ImageProcessor
from angle_calculator import AngleCalculator

# Initialize
sam = SAMAngleModel()          # Load SAM model
processor = ImageProcessor()   # Image processing pipeline
calculator = AngleCalculator() # Angle calculation

# Process
sam.set_image(image_np)
mask = sam.get_best_mask(point_coords)
data = processor.process_full_pipeline(mask, prompt_x=x)
result = calculator.calculate_angle_from_points(
    data['left_points'],
    data['right_points']
)

print(f"Angle: {result['angle']:.2f}°")

Configuration

Edit config.py to adjust:

SAM Model:

  • SAM_MODEL_TYPE: vit_h, vit_l, or vit_b
  • SAM_CHECKPOINT_PATH: Model file location

Processing Parameters:

  • morph_kernel_size: Morphological closing (default: 5)
  • canny_low_threshold: Edge detection (default: 50)
  • canny_high_threshold: Edge detection (default: 150)
  • bottom_margin: Bottom exclusion zone (default: 30px)
  • percentile: Point filtering (default: 80)

Angle Calculation:

  • FAILSAFE_THRESHOLD: Asymmetry threshold in degrees (default: 5.0)

Failsafe Logic

Automatically detects and corrects asymmetric measurements:

  1. Measures both line angles
  2. Checks vertical mirror symmetry
  3. If asymmetry > threshold, mirrors right line to left
  4. Provides both original and failsafe measurements

Triggers when:

  • |angle1 + angle2| > FAILSAFE_THRESHOLD degrees

Performance

Expected on Jetson Orin Nano:

  • SAM Inference: ~2-3s per image (CUDA)
  • Edge Processing: <100ms
  • Total Pipeline: ~2-4s per measurement

Memory Usage:

  • SAM Model: ~2.5GB VRAM
  • Image Processing: ~500MB
  • Total: ~3GB (fits in 8GB with headroom)

Future Enhancements

  • REST API for headless operation
  • OBSBOT camera integration (live angle measurement)
  • Material compensation database (springback)
  • Multi-angle batch processing
  • WebSocket streaming for real-time

About

Vision-based angle measurement using Meta's SAM for industrial press brake applications

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages