This project implements the EdgeSAM (Segmentation-Anything Model) using ONNX Runtime and OpenCV in C++, delivering 40x faster inference for edge device deployment.
Visit our ultra-modern web interface by opening index.html in your browser to explore:
- π¨ Interactive demo with animations
- β‘ Feature showcase with glassmorphism design
- π Quick installation guide
- π― Technology stack overview
Features:
- Modern, responsive design
- Smooth animations and transitions
- Interactive UI elements
- Dark theme with gradient effects
- Uses Edge SAM model for segmentation, which includes a preprocessing model and a SAM model.
- Image preprocessing and segmentation with ONNX Runtime and OpenCV.
- Efficient handling of image inputs and outputs.
- Customizable for different segmentation tasks.
- This implementation is compatible with the Edge SAM model in ONNX format. The model paths are specified in the parameters and expected to be in the ONNX format.
Before running the project, ensure that the following libraries are installed:
- C++ Compiler (supporting C++17 or later)
- OpenCV (4.8.0)
- ONNX Runtime (1.12.1)
These libraries can typically be installed via pip or your system's package manager.
- Place your ONNX model files in the models directory.
- Place the images for processing in the images directory.
- Compile the code using a C++ compiler.
- Run the executable. The program processes the image using the EdgeSAM model and outputs the results.
. ./build.sh
./edgeSamOrtCpp ../images/xxx.pngThe application expects the following input format:
- Model path: "../models/edge_sam_3x_encoder.onnx" or "../models/edge_sam_3x_decoder.onnx"
- Image path: "../images/xxx.png"
The program outputs the segmented image with applied masks. Additional information like image resizing and processing steps are logged to the console.
EdgeSAM now includes a comprehensive Python package with modern development tooling!
# Install from source
pip install -e .
# Install with development dependencies
pip install -e ".[dev]"
# Install with all optional dependencies
pip install -e ".[all]"from edgesam_py import EdgeSAMSegmenter
import numpy as np
# Initialize segmenter
segmenter = EdgeSAMSegmenter(
encoder_path="models/edge_sam_3x_encoder.onnx",
decoder_path="models/edge_sam_3x_decoder.onnx"
)
# Segment an image
image, mask = segmenter.segment("path/to/image.png")
# Save result
segmenter.save_result(image, mask, "output.png")# Basic usage
edgesam -i input.png
# With custom prompt point
edgesam -i input.png --point-x 512 --point-y 512
# With GPU acceleration
edgesam -i input.png --gpu
# Full options
edgesam -i input.png -o output.png \
-e models/encoder.onnx \
-d models/decoder.onnx \
--threshold 0.7 \
--verboseThis project uses ultra-modern Python development tooling for production-ready code:
- Hatch - Modern Python project manager with environment isolation
- Ruff β‘ - Ultra-fast linter AND formatter (10-100x faster than traditional tools!)
- Replaces Black + Flake8 + isort + pyupgrade in a single tool
- Written in Rust for maximum performance
- UV π - Blazing fast Python package installer (10-100x faster than pip)
- MyPy - Strict static type checker with full coverage
- Pytest - Testing framework with:
- pytest-xdist for parallel execution (6x faster!)
- pytest-cov for comprehensive coverage tracking (73%+ coverage)
- pytest-benchmark for performance testing
- Pre-commit - Automated quality gates on every commit
β
11/11 tests passing (100%)
β
73.06% code coverage
β
0 mypy errors
β
0 ruff violations
β
0 security issues (bandit)
β
Production ready!
Name Stmts Miss Branch BrPart Cover
-----------------------------------------------------------------
edgesam_py/__init__.py 7 2 0 0 71.43%
edgesam_py/cli.py 64 21 22 8 59.30%
edgesam_py/segmentation.py 78 8 22 7 85.00%
-----------------------------------------------------------------
TOTAL 149 31 44 15 73.06%
# 1. Clone the repository
git clone https://github.com/umitkacar/edgeSAM-onnxruntime-cpp
cd edgeSAM-onnxruntime-cpp
# 2. Install with development dependencies
pip install -e ".[dev]"
# 3. Install pre-commit hooks
pre-commit install
# 4. Run tests to verify installation
pytest -n auto
# 5. You're ready to develop! π# Run tests
hatch run test
# Run tests with coverage
hatch run test-cov
# Run tests in parallel (6x faster!)
pytest -n auto
# Run linters and type checking
hatch run lint:all
# Format code (Ruff)
hatch run lint:fmt
# Type checking (MyPy)
hatch run lint:typing# Run all tests (fast, parallel)
pytest -n auto
# Run with verbose output
pytest -xvs
# Run with coverage report
pytest --cov=edgesam_py --cov-report=term-missing
# Run only fast tests (skip slow integration tests)
pytest -m "not slow"
# Run specific test file
pytest tests/test_segmentation.py
# Run specific test
pytest tests/test_cli.py::TestCLI::test_version_flag# Lint and auto-fix with Ruff
ruff check --fix .
# Format code with Ruff
ruff format .
# Type check with MyPy
mypy edgesam_py tests
# Run all quality checks
ruff check . && ruff format --check . && mypy edgesam_py testsInstall pre-commit hooks:
pre-commit installRun on all files:
pre-commit run --all-files# Run all tests with parallel execution (FAST!)
pytest -n auto
# Run with comprehensive coverage
pytest --cov=edgesam_py --cov-branch --cov-report=html
# Open htmlcov/index.html in browser to see detailed coverage
# Run only unit tests
pytest -m unit
# Run only integration tests
pytest -m integration
# Run only slow tests
pytest -m slow
# Run benchmarks
pytest -m benchmark
# Run with verbose output and stop on first failure
pytest -xvs --tb=short
# Run and generate all report formats
pytest --cov=edgesam_py --cov-report=term --cov-report=html --cov-report=xml# Install dependencies with UV (10-100x faster than pip!)
uv pip install -e ".[dev]"
# Compile requirements with lock file
uv pip compile pyproject.toml -o requirements.txt
# Sync dependencies
uv pip sync requirements.txt
# Install a single package
uv pip install numpyEvery commit is automatically checked for:
- β Code Style: Ruff formatting (Black-compatible, 100-char lines)
- β Linting: Ruff linter (20+ rule categories including security)
- β Type Safety: MyPy strict type checking with zero errors
- β Testing: 73.06% code coverage, all tests passing
- β Security: Bandit security scanning, no vulnerabilities
- β Secrets: No credentials or API keys in code (detect-secrets)
- β Shell Scripts: ShellCheck validation for bash scripts
- β Documentation: Markdownlint for consistent docs
| Metric | Target | Current | Status |
|---|---|---|---|
| Test Coverage | β₯70% | 73.06% | β Pass |
| Tests Passing | 100% | 11/11 (100%) | β Pass |
| MyPy Errors | 0 | 0 | β Pass |
| Ruff Violations | 0 | 0 | β Pass |
| Security Issues | 0 | 0 | β Pass |
| Type Hints | 100% | 100% | β Pass |
When you commit, these checks run automatically:
-
General Checks (5 hooks)
- Trailing whitespace removal
- End-of-file fixing
- YAML/TOML/JSON validation
- Merge conflict detection
- Large file prevention (>1MB)
-
Python Quality (4 hooks)
- Ruff linting with auto-fix
- Ruff formatting
- MyPy type checking
- PyUpgrade syntax modernization
-
Security (2 hooks)
- Bandit security scanning
- Detect-secrets credential scanning
-
Multi-language (5 hooks)
- ShellCheck for bash scripts
- Clang-format for C++ code
- CMake-format for CMake files
- Prettier for web files (JS/CSS/HTML)
- Markdownlint for documentation
-
Testing (on push only - slower)
- Full pytest suite
- Coverage threshold check (β₯70%)
Total time: ~5 seconds on commit, ~30 seconds on push
edgeSAM-onnxruntime-cpp/
βββ π¦ edgesam_py/ # Python package (production-ready)
β βββ __init__.py # Package exports and version
β βββ _version.py # Auto-generated version (VCS)
β βββ segmentation.py # Core EdgeSAM segmentation (85% coverage)
β βββ cli.py # Command-line interface (59% coverage)
β
βββ π§ͺ tests/ # Comprehensive test suite (73% coverage)
β βββ __init__.py
β βββ conftest.py # Pytest fixtures and configuration
β βββ test_segmentation.py # Segmentation tests (7 tests)
β βββ test_cli.py # CLI tests (4 tests)
β
βββ π§ src/ # C++ source code
β βββ edgeSam.cpp # C++ implementation
β βββ edgeSam.h # C++ headers
β βββ main.cpp # C++ entry point
β
βββ π include/ # ONNX Runtime headers
β βββ onnxruntime/ # ONNX Runtime C++ API
β
βββ π€ models/ # ONNX model files (not in repo)
β βββ edge_sam_3x_encoder.onnx
β βββ edge_sam_3x_decoder.onnx
β
βββ πΌοΈ images/ # Test images (not in repo)
β
βββ π Web Interface
β βββ index.html # Modern glassmorphism UI
β βββ styles.css # Responsive styling
β βββ script.js # Interactive features
β
βββ π Documentation
β βββ README.md # This file
β βββ CHANGELOG.md # Detailed version history
β βββ LESSONS_LEARNED.md # Development insights (400+ lines)
β
βββ βοΈ Configuration
β βββ pyproject.toml # Python project config (modern, Hatch-based)
β βββ .pre-commit-config.yaml # 15+ pre-commit hooks
β βββ .clang-format # C++ formatting rules
β βββ .secrets.baseline # Secret scanning baseline
β βββ build.sh # C++ build script
β
βββ π¨ Build artifacts
βββ htmlcov/ # Coverage HTML reports
βββ .coverage # Coverage data
βββ .pytest_cache/ # Pytest cache
- pyproject.toml: Modern Python packaging with Hatch, Ruff, MyPy configuration
- LESSONS_LEARNED.md: In-depth analysis of refactoring decisions (must-read!)
- CHANGELOG.md: Complete version history with migration guides
We welcome contributions! Here's how to get started:
- Fork and clone:
git clone https://github.com/YOUR_USERNAME/edgeSAM-onnxruntime-cpp
cd edgeSAM-onnxruntime-cpp- Create a feature branch:
git checkout -b feature/amazing-feature- Install development dependencies:
pip install -e ".[dev]"
# Or use UV for faster installation:
uv pip install -e ".[dev]"- Install pre-commit hooks:
pre-commit install-
Make your changes with confidence - tests will catch issues!
-
Run tests locally:
# Fast parallel tests
pytest -n auto
# With coverage
pytest --cov=edgesam_py- Format and lint:
# Auto-format code
ruff format .
# Lint and auto-fix
ruff check --fix .
# Type check
mypy edgesam_py tests- Commit your changes:
git add .
git commit -m 'Add amazing feature'
# Pre-commit hooks will run automatically!- Push and create PR:
git push origin feature/amazing-feature
# Then open a Pull Request on GitHubAll contributions must pass:
- β Ruff linting (no violations)
- β Ruff formatting (Black-compatible style)
- β MyPy type checking (strict mode, zero errors)
- β Pytest tests (all tests passing)
- β Coverage (maintain or improve 73%+ coverage)
- β Pre-commit hooks (15+ automated checks)
- β Security scanning (Bandit, no vulnerabilities)
# Run the full test suite
pytest -xvs
# Run with coverage check
pytest --cov=edgesam_py --cov-report=term-missing --cov-fail-under=70
# Run pre-commit on all files (same as CI)
pre-commit run --all-files
# Verify type safety
mypy edgesam_py testsIf you're adding new features:
- Add docstrings (Google style)
- Update README.md if needed
- Add tests for new functionality
- Update CHANGELOG.md
We use Ruff for both linting and formatting:
# Good - Type hints, clear names, docstrings
def segment_image(
image_path: Path,
point_coords: NDArray[np.float32] | None = None,
) -> tuple[NDArray[np.uint8], NDArray[np.float32]]:
"""Segment an image using EdgeSAM.
Args:
image_path: Path to input image.
point_coords: Optional point coordinates for prompting.
Returns:
Tuple of (original image, segmentation mask).
Raises:
FileNotFoundError: If image doesn't exist.
"""
# Implementation here- π Read LESSONS_LEARNED.md for detailed insights
- π Check CHANGELOG.md for recent changes
- π¬ Open an issue for questions or suggestions
- π Report bugs with minimal reproduction examples
This project is licensed under the MIT License - see the LICENSE file for details.