Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,7 @@ cython_debug/

# PyPI configuration file
.pypirc

# Test datasets
test_dataset/
test_modules.py
80 changes: 80 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Changelog

All notable changes to the SAM Viewer project will be documented in this file.

## [1.0.0] - 2024-01-15

### Added
- **SAM Viewer Application**: Complete Qt5-based GUI application for YOLO annotation viewing with SAM2 integration
- **YOLO Parser Module**: Full support for YOLOv5 annotation format
- Parse `.txt` annotation files with format: `class_id x_center y_center width height`
- Load class definitions from `classes.txt`
- Validate annotation ranges and handle errors gracefully
- Convert YOLO format to pixel coordinates for visualization
- **Image Navigator**: Comprehensive image browsing functionality
- Load images from directories (supports .jpg, .png, .bmp, .tiff)
- Navigate with Previous/Next controls
- Display "Image X of Y" information
- Click-to-select detections on images
- Draw bounding boxes with class labels
- **SAM Interface**: Mock SAM2 integration ready for real model replacement
- Predict masks from bounding box prompts
- Predict masks from point prompts
- Extract feature embeddings from masked regions
- Compute similarity metrics (cosine and Euclidean distance)
- **Feature Matcher**: Advanced similarity search capabilities
- Extract features from all detections in dataset
- Background processing with progress indication
- Feature caching system for improved performance
- Find similar objects across entire dataset
- Threshold-based filtering of results
- **GUI Components**: Rich user interface with Qt5
- Main image display with zoom and scroll
- Detection list with click selection
- SAM controls (Find Similar, Apply Threshold, Name Objects)
- Similarity results browser
- Progress bars for background operations
- Status information and logging
- **Object Grouping**: Save and export similar object groups
- Name groups of similar objects
- Export metadata to JSON format
- Include similarity scores and detection details
- **Navigation Filtering**: Advanced navigation modes
- Normal navigation through all images
- Filtered navigation showing only images with similar objects
- Threshold-based filtering with real-time updates
- **Comprehensive Testing**: 40+ unit tests
- Test coverage for all core modules
- Mock-based testing for SAM integration
- Error handling and edge case validation
- Performance and memory efficiency tests

### Technical Features
- **Thread-Safe Design**: Background processing doesn't block UI
- **Memory Efficient**: Images loaded on-demand
- **Robust Error Handling**: Graceful handling of invalid annotations and missing files
- **Extensible Architecture**: Modular design for easy feature additions
- **Caching System**: Feature embeddings cached to disk for faster subsequent runs
- **Logging System**: Comprehensive logging for debugging and monitoring

### CLI Interface
- **Command Line Interface**: Run with `sam-viewer --dataset /path/to/dataset`
- **Flexible Options**: Verbose logging, log level control
- **Dataset Validation**: Automatic validation of dataset structure

### Documentation
- **Complete README**: Comprehensive documentation with usage examples
- **Code Documentation**: Extensive docstrings and comments
- **Type Hints**: Full type annotation for better development experience

## [Future Releases]

### Planned Features
- **Real SAM2 Integration**: Replace mock implementation with actual SAM2 models
- **Advanced Export Options**: Support for COCO, CVAT, Roboflow formats
- **Batch Processing**: Process multiple datasets in parallel
- **Visual Analytics**: PCA/UMAP visualization of embeddings
- **Performance Optimizations**: GPU acceleration and model optimization
- **Additional Similarity Metrics**: More sophisticated similarity measures
- **Annotation Editing**: Ability to modify and save annotations
- **Integration APIs**: REST API for integration with other tools
200 changes: 167 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,185 @@
# Template : How to start and customize?

- [ ] Create new repository from this template
- [ ] Inside pyproject.toml rename `package_name`
- [ ] Rename aisp_template directory to `package_name`
- [ ] Update `README.md`
# SAM Viewer - YOLO Annotation Viewer with SAM2 Integration

A powerful Qt5-based application for viewing YOLO annotations and finding similar objects using SAM2 (Segment Anything Model 2).

## Features

### βœ… Core Features (Implemented)
- **YOLO Annotation Support**: Load and display YOLOv5 format annotations
- **Interactive Image Browsing**: Navigate through datasets with arrow controls
- **Detection Visualization**: Visual overlay of bounding boxes with class labels
- **Click-to-Select**: Click on detections or select from list
- **SAM Integration**: Mock SAM2 interface ready for real model integration
- **Feature Extraction**: Extract embeddings from detected objects
- **Similarity Search**: Find similar objects across the entire dataset
- **Threshold Filtering**: Filter results by similarity threshold
- **Object Grouping**: Name and save groups of similar objects
- **Caching System**: Cache extracted features for faster subsequent runs

### πŸ”„ Coming Soon
- Real SAM2 model integration (currently using mock implementation)
- Advanced filtering options
- Batch processing capabilities
- Export to various formats (COCO, CVAT, etc.)

## Installation

### Requirements
- Python >= 3.11, < 3.12
- PyQt5 >= 5.15.7
- OpenCV >= 4.8.0
- NumPy >= 1.24.0
- Pillow >= 10.0.0

### Install from Source
```bash
git clone https://github.com/AISP-PL/ssya.git
cd ssya
pip install -e .
```

# Template directory structure
## Usage

