ruvector-scipix 2.0.4

Rust OCR engine for scientific documents - extract LaTeX, MathML from math equations, research papers, and technical diagrams with ONNX GPU acceleration
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# ruvector-scipix Examples

This directory contains comprehensive examples demonstrating various features and use cases of ruvector-scipix.

## Quick Start

All examples can be run using:
```bash
cargo run --example <example_name> -- [arguments]
```

## Examples Overview

### 1. Simple OCR (`simple_ocr.rs`)

**Basic OCR functionality with single image processing.**

Demonstrates:
- Loading and processing a single image
- OCR recognition
- Output in multiple formats (plain text, LaTeX)
- Confidence scores

**Usage:**
```bash
cargo run --example simple_ocr -- path/to/image.png
```

**Example Output:**
```
Plain Text: x² + 2x + 1 = 0
LaTeX: x^{2} + 2x + 1 = 0
Confidence: 95.3%
```

---

### 2. Batch Processing (`batch_processing.rs`)

**Parallel processing of multiple images with progress tracking.**

Demonstrates:
- Directory-based batch processing
- Parallel/concurrent processing
- Progress bar visualization
- Statistics and metrics
- JSON output

**Usage:**
```bash
cargo run --example batch_processing -- /path/to/images output.json
```

**Features:**
- Automatic CPU core detection for optimal parallelism
- Real-time progress visualization
- Per-file error handling
- Aggregate statistics

---

### 3. API Server (`api_server.rs`)

**REST API server for OCR processing.**

Demonstrates:
- HTTP server with Axum framework
- Single and batch image processing endpoints
- Health check endpoint
- Graceful shutdown
- CORS support
- Multipart file uploads

**Usage:**
```bash
# Start server
cargo run --example api_server

# In another terminal, test the API
curl -X POST -F "image=@equation.png" http://localhost:8080/ocr
curl http://localhost:8080/health
```

**Endpoints:**
- `GET /health` - Health check
- `POST /ocr` - Process single image
- `POST /batch` - Process multiple images

---

### 4. Streaming Processing (`streaming.rs`)

**Streaming PDF processing with real-time results.**

Demonstrates:
- Large document processing
- Streaming results as pages are processed
- Real-time progress reporting
- Incremental JSON output
- Memory-efficient processing

**Usage:**
```bash
cargo run --example streaming -- document.pdf output/
```

**Features:**
- Processes pages concurrently (4 at a time)
- Saves individual page results immediately
- Generates final document summary
- Per-page timing statistics

---

### 5. Custom Pipeline (`custom_pipeline.rs`)

**Custom OCR pipeline with preprocessing and post-processing.**

Demonstrates:
- Image preprocessing (denoising, sharpening, binarization)
- Post-processing filters
- LaTeX validation
- Confidence filtering
- Custom output formatting
- Otsu's thresholding

**Usage:**
```bash
cargo run --example custom_pipeline -- image.png
```

