Production-ready zero-shot object detection and segmentation toolkit
Project description
Perceptra Zero-Shot
A production-ready toolkit for zero-shot object detection and segmentation using state-of-the-art vision-language models.
๐ Features
- Unified Interface: Single API for multiple zero-shot detection models
- Multiple Models: OWL-ViT v2, Grounding DINO, and extensible architecture
- Production Ready: Robust error handling, type hints, comprehensive docs
- Three Ways to Use:
- ๐ Python Package
- ๐ฅ๏ธ Command Line Interface (CLI)
- ๐ REST API Server
- Flexible Output: Dict, COCO format, custom serialization
- Visualization Tools: Built-in result visualization
- Batch Processing: Efficient multi-image inference
๐ฆ Installation
Basic Installation
pip install perceptra-zero-shot
With Optional Dependencies
# For CLI support
pip install perceptra-zero-shot[cli]
# For API server
pip install perceptra-zero-shot[api]
# For visualization
pip install perceptra-zero-shot[viz]
# Install everything
pip install perceptra-zero-shot[all]
From Source
git clone https://github.com/tannousgeagea/perceptra-zero-shot.git
cd perceptra-zero-shot
pip install -e .
๐ฏ Quick Start
Python Package
from perceptra_zero_shot import ZeroShotDetector
from PIL import Image
# Initialize detector
detector = ZeroShotDetector(model_name="owlv2-base", device="cuda")
# Load image
image = Image.open("photo.jpg")
# Detect objects
result = detector.detect(
image=image,
prompts=["cat", "dog", "person"],
confidence_threshold=0.3
)
# Print results
print(f"Found {len(result)} objects")
for box in result.boxes:
print(f"{box.label}: {box.confidence:.2f} at {box.to_xyxy()}")
Command Line Interface
# Detect objects in an image
perceptra detect image.jpg person car dog --model owlv2-base --output result.jpg
# Batch process a directory
perceptra detect-batch images/ person car --pattern "*.jpg" --output-dir results/
# List available models
perceptra list-models
# Start API server
perceptra serve --port 8000
REST API
Start the server:
perceptra serve --port 8000
Use the API:
import requests
url = "http://localhost:8000/detect"
files = {"file": open("image.jpg", "rb")}
data = {"prompts": "person,car,dog"}
response = requests.post(url, files=files, data=data)
print(response.json())
Or with curl:
curl -X POST "http://localhost:8000/detect" \
-F "file=@image.jpg" \
-F "prompts=person,car,dog"
API Documentation available at: http://localhost:8000/docs
๐ค Supported Models
| Model | Name | Description |
|---|---|---|
| OWL-ViT v2 Base | owlv2-base |
Fast, accurate zero-shot detection |
| OWL-ViT v2 Large | owlv2-large |
Higher accuracy, slower |
| Grounding DINO | grounding-dino |
State-of-the-art open-vocabulary |
| Grounding DINO Tiny | grounding-dino-tiny |
Faster, lightweight |
from perceptra_zero_shot import ModelRegistry
# List all available models
print(ModelRegistry.list_models())
๐ Advanced Usage
Batch Processing
from perceptra_zero_shot import ZeroShotDetector
detector = ZeroShotDetector("owlv2-base")
# Process multiple images
results = detector.detect_batch(
images=["img1.jpg", "img2.jpg", "img3.jpg"],
prompts=["car", "truck", "bus"]
)
for i, result in enumerate(results):
print(f"Image {i}: {len(result)} detections")
Filtering and Post-Processing
# Filter by confidence
high_conf = result.filter_by_confidence(0.5)
# Filter by specific labels
cars_only = result.filter_by_label(["car"])
# Apply Non-Maximum Suppression
result_nms = result.apply_nms(iou_threshold=0.5)
# Get boxes for specific label
car_boxes = result.get_boxes_by_label("car")
Visualization
from perceptra_zero_shot.utils import visualize_detections
# Visualize results
vis_image = visualize_detections(
image=image,
result=result,
show_labels=True,
show_confidence=True,
output_path="output.jpg"
)
Export Formats
# Export as dictionary
data = result.to_dict()
# Export as COCO format
coco_annotations = result.to_coco_format()
# Individual box formats
for box in result.boxes:
xyxy = box.to_xyxy() # [x_min, y_min, x_max, y_max]
xywh = box.to_xywh() # [x_min, y_min, width, height]
cxcywh = box.to_cxcywh() # [center_x, center_y, width, height]
Custom Model Parameters
# OWLv2 with custom settings
detector = ZeroShotDetector(
model_name="owlv2-base",
confidence_threshold=0.4,
device="cuda"
)
# Grounding DINO with custom thresholds
detector = ZeroShotDetector(
model_name="grounding-dino",
confidence_threshold=0.3,
box_threshold=0.25,
text_threshold=0.25
)
๐๏ธ Architecture
perceptra-zero-shot/
โโโ perceptra_zero_shot/
โ โโโ core/ # Core detection logic
โ โ โโโ detector.py # Main ZeroShotDetector class
โ โ โโโ result.py # DetectionResult & BoundingBox
โ โโโ models/ # Model implementations
โ โ โโโ base.py # Abstract base class
โ โ โโโ registry.py # Model registry
โ โ โโโ owlv2.py # OWL-ViT v2
โ โ โโโ grounding_dino.py # Grounding DINO
โ โโโ utils/ # Utility functions
โ โโโ api/ # FastAPI REST API
โ โโโ cli/ # Command-line interface
โโโ examples/ # Usage examples
โโโ tests/ # Unit tests
๐ API Reference
ZeroShotDetector
Main interface for zero-shot object detection.
detector = ZeroShotDetector(
model_name: str = "owlv2-base",
device: Optional[str] = None,
confidence_threshold: float = 0.3,
**model_kwargs
)
Methods:
detect(image, prompts, **kwargs)โ DetectionResultdetect_batch(images, prompts, **kwargs)โ List[DetectionResult]to(device)โ ZeroShotDetector
DetectionResult
Container for detection results.
Properties:
boxes: List[BoundingBox]image_size: tuplemodel_name: strinference_time: float
Methods:
filter_by_confidence(threshold)โ DetectionResultfilter_by_label(labels)โ DetectionResultapply_nms(iou_threshold)โ DetectionResultto_dict()โ dictto_coco_format()โ List[dict]
BoundingBox
Represents a detected object.
Properties:
x_min, y_min, x_max, y_max: floatconfidence: floatlabel: strwidth, height, area: floatcenter: tuple
Methods:
to_xyxy()โ List[float]to_xywh()โ List[float]to_cxcywh()โ List[float]iou(other)โ float
๐ง Development
Setup Development Environment
git clone https://github.com/tannousgeagea/perceptra-zero-shot.git
cd perceptra-zero-shot
pip install -e ".[dev]"
Run Tests
pytest tests/ -v --cov=perceptra_zero_shot
Code Formatting
black perceptra_zero_shot/
isort perceptra_zero_shot/
flake8 perceptra_zero_shot/
๐ Examples
See the examples/ directory for complete examples:
basic_detection.py- Simple detection examplebatch_processing.py- Process multiple imagesapi_usage.py- Using the REST APIcompare_models.py- Compare different models
๐ Requirements
- Python โฅ 3.8
- PyTorch โฅ 2.0.0
- Transformers โฅ 4.30.0
- PIL/Pillow โฅ 9.0.0
- NumPy โฅ 1.21.0
๐ค Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- OWL-ViT v2 by Google Research
- Grounding DINO by IDEA-Research
- Hugging Face Transformers library
๐ Citation
@software{perceptra2025,
title = {Perceptra Zero-Shot: Production-Ready Zero-Shot Object Detection},
author = {Perceptra Team},
year = {2025},
url = {https://github.com/tannousgeagea/perceptra-zero-shot}
}
๐ง Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: team@perceptra.ai
Made with โค๏ธ by the Perceptra Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file perceptra_zero_shot-0.1.0.tar.gz.
File metadata
- Download URL: perceptra_zero_shot-0.1.0.tar.gz
- Upload date:
- Size: 29.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebdd8a94ba0ed6ca7c04b3556844bfce623ba74a5de1d66375b63c6de56951bb
|
|
| MD5 |
36555bc3a9220a3130b7a8dda0a5aa02
|
|
| BLAKE2b-256 |
a7d12c87aab72afc92e2e713ba06086e689b0712800d8078f0ea196313675976
|
File details
Details for the file perceptra_zero_shot-0.1.0-py3-none-any.whl.
File metadata
- Download URL: perceptra_zero_shot-0.1.0-py3-none-any.whl
- Upload date:
- Size: 30.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
751d019c6082cc6f015116203374af27e8c1d7484c9a3cae4d1184dce7bf7bcd
|
|
| MD5 |
b5bdd565b53399915048d6c4e616beba
|
|
| BLAKE2b-256 |
56d7c5e82e8d5cd78da331ba6c18995fe1bacb21d58fc822ab0a83ed582831f0
|