- package_name/ - Insert package code here
- tests/ - Insert unit tests here
- scripts/ - Insert scripts here
- images/ - If this is CV/AI repository then insert images here
### Dataset Structure
Your dataset should be organized as follows:
```
dataset/
β”œβ”€β”€ images/ # Image files (.jpg, .png)
β”‚ β”œβ”€β”€ image1.jpg
β”‚ β”œβ”€β”€ image2.jpg
β”‚ └── ...
β”œβ”€β”€ labels/ # YOLO annotation files (.txt)
β”‚ β”œβ”€β”€ image1.txt
β”‚ β”œβ”€β”€ image2.txt
β”‚ └── ...
└── classes.txt # Class names (one per line)
```

# Package name
### YOLO Annotation Format
Each `.txt` file should contain detections in YOLOv5 format:
```
class_id x_center y_center width height
```
Where all coordinates are normalized (0.0-1.0).

Write package short description here.
### Running the Application
```bash
# Using the installed script
sam-viewer --dataset /path/to/your/dataset

# Installation : Developer
# Or using Python module
python -m ssya.sam_viewer.main --dataset /path/to/your/dataset

Use poetry to install the package in development mode.
# With verbose logging
sam-viewer --dataset /path/to/your/dataset --verbose
```

### Basic Workflow
1. **Load Dataset**: Start the application with your dataset path
2. **Browse Images**: Use arrow buttons or navigate through the image list
3. **Select Detection**: Click on a bounding box or select from the detection list
4. **Find Similar**: Click "Find Similar Objects" to extract features and search
5. **Filter Results**: Use the threshold slider to filter similarity results
6. **Name Groups**: Save groups of similar objects with custom names

## Application Interface

### Main Window Components
- **Image Display**: Shows current image with YOLO bounding boxes
- **Navigation Controls**: Previous/Next buttons for browsing
- **Detection List**: Lists all detections in current image
- **SAM Controls**: Find similar, apply threshold, name objects
- **Similarity Results**: Shows found similar objects
- **Status Information**: Current operation status and statistics

### Key Features
- **Visual Feedback**: Selected detections are highlighted in yellow
- **SAM Mask Overlay**: Generated masks are overlaid on images (orange with yellow contours)
- **Filtered Navigation**: When threshold is applied, navigation is limited to matching images
- **Progress Indication**: Background feature extraction shows progress
- **Comprehensive Logging**: Detailed logging for debugging and monitoring

## Technical Architecture

### Core Modules
- **`yolo_parser.py`**: Handles YOLO annotation parsing and validation
- **`image_navigator.py`**: Manages image loading and navigation
- **`sam_interface.py`**: SAM2 model interface (currently mock implementation)
- **`feature_matcher.py`**: Feature extraction and similarity search
- **`main_window.py`**: Qt5 GUI implementation

### Features
- **Thread-Safe**: Background processing doesn't block the UI
- **Caching**: Extracted features are cached for performance
- **Error Handling**: Robust error handling for invalid data
- **Memory Efficient**: Processes images on-demand
- **Extensible**: Modular design for easy feature additions

## Development

### Running Tests
```bash
git clone {URL}
uv sync
uv venv
```
# Run all tests
python -m pytest

# Testing
# Run only SAM Viewer tests
python -m pytest tests/unit/sam_viewer/ -v

Run the tests using pytest.
# Test specific module
python test_modules.py
```

```bash
uv run pytest
### Test Coverage
- 40+ unit tests covering all core functionality
- Mock-based testing for SAM integration
- Comprehensive error case coverage
- Performance and memory tests

## Example Output

### Metadata Export
When you name an object group, it saves to `dataset/output/{group_name}_group.json`:
```json
{
"group_name": "red_cars",
"created_at": "2024-01-15T10:30:00",
"threshold": 0.75,
"total_objects": 12,
"objects": [
{
"image_name": "image1.jpg",
"detection_index": 0,
"class_id": 1,
"class_name": "car",
"bbox": {
"x_center": 0.5,
"y_center": 0.6,
"width": 0.2,
"height": 0.3
},
"similarity_score": 0.87
}
]
}
```

# Release
## Contributing

Github workflow is created to automatically release the package to PyPI when a new tag "vX.X.X" (example v1.0.0) is pushed to the main branch.
1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure all tests pass
5. Submit a pull request

```bash
git tag vX.X.X
git push --tags
```
## Future Integration

Or manually build and upload the package to PyPI using the following command.
The application is designed to easily integrate with real SAM2 models. To replace the mock implementation:

```
uv build
```
1. Install SAM2 dependencies
2. Update `sam_interface.py` to use real SAM2 models
3. Replace mock prediction methods with actual SAM2 calls

## License

This project is part of the AISP-PL organization. See LICENSE for details.

11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ dynamic = ["version"]

dependencies = [
"dotenv>=0.9.9",
"yaya-tools",
"PyQt5>=5.15.7",
"opencv-python>=4.8.0",
"numpy>=1.24.0",
"Pillow>=10.0.0",
"torch>=2.0.0",
"torchvision>=0.15.0",
"segment-anything-2",
"faiss-cpu>=1.7.4",
"scikit-learn>=1.3.0",
]

[dependency-groups]
Expand Down Expand Up @@ -95,3 +103,4 @@ yaya-tools = { url = "https://github.com/AISP-PL/yaya-tools/releases/download/v1

[project.scripts]
ssya = "ssya.main:main"
sam-viewer = "ssya.sam_viewer.main:main"
1 change: 1 addition & 0 deletions ssya/sam_viewer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""SAM Viewer with filtering similar objects (Python + Qt5 + YOLO + SAM2)"""
Loading