**Pipeline Steps:**
1. **Preprocessing:**
   - Denoising
   - Contrast enhancement
   - Sharpening
   - Binarization (Otsu's method)
   - Deskewing

2. **Post-processing:**
   - Confidence filtering
   - LaTeX validation
   - Spell checking
   - Custom formatting

---

### 6. WASM Browser Demo (`wasm_demo.html`)

**Browser-based OCR demonstration.**

Demonstrates:
- WebAssembly integration
- Browser-based image upload
- Drag-and-drop interface
- Real-time visualization
- Client-side processing

**Setup:**
```bash
# Build WASM module (when available)
wasm-pack build --target web

# Serve the demo
python3 -m http.server 8000
# Open http://localhost:8000/examples/wasm_demo.html
```

**Features:**
- Modern, responsive UI
- Drag-and-drop file upload
- Live preview
- Real-time results
- No server required (runs in browser)

---

### 7. Agent-Based Processing (`lean_agentic.rs`)

**Distributed OCR processing with agent coordination.**

Demonstrates:
- Multi-agent coordination
- Distributed task processing
- Fault tolerance
- Load balancing
- Agent statistics

**Usage:**
```bash
cargo run --example lean_agentic -- /path/to/documents
```

**Features:**
- Spawns multiple OCR agents (default: 4)
- Automatic task distribution
- Per-agent statistics
- Throughput metrics
- JSON result export

**Architecture:**
```
Coordinator
├── Agent 1 (tasks: 12)
├── Agent 2 (tasks: 15)
├── Agent 3 (tasks: 11)
└── Agent 4 (tasks: 13)
```

---

### 8. Accuracy Testing (`accuracy_test.rs`)

**OCR accuracy testing against ground truth datasets.**

Demonstrates:
- Dataset-based testing
- Multiple accuracy metrics
- Category-based analysis
- Statistical correlation
- Comprehensive reporting

**Usage:**
```bash
cargo run --example accuracy_test -- dataset.json
```

**Dataset Format:**
```json
[
  {
    "image_path": "tests/images/quadratic.png",
    "ground_truth_text": "x^2 + 2x + 1 = 0",
    "ground_truth_latex": "x^{2} + 2x + 1 = 0",
    "category": "quadratic"
  }
]
```

**Metrics Calculated:**
- **Text Accuracy** - Overall string similarity
- **Character Error Rate (CER)** - Character-level errors
- **Word Error Rate (WER)** - Word-level errors
- **LaTeX Accuracy** - LaTeX format correctness
- **Confidence Correlation** - Pearson correlation between confidence and accuracy
- **Category Breakdown** - Per-category statistics

**Example Output:**
```
Total Cases: 100
Successful: 98 (98.0%)
Average Confidence: 92.5%
Average Text Accuracy: 94.2%
Average CER: 3.1%
Average WER: 5.8%
Confidence Correlation: 0.847

Category Breakdown:
  quadratic: 25 cases, 96.3% accuracy
  linear: 30 cases, 98.1% accuracy
  calculus: 20 cases, 89.7% accuracy
```

---

## Common Patterns

### Error Handling
All examples use `anyhow::Result` for error handling:
```rust
use anyhow::{Context, Result};

fn main() -> Result<()> {
    let image = image::open(path)
        .context("Failed to open image")?;
    Ok(())
}
```

### Logging
Examples use `env_logger` for debug output:
```bash
# Run with debug logging
RUST_LOG=debug cargo run --example simple_ocr -- image.png

# Run with info logging (default)
RUST_LOG=info cargo run --example simple_ocr -- image.png
```

### Configuration
OCR engine configuration:
```rust
use ruvector_scipix::OcrConfig;

let config = OcrConfig {
    confidence_threshold: 0.7,
    max_image_size: 4096,
    enable_preprocessing: true,
    // ... other options
};
```

## Dependencies

Core dependencies used in examples:
- `anyhow` - Error handling
- `tokio` - Async runtime
- `image` - Image processing
- `serde/serde_json` - Serialization
- `indicatif` - Progress bars
- `axum` - HTTP server (api_server)
- `env_logger` - Logging

## Building Examples

Build all examples:
```bash
cargo build --examples
```

Build specific example:
```bash
cargo build --example simple_ocr
```

Run with optimizations:
```bash
cargo run --release --example batch_processing -- images/ output.json
```

## Testing Examples

Create test images:
```bash
# Create test directory
mkdir -p test_images

# Add some test images
cp /path/to/math_equation.png test_images/
```

Run examples:
```bash
# Simple OCR
cargo run --example simple_ocr -- test_images/equation.png

# Batch processing
cargo run --example batch_processing -- test_images/ results.json

# Accuracy test (requires dataset)
cargo run --example accuracy_test -- test_dataset.json
```

## Integration Guide

### Using in Your Project

1. **Add dependency:**
```toml
[dependencies]
ruvector-scipix = "0.1.0"
```

2. **Basic usage:**
```rust
use ruvector_scipix::{OcrEngine, OcrConfig};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let config = OcrConfig::default();
    let engine = OcrEngine::new(config).await?;

    let image = image::open("equation.png")?;
    let result = engine.recognize(&image).await?;

    println!("Text: {}", result.text);
    Ok(())
}
```

3. **Advanced usage:**
See individual examples for advanced patterns like:
- Custom pipelines
- Batch processing
- API integration
- Agent-based processing

## Performance Tips

1. **Batch Processing:**
   - Use parallel processing for multiple images
   - Adjust concurrency based on CPU cores
   - Enable model caching for repeated runs

2. **Memory Management:**
   - Stream large documents instead of loading all at once
   - Use appropriate image resolution (downscale if needed)
   - Clear cache periodically for long-running processes

3. **Accuracy vs Speed:**
   - Higher confidence thresholds = more accuracy, slower processing
   - Preprocessing improves accuracy but adds overhead
   - Balance based on your use case

## Troubleshooting

### Common Issues

**"Model not found"**
```bash
# Download models first
./scripts/download_models.sh
```

**"Out of memory"**
- Reduce batch size or concurrent workers
- Downscale large images before processing
- Enable streaming for PDFs

**"Low confidence scores"**
- Enable preprocessing pipeline
- Improve image quality (resolution, contrast)
- Check for skewed or rotated images

## Contributing

When adding new examples:
1. Add the `.rs` file to `examples/`
2. Update `Cargo.toml` with example entry
3. Document in this README
4. Include usage examples and expected output
5. Add error handling and logging
6. Keep examples self-contained

## Resources

- [Main Documentation]../README.md
- [API Reference]../docs/API.md
- [Model Guide]../docs/MODELS.md
- [Benchmarks]../benches/README.md

## License

All examples are provided under the same license as ruvector-scipix.