CoolFace
Apppublic

algoryn/dots-ocr-idcard

sourceHugging Faceotherupdated 1y agoView on Hugging Face
0likes
App README

KYB Dots.OCR Text Extraction

This Hugging Face Space provides a FastAPI endpoint for text extraction from identity documents using Dots.OCR with ROI (Region of Interest) support. Built as a Docker Space for maximum flexibility and performance.

๐Ÿš€ Quick Start

Using the API

  1. 1.Upload an image (JPEG, PNG, or other supported formats)
  2. 2.Optionally specify ROI coordinates for targeted extraction
  3. 3.Get structured results with confidence scores and field mapping

Test the API

bash
# Basic OCR test
curl -X POST https://algoryn-dots-ocr-idcard.hf.space/v1/id/ocr \
  -F "file=@test_image.jpg"

# With ROI (region of interest)
curl -X POST https://algoryn-dots-ocr-idcard.hf.space/v1/id/ocr \
  -F "file=@test_image.jpg" \
  -F 'roi={"x1":0.1,"y1":0.1,"x2":0.9,"y2":0.9}'

โœจ Features

  • โ€”๐Ÿ” Text Extraction: Extract text from identity documents using Dots.OCR
  • โ€”๐Ÿ“ ROI Support: Process pre-cropped images or full images with ROI coordinates
  • โ€”๐Ÿ“‹ Field Mapping: Structured field extraction with confidence scores
  • โ€”๐Ÿ†” MRZ Detection: Machine Readable Zone data extraction
  • โ€”๐Ÿ”Œ Standardized API: Consistent response format for integration
  • โ€”๐Ÿณ Docker-based: Full control over dependencies and environment
  • โ€”โšก GPU Support: Optimized for Hugging Face Spaces GPU instances

๐Ÿ“ก API Endpoints

Health Check

http
GET /health

Returns service status and version information.

Text Extraction

http
POST /v1/id/ocr
Content-Type: multipart/form-data

file: <image_file>
roi: {"x1": 0.0, "y1": 0.0, "x2": 1.0, "y2": 1.0} (optional)

Parameters:

  • โ€”file: Image file to process (required)
  • โ€”roi: JSON string with normalized coordinates (optional)
  • โ€”x1, y1: Top-left corner (0.0 to 1.0)
  • โ€”x2, y2: Bottom-right corner (0.0 to 1.0)

๐Ÿ“„ Response Format

json
{
  "request_id": "uuid",
  "media_type": "image",
  "processing_time": 0.456,
  "detections": [
    {
      "mrz_data": {
        "document_type": "TD3",
        "issuing_country": "NLD",
        "surname": "MULDER",
        "given_names": "THOMAS",
        "document_number": "NLD123456789",
        "nationality": "NLD",
        "date_of_birth": "1990-01-01",
        "gender": "M",
        "date_of_expiry": "2030-01-01",
        "personal_number": "123456789",
        "raw_mrz": "P<NLDMULDER<<THOMAS<<<<<<<<<<<<<<<<<<<<<<<<<",
        "confidence": 0.95
      },
      "extracted_fields": {
        "document_number": {
          "field_name": "document_number",
          "value": "NLD123456789",
          "confidence": 0.92,
          "source": "ocr"
        },
        "surname": {
          "field_name": "surname",
          "value": "MULDER",
          "confidence": 0.96,
          "source": "ocr"
        }
      }
    }
  ]
}

๐Ÿ› ๏ธ Deployment to Hugging Face Spaces

Prerequisites

1. Create HF Space

bash
# Login to Hugging Face
huggingface-cli login

# Create a new Docker Space
huggingface-cli repo create dots-ocr-idcard --type space --space_sdk docker --organization algoryn

2. Clone and Setup

bash
# Clone the space locally
git clone https://huggingface.co/spaces/algoryn/dots-ocr-idcard
cd dots-ocr-idcard

# Copy required files
cp /path/to/kybtech-ml-pipelines/docker/hf/dots-ocr/* .

# Copy field extraction module
mkdir -p src/idcard_api
cp /path/to/kybtech-ml-pipelines/src/idcard_api/field_extraction.py src/idcard_api/
touch src/idcard_api/__init__.py

3. Deploy

bash
git add .
git commit -m "Deploy Dots-OCR text extraction service"
git push

4. Test Deployment

The Space will be available at https://algoryn-dots-ocr-idcard.hf.space after deployment (usually 5-10 minutes).

โš™๏ธ Configuration

Environment Variables

  • โ€”HF_DOTS_MODEL_PATH: Path to Dots.OCR model weights
  • โ€”HF_DOTS_CONFIDENCE_THRESHOLD: Confidence threshold for field extraction
  • โ€”HF_DOTS_DEVICE: Device to use (auto, cpu, cuda)
  • โ€”HF_DOTS_MAX_IMAGE_SIZE: Maximum image size for processing
  • โ€”HF_DOTS_MRZ_ENABLED: Enable MRZ detection

Hugging Face Spaces Settings

  • โ€”SDK: Docker
  • โ€”Port: 7860 (default)
  • โ€”Hardware: CPU (upgradeable to GPU)
  • โ€”Storage: Persistent storage available for model caching

๐Ÿ“Š Performance

HardwareProcessing TimeMemory Usage
GPU300-900ms~6GB
CPU3-8s~2GB

๐Ÿ”’ Privacy & Security

  • โ€”No Data Storage: Images are processed temporarily and not stored
  • โ€”Privacy Protection: All field values are redacted in logs
  • โ€”Secure Processing: Runs in isolated Docker containers
  • โ€”No Tracking: No user data or usage analytics collected

๐Ÿณ Local Development

Quick Start with uv

bash
# Set up development environment
make setup

# Activate virtual environment
source .venv/bin/activate  # On Unix/macOS
# or
.venv\Scripts\activate     # On Windows

# Run the application
make run-dev

Docker Development

bash
# Build and run with Docker
make build
make run-docker

# View logs
make logs

Development Commands

bash
# Run tests
make test

# Format code
make format

# Run linting
make lint

# Test API endpoints
make test-local
make test-production

For detailed development instructions, see the documentation in docs/.

๐Ÿ“š Documentation

๐Ÿค Contributing

  1. 1.Fork the repository
  2. 2.Create a feature branch
  3. 3.Make your changes
  4. 4.Test thoroughly
  5. 5.Submit a pull request

๐Ÿ“„ License

This project is licensed under a private license. See the license file for details.

๐Ÿ†˜ Support

  • โ€”Issues: Report bugs and request features via GitHub Issues
  • โ€”Discussions: Join the community discussions
  • โ€”Email: Contact us at website@huggingface.co for advanced support

Built with โค๏ธ using Hugging Face Spaces and FastAPI