Vision-based angle measurement system using Meta's Segment Anything Model (SAM), refactored for modular deployment on NVIDIA Jetson Orin Nano.
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.
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
- 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
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
cd ~/sam_angle_project
source venv/bin/activatepip install --upgrade pip
pip install -r requirements.txt# Download SAM ViT-H model (2.4GB)
cd models/
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
cd ..python config.py # Check configuration
python main.py # Initialize systemfrom 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']}")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}°")Edit config.py to adjust:
SAM Model:
SAM_MODEL_TYPE: vit_h, vit_l, or vit_bSAM_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)
Automatically detects and corrects asymmetric measurements:
- Measures both line angles
- Checks vertical mirror symmetry
- If asymmetry > threshold, mirrors right line to left
- Provides both original and failsafe measurements
Triggers when:
|angle1 + angle2| > FAILSAFE_THRESHOLDdegrees
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)
- REST API for headless operation
- OBSBOT camera integration (live angle measurement)
- Material compensation database (springback)
- Multi-angle batch processing
- WebSocket streaming for real-time