CoolFace
Apppublic

algoryn/dots-ocr-idcard

sourceHugging Faceotherupdated 1y agoView on Hugging Face
0likes
quick_test.py76 linesDownload Raw Back to scripts
1#!/usr/bin/env python32"""Quick API Test Script3 4A simple script to quickly test the deployed Dots.OCR API endpoint.5"""6 7import requests8import json9import sys10from pathlib import Path11 12def test_api(base_url="http://localhost:7860"):13    """Quick test of the API endpoint."""14    15    print(f"๐Ÿ” Testing API at {base_url}")16    17    # Health check18    try:19        health_response = requests.get(f"{base_url}/health", timeout=10)20        health_response.raise_for_status()21        health_data = health_response.json()22        print(f"โœ… Health check passed: {health_data}")23    except Exception as e:24        print(f"โŒ Health check failed: {e}")25        return False26    27    # Test with front image28    front_image = Path(__file__).parent / "tom_id_card_front.jpg"29    if not front_image.exists():30        print(f"โŒ Test image not found: {front_image}")31        return False32    33    print(f"๐Ÿ“ธ Testing with {front_image.name}")34    35    try:36        with open(front_image, 'rb') as f:37            files = {'file': f}38            response = requests.post(39                f"{base_url}/v1/id/ocr",40                files=files,41                timeout=3042            )43            response.raise_for_status()44            result = response.json()45        46        print(f"โœ… OCR test passed")47        print(f"   Request ID: {result.get('request_id')}")48        print(f"   Media type: {result.get('media_type')}")49        print(f"   Processing time: {result.get('processing_time'):.2f}s")50        print(f"   Detections: {len(result.get('detections', []))}")51        52        # Show extracted fields53        for i, detection in enumerate(result.get('detections', [])):54            fields = detection.get('extracted_fields', {})55            field_count = len([f for f in fields.values() if f is not None])56            print(f"   Page {i+1}: {field_count} fields extracted")57            58            # Show some key fields59            key_fields = ['document_number', 'surname', 'given_names', 'nationality']60            for field in key_fields:61                if field in fields and fields[field] is not None:62                    value = fields[field].get('value', 'N/A') if isinstance(fields[field], dict) else str(fields[field])63                    confidence = fields[field].get('confidence', 'N/A') if isinstance(fields[field], dict) else 'N/A'64                    print(f"     {field}: {value} (confidence: {confidence})")65        66        return True67        68    except Exception as e:69        print(f"โŒ OCR test failed: {e}")70        return False71 72if __name__ == "__main__":73    base_url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:7860"74    success = test_api(base_url)75    sys.exit(0 if success else 1)76