A robust receipt detection system using Mask R-CNN for automatic identification and extraction of receipts from images. The system provides both a REST API and Python client for easy integration.
- High Accuracy: Mask R-CNN model trained for receipt detection
- REST API: FastAPI-based web service for easy integration
- Batch Processing: Process multiple images simultaneously
- Multiple Formats: Support for JPG, PNG, WEBP, and other image formats
- Confidence Scoring: Returns confidence scores for each detection
- Box Merging: Automatically merges overlapping detections
- Easy Integration: Simple Python client and examples
Receipts_Detection/
βββ app.py # FastAPI web server
βββ config.py # Configuration settings
βββ requirements.txt # Python dependencies
βββ environment.yml # Conda environment
βββ src/
β βββ pipeline.py # Core detection pipeline
β βββ post_processing.py # Post-processing utilities
βββ models/
β βββ model.pth # Trained Mask R-CNN model
βββ examples/ # Usage examples
β βββ basic_usage.py # Basic detection example
β βββ batch_processing.py # Batch processing example
β βββ api_client.py # API client class
βββ scripts/ # Utility scripts
β βββ test_api.py # API testing script
β βββ test_bbox.py # Bounding box testing
β βββ test_bbox_bulk.py # Bulk testing script
βββ data/
β βββ raw/ # Input test images
β βββ processed/ # Output images with annotations
βββ docs/ # Documentation
β βββ API_REFERENCE.md # API documentation
β βββ USAGE_GUIDE.md # Usage guide
β βββ ARCHITECTURE.md # System architecture
β βββ EXAMPLES.md # Input/output examples
βββ README.md # This file
# Clone the repository
git clone <repository-url>
cd Receipts_Detection
# Install dependencies
pip install -r requirements.txt# Clone the repository
git clone <repository-url>
cd Receipts_Detection
# Create conda environment
conda env create -f environment.yml
conda activate receiptspython app.pyThe API will be available at http://127.0.0.1:8888
import requests
# Test API
url = "http://127.0.0.1:8888/predict"
with open("data/raw/unnamed (1).webp", "rb") as f:
files = {"image": f}
response = requests.post(url, files=files)
if response.status_code == 200:
result = response.json()
print(f"Found {len(result['boxes'])} receipts")
for i, (box, score) in enumerate(zip(result['boxes'], result['scores'])):
print(f"Receipt {i+1}: confidence={score:.2f}, box={box}")# Basic usage
python examples/basic_usage.py
# Batch processing
python examples/batch_processing.py
# API client example
python examples/api_client.pyfrom examples.api_client import ReceiptDetectionClient
# Initialize client
client = ReceiptDetectionClient()
# Detect receipts
result = client.detect_receipts("image.jpg")
# Get summary
summary = client.get_detection_summary(result)
print(f"Found {summary['count']} receipts")
# Crop receipts
cropped_paths = client.crop_receipts("image.jpg", result)from examples.batch_processing import process_batch
# Process all images in data/raw directory
process_batch()import requests
# Single image detection
url = "http://127.0.0.1:8888/predict"
with open("receipt.jpg", "rb") as f:
files = {"image": f}
response = requests.post(url, files=files)
result = response.json()Edit config.py to modify:
CONFIDENCE_THRESHOLD = 0.8 # Minimum confidence for detections
MODEL_PATH = "models/model.pth" # Path to trained modelModify app.py to change:
uvicorn.run("app:app", host="0.0.0.0", port=8888)- API Reference: Complete API documentation
- Usage Guide: Detailed usage instructions
- Architecture: System architecture overview
- Examples: Input/output examples and test cases
# Test API endpoint
python scripts/test_api.py
# Test bounding box detection
python scripts/test_bbox.py
# Test bulk processing
python scripts/test_bbox_bulk.pyTest images are located in data/raw/:
- Single receipts
- Multiple receipts per page
- Handwritten receipts
- Complex documents
Detect receipts in an uploaded image.
Request:
- Method: POST
- Content-Type: multipart/form-data
- Body:
image(file)
Response:
{
"boxes": [[x1, y1, x2, y2], ...],
"scores": [0.95, 0.87, ...],
"labels": [1, 1, ...]
}src/pipeline.py: Core detection logic using Mask R-CNNsrc/post_processing.py: Box merging and IoU calculationsapp.py: FastAPI web serverconfig.py: Configuration management
- New Detection Classes: Modify model architecture in
src/pipeline.py - Custom Post-processing: Add functions to
src/post_processing.py - API Endpoints: Add new routes in
app.py - Configuration: Update
config.pyfor new settings
# Using uvicorn with multiple workers
uvicorn app:app --host 0.0.0.0 --port 8888 --workers 4
# Using gunicorn
gunicorn app:app -w 4 -k uvicorn.workers.UvicornWorkerFROM python:3.10
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8888"]- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support:
- Create an issue in the repository
- Check the documentation in the
docs/folder - Review the examples in the
examples/folder
Note: This system requires a trained model file (models/model.pth). Ensure the model file is present before running the system.