This module provides OCR (Optical Character Recognition) functionality to extract structured data from receipt images using Tesseract.js.
- Image Processing: Automatic image preprocessing (grayscale, contrast enhancement, sharpening)
- Text Extraction: OCR text extraction using Tesseract.js
- Smart Parsing: Intelligent parsing of receipt text to extract:
- Item names and quantities
- Prices
- Subtotal, tax, tip, and total amounts
- Confidence Scoring: Returns confidence scores to help determine OCR accuracy
- Error Handling: Graceful fallback when OCR fails
Upload a receipt image to extract structured data.
Request:
- Content-Type:
multipart/form-data - Body:
{ image: File }
Supported Image Formats:
- JPEG/JPG
- PNG
- WebP
File Size Limit: 10MB
Response:
{
"items": [
{
"name": "Burger",
"quantity": 2,
"price": 12.99
}
],
"subtotal": 25.98,
"tax": 2.08,
"tip": 5.00,
"total": 33.06,
"confidence": 0.85
}Error Responses:
400 Bad Request: Invalid file type, file too large, or OCR processing failed
curl -X POST http://localhost:3000/api/receipts/scan \
-F "image=@/path/to/receipt.jpg"const formData = new FormData();
formData.append('image', fileInput.files[0]);
const response = await fetch('http://localhost:3000/api/receipts/scan', {
method: 'POST',
body: formData,
});
const data = await response.json();
console.log(data);The OCR parser is designed to handle various receipt formats:
- Item lines:
Item Name $10.00or2x Item Name 20.00 - Total lines:
Total: $25.00orAMOUNT DUE: 25.00 - Subtotal:
Subtotal: $20.00orSUB-TOTAL: 20.00 - Tax:
Tax: $2.00orSALES TAX: 2.00 - Tip:
Tip: $5.00orGRATUITY: 5.00
- Single column receipts
- Multi-column receipts
- Receipts with headers/footers
- Receipts with store names and dates
The confidence score (0-1) indicates the reliability of the OCR extraction:
- 0.8-1.0: High confidence - results are likely accurate
- 0.5-0.8: Medium confidence - review results carefully
- 0.3-0.5: Low confidence - manual entry recommended
- <0.3: Very low confidence - OCR likely failed
- Image quality and resolution
- Text clarity and contrast
- Receipt format complexity
- Number of items successfully extracted
- Presence of total amounts
The service automatically preprocesses images to improve OCR accuracy:
- Grayscale conversion: Reduces color noise
- Contrast normalization: Enhances text visibility
- Resizing: Optimizes image size for OCR (max 2000px width)
- Sharpening: Improves text edge definition
If OCR processing fails or confidence is too low:
- The API returns a
400 Bad Requesterror - Error message suggests manual entry
- Original image is not stored (processed in memory only)
When OCR fails or confidence is low, users should:
- Review the extracted data (if any)
- Manually correct or enter items
- Verify totals match the receipt
Run tests with:
npm test receiptsIntegration tests include:
- Receipt parsing with various formats
- Image preprocessing
- Error handling
- Confidence score calculation
- First Request: May take longer due to Tesseract.js initialization (~2-5 seconds)
- Subsequent Requests: Typically 1-3 seconds per receipt
- Image Size: Larger images take longer to process
- Worker Reuse: Worker is reused across requests for better performance
- OCR accuracy depends on image quality
- Handwritten receipts are not supported
- Very low-quality images may fail
- Complex receipt layouts may require manual correction
- Non-English receipts may have reduced accuracy (English language model used)
- Support for multiple languages
- Machine learning for better item name recognition
- Receipt format learning/training
- Batch processing for multiple receipts
- Receipt storage and history