CoolFace
Apppublic

algoryn/dots-ocr-idcard

sourceHugging Faceotherupdated 1y agoView on Hugging Face
0likes
test_production.py80 linesDownload Raw Back to scripts
1#!/usr/bin/env python32"""Production API Test Script3 4Quick test script specifically for the production Dots.OCR API.5"""6 7import requests8import json9import sys10from pathlib import Path11 12def test_production_api():13    """Test the production API endpoint."""14    15    api_url = "https://algoryn-dots-ocr-idcard.hf.space"16    print(f"๐Ÿ” Testing Production API at {api_url}")17    18    # Health check19    try:20        print("๐Ÿ“ก Checking API health...")21        health_response = requests.get(f"{api_url}/health", timeout=10)22        health_response.raise_for_status()23        health_data = health_response.json()24        print(f"โœ… Health check passed: {health_data}")25    except Exception as e:26        print(f"โŒ Health check failed: {e}")27        return False28    29    # Test with front image30    front_image = Path(__file__).parent / "tom_id_card_front.jpg"31    if not front_image.exists():32        print(f"โŒ Test image not found: {front_image}")33        return False34    35    print(f"๐Ÿ“ธ Testing OCR with {front_image.name}")36    37    try:38        with open(front_image, 'rb') as f:39            files = {'file': f}40            response = requests.post(41                f"{api_url}/v1/id/ocr",42                files=files,43                timeout=60  # Longer timeout for production44            )45            response.raise_for_status()46            result = response.json()47        48        print(f"โœ… OCR test passed")49        print(f"   Request ID: {result.get('request_id')}")50        print(f"   Media type: {result.get('media_type')}")51        print(f"   Processing time: {result.get('processing_time'):.2f}s")52        print(f"   Detections: {len(result.get('detections', []))}")53        54        # Show extracted fields55        for i, detection in enumerate(result.get('detections', [])):56            fields = detection.get('extracted_fields', {})57            field_count = len([f for f in fields.values() if f is not None])58            print(f"   Page {i+1}: {field_count} fields extracted")59            60            # Show some key fields61            key_fields = ['document_number', 'surname', 'given_names', 'nationality']62            for field in key_fields:63                if field in fields and fields[field] is not None:64                    value = fields[field].get('value', 'N/A') if isinstance(fields[field], dict) else str(fields[field])65                    confidence = fields[field].get('confidence', 'N/A') if isinstance(fields[field], dict) else 'N/A'66                    print(f"     {field}: {value} (confidence: {confidence})")67        68        return True69        70    except Exception as e:71        print(f"โŒ OCR test failed: {e}")72        if hasattr(e, 'response') and e.response is not None:73            print(f"   Status code: {e.response.status_code}")74            print(f"   Response: {e.response.text}")75        return False76 77if __name__ == "__main__":78    success = test_production_api()79    sys.exit(0 if success else 1)80