A powerful toolkit for generating LabelMe format JSON annotations using trained models (.pt or .onnx). Perfect for creating training datasets from your existing computer vision models.
- Multiple Model Support: Works with PyTorch (.pt) and ONNX (.onnx) models
- LabelMe Format: Generates standard LabelMe JSON annotations
- Visual Reports: Generate annotated images with confidence scores and bounding boxes
- Model Verification: Verify model class configuration and detect mismatches
- Batch Processing: Process entire folders of images automatically
- Command Line Interface: Easy-to-use CLI with configuration file support
- Customizable Classes: Map model outputs to custom class names
- Confidence Filtering: Set minimum confidence thresholds
- Progress Tracking: Real-time processing feedback
auto-annotation-toolkit/
βββ src/
β βββ auto_annotation_generator.py # Main annotation generator class
β βββ model_verifier.py # Model verification utilities
β βββ quick_auto_annotate.py # Simple standalone script
βββ examples/
β βββ annotation_examples.py # Usage examples
β βββ model_verification_examples.py # Model verification examples
β βββ generate_reports_example.py # Visual report generation example
βββ tests/
β βββ quick_test.py # Quick functionality test
β βββ test_fixes.py # Model verification fixes test
β βββ test_input_size.py # Input size detection test
β βββ test_installation.py # Installation test
β βββ test_verification.py # Comprehensive verification test
β βββ run_tests.py # Test runner script
β βββ README.md # Test documentation
βββ config/
β βββ annotation_config.ini # Configuration file
βββ annotate_dataset.py # Command line interface
βββ verify_model.py # Standalone model verification tool
βββ requirements.txt # Dependencies
βββ README.md # This file
-
Clone or copy this toolkit to your project
-
Install dependencies:
pip install -r requirements.txt- For PyTorch models (.pt files):
pip install torch ultralytics- For ONNX models (.onnx files):
pip install onnxruntime- Configure settings in
config/annotation_config.ini:
[MODEL]
model_path = ../model/gn24_deployment/best.pt
confidence_threshold = 0.5
[PATHS]
input_folder = ../images_GN24/CLAHE
output_folder = ./output_annotations
report_folder = ./output_reports
[CLASSES]
0 = Good
1 = DieDefect
2 = Scratch
[REPORTS]
generate_reports = true
show_confidence_in_reports = true- Run the annotation tool:
# Basic usage (generates both annotations and reports if configured)
python annotate_dataset.py
# With custom settings
python annotate_dataset.py --model ../model/best.pt --input ../images/ --output ./annotations/ --report-folder ./reports/
# Disable reports even if configured
python annotate_dataset.py --no-reports
# Verify model configuration before processing
python annotate_dataset.py --verify-model
# Only verify model without processing
python annotate_dataset.py --verify-only
# Dry run to preview
python annotate_dataset.py --dry-runfrom src.auto_annotation_generator import AutoAnnotationGenerator
# Initialize
generator = AutoAnnotationGenerator("model/best.pt", confidence_threshold=0.5)
# Set custom class names
generator.set_class_names({0: "Good", 1: "DieDefect", 2: "Scratch"})
# Process images
generator.process_images("input_folder/", "output_annotations/")# Edit paths in src/quick_auto_annotate.py, then run:
python src/quick_auto_annotate.pyBefore processing images, verify your model's class configuration:
# Standalone model verification
python verify_model.py model/best.pt
# Verify using config file
python verify_model.py --config config/annotation_config.ini
# Save detailed report
python verify_model.py model/best.pt --output model_report.json
# Quick verification during annotation
python annotate_dataset.py --verify-modelfrom src.auto_annotation_generator import AutoAnnotationGenerator
from src.model_verifier import ModelVerifier
# Quick verification
generator = AutoAnnotationGenerator("model/best.pt")
generator.print_model_verification()
# Detailed verification
verifier = ModelVerifier("model/best.pt")
info = verifier.verify_model()
verifier.print_verification_report(info)Generate annotated images with bounding boxes and confidence scores for visual verification:
from src.auto_annotation_generator import AutoAnnotationGenerator
# Initialize generator
generator = AutoAnnotationGenerator("model/best.pt", confidence_threshold=0.5)
# Process with visual reports
generator.process_images(
input_folder="images/",
output_folder="annotations/",
report_folder="reports/", # New: visual reports folder
generate_reports=True # New: enable report generation
)Report Features:
- π― Bounding boxes with class-specific colors
- π Confidence scores displayed on each detection
- π Detection count summary on each image
- πΌοΈ High-quality images for presentation and verification
Command Line Usage:
# Generate reports with annotations
python annotate_dataset.py --report-folder ./reports/
# Disable reports (even if enabled in config)
python annotate_dataset.py --no-reportsThe toolkit generates LabelMe-compatible JSON files:
{
"version": "5.8.1",
"flags": {},
"shapes": [
{
"label": "DieDefect",
"points": [[x1, y1], [x2, y2]],
"group_id": null,
"description": "confidence: 0.85",
"shape_type": "rectangle",
"flags": {},
"mask": null
}
],
"imagePath": "image.jpg",
"imageData": null,
"imageHeight": 512,
"imageWidth": 512
}- YOLO models (.pt): Automatic detection and bounding boxes
- ONNX models (.onnx): Custom inference with configurable post-processing
- Classification models: Creates image-level annotations
- Class mapping: Map model class IDs to meaningful names
- Confidence thresholds: Filter low-confidence detections
- Image formats: Support for .jpg, .png, .bmp, .tiff
- Batch processing: Handle thousands of images efficiently
Check the examples/ folder for:
- Basic usage examples
- Single image processing
- Batch processing multiple folders
- Custom class configuration
- Model verification examples
The toolkit includes comprehensive model verification to help you identify and fix configuration issues:
- File existence and format (.pt vs .onnx)
- Model class information (number of classes, class names)
- Input/output shapes (for ONNX models)
- Class configuration mismatches between model and config
- Model metadata (training info, version, etc.)
- β Class count mismatch: Model has different number of classes than configured
- β Missing class names: Model has no class name information
- β Class name mismatch: Model class names differ from configuration
- β Single-class models: Models with only one detection class
- β ONNX shape inference: Automatic class count detection from output shape
# Verify before annotation
python annotate_dataset.py --verify-model
# Standalone verification
python verify_model.py model.pt
# Save verification report
python verify_model.py model.pt --output report.json- Python 3.7+
- OpenCV
- NumPy
- For PyTorch models: torch, ultralytics
- For ONNX models: onnxruntime
- Import errors: Make sure you're running from the correct directory
- Model loading failures: Check model file paths and formats
- Class configuration mismatches: Use
--verify-modelto check class setup - Memory issues: Process images in smaller batches for large datasets
- Permission errors: Ensure write access to output directories
- Missing dependencies: Install torch/ultralytics for .pt or onnxruntime for .onnx
Modify the post-processing in src/auto_annotation_generator.py:
def _postprocess_onnx(self, outputs, original_shape):
# Add your custom post-processing logic here
detections = []
# ... your code ...
return detectionsExtend the create_labelme_annotation method to support other formats like COCO, YOLO, etc.
The toolkit includes comprehensive tests to ensure reliability:
python tests/run_tests.pypython tests/run_tests.py --model "C:\path\to\your\model.pt"# Quick functionality test
python tests/quick_test.py
# Input size detection test
python tests/test_input_size.py
# Model verification test
python tests/test_fixes.pyThe tests verify:
- β Model loading and class detection
- β Input size (patch size) detection
- β Configuration file parsing
- β Error handling and logging
- β Import functionality
This toolkit is part of the Intel Manufacturing AI project. Please follow your organization's licensing guidelines.
To contribute to this toolkit:
- Run tests before submitting changes:
python tests/run_tests.py - Test your changes with different model types
- Update documentation for new features
- Add examples for new functionality
- Ensure compatibility with existing configurations
Happy Annotating! π―