File size: 6,216 Bytes
211e423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34c6057
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Dots.OCR API Testing

This directory contains comprehensive testing scripts for the Dots.OCR API endpoint.

## Test Scripts

### 1. `test_api_endpoint.py` - Comprehensive API Testing

The main testing script that provides full API validation capabilities.

**Features:**
- Health check validation
- Single and multiple image testing
- ROI (Region of Interest) testing
- Field extraction validation
- Response structure validation
- Performance metrics
- Detailed error reporting

**Usage:**
```bash
# Basic test with default settings
python test_api_endpoint.py

# Test with custom API URL
python test_api_endpoint.py --url https://your-api.example.com

# Test with ROI
python test_api_endpoint.py --roi '{"x1": 0.1, "y1": 0.1, "x2": 0.9, "y2": 0.9}'

# Test with specific expected fields
python test_api_endpoint.py --expected-fields document_number surname given_names

# Verbose output
python test_api_endpoint.py --verbose

# Custom timeout
python test_api_endpoint.py --timeout 60
```

**Options:**
- `--url`: API base URL (default: http://localhost:7860)
- `--timeout`: Request timeout in seconds (default: 30)
- `--roi`: ROI coordinates as JSON string
- `--expected-fields`: List of expected field names to validate
- `--verbose`: Enable verbose logging

### 2. `quick_test.py` - Quick Validation

A simple script for quick API validation after deployment.

**Usage:**
```bash
# Test local API
python quick_test.py

# Test remote API
python quick_test.py https://your-api.example.com
```

## Test Configuration

### `test_config.json`

Configuration file for test parameters and thresholds.

**Configuration sections:**
- `api_endpoints`: Different API URLs for various environments
- `test_images`: List of test image files
- `expected_fields`: Fields that should be extracted
- `roi_test_cases`: Different ROI configurations to test
- `performance_thresholds`: Performance validation criteria
- `test_timeout`: Default timeout for requests

## Test Images

The following test images are used for validation:

- `tom_id_card_front.jpg` - Front of Dutch ID card
- `tom_id_card_back.jpg` - Back of Dutch ID card

## Testing Scenarios

### 1. Basic Functionality Test
```bash
python test_api_endpoint.py
```
Tests basic API functionality with default settings.

### 2. ROI Testing
```bash
python test_api_endpoint.py --roi '{"x1": 0.25, "y1": 0.25, "x2": 0.75, "y2": 0.75}'
```
Tests Region of Interest cropping functionality.

### 3. Field Validation Test
```bash
python test_api_endpoint.py --expected-fields document_number surname given_names nationality
```
Tests that specific fields are extracted correctly.

### 4. Performance Test
```bash
python test_api_endpoint.py --timeout 60 --verbose
```
Tests API performance with extended timeout and detailed logging.

## Expected Results

### Successful Test Output
```
πŸ” Checking API health...
βœ… API is healthy: {'status': 'healthy', 'version': '1.0.0', 'model_loaded': True}
πŸš€ Starting API tests with 2 images...
βœ… tom_id_card_front.jpg: 2.45s
βœ… tom_id_card_back.jpg: 1.23s
πŸ“Š Test Results:
   Total images: 2
   Successful: 2
   Failed: 0
   Success rate: 100.0%
   Average processing time: 1.84s
πŸŽ‰ All tests completed successfully!
```

### Field Extraction Example
```
Page 1: 11 fields extracted
  document_number: NLD123456789 (confidence: 0.90)
  surname: MULDER (confidence: 0.90)
  given_names: THOMAS JAN (confidence: 0.90)
  nationality: NLD (confidence: 0.95)
  date_of_birth: 15-03-1990 (confidence: 0.90)
  gender: M (confidence: 0.95)
```

## Troubleshooting

### Common Issues

1. **Connection Refused**
   - Check if the API is running
   - Verify the correct URL and port
   - Check firewall settings

2. **Timeout Errors**
   - Increase timeout with `--timeout` parameter
   - Check API performance and resource usage

3. **Missing Fields**
   - Verify test images contain the expected text
   - Check field extraction patterns in the code
   - Review API logs for processing errors

4. **Validation Errors**
   - Check API response format
   - Verify model is loaded correctly
   - Review error logs for details

### Debug Mode

Enable verbose logging for detailed debugging:
```bash
python test_api_endpoint.py --verbose
```

## Integration with CI/CD

The test scripts can be integrated into CI/CD pipelines:

```yaml
# Example GitHub Actions step
- name: Test API Endpoint
  run: |
    python scripts/test_api_endpoint.py --url ${{ env.API_URL }} --timeout 60
```

## Performance Monitoring

The scripts provide performance metrics that can be used for monitoring:

- Processing time per image
- Success rate
- Field extraction accuracy
- Response validation results

These metrics can be integrated with monitoring systems like Prometheus or DataDog.

## πŸš€ Production API Testing

### Current Production Endpoint
- **URL**: https://algoryn-dots-ocr-idcard.hf.space
- **Health Check**: https://algoryn-dots-ocr-idcard.hf.space/health
- **API Docs**: https://algoryn-dots-ocr-idcard.hf.space/docs

### Quick Production Test
```bash
# Test production API
./run_tests.sh -e production

# Quick test with curl (no Python dependencies)
./test_production_curl.sh
```

### Staging Environment
- **Staging URL**: https://algoryn-dots-ocr-idcard-staging.hf.space (to be created)
- **Purpose**: Safe testing before production deployment

### Environment-Specific Testing
```bash
# Test different environments
./run_tests.sh -e local      # Local development
./run_tests.sh -e staging    # Staging environment
./run_tests.sh -e production # Production environment
```

---

### 5. `test_debug_ocr.sh` - Per-request debug logging via curl

Use this for quick, dependency-light testing of the server-side debug mode that prints OCR snippets, extracted fields, and MRZ details to logs.

**Usage:**
```bash
# Local server (per-request debug on)
./test_debug_ocr.sh -u http://localhost:7860 -f tom_id_card_front.jpg -d

# Hugging Face Space (replace with your Space URL)
./test_debug_ocr.sh -u https://<your-space>.hf.space -f tom_id_card_front.jpg -d \
  -r '{"x1":0,"y1":0,"x2":1,"y2":0.5}'
```

You can also enable debug globally on the server with `DOTS_OCR_DEBUG=1`. The script only toggles the request-level flag via `-d`.