File size: 2,568 Bytes
ce847d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Test the cross-platform ONNX OCR engine."""
import sys
import time
from pathlib import Path
from PIL import Image

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))

from ocr.engine_onnx import OcrEngineOnnx


def main():
    image_path = sys.argv[1] if len(sys.argv) > 1 else "image.png"
    
    print("=" * 70)
    print("  ONEOCR Cross-Platform Pipeline Test")
    print(f"  Image: {image_path}")
    print("=" * 70)
    
    # Load image
    img = Image.open(image_path)
    print(f"\n  Image size: {img.size[0]}x{img.size[1]}, mode: {img.mode}")
    
    # Create engine
    print("\n[1/2] Loading ONNX engine...")
    t0 = time.perf_counter()
    engine = OcrEngineOnnx()
    t_load = time.perf_counter() - t0
    print(f"  Engine loaded in {t_load:.2f}s")
    
    # Run OCR
    print("\n[2/2] Running full pipeline...")
    t0 = time.perf_counter()
    result = engine.recognize_pil(img)
    t_ocr = time.perf_counter() - t0
    print(f"  Pipeline completed in {t_ocr:.2f}s")
    
    # Display results
    print("\n" + "=" * 70)
    print("  RESULTS")
    print("=" * 70)
    
    if result.error:
        print(f"\n  ERROR: {result.error}")
        return
    
    print(f"\n  Text angle: {result.text_angle:.1f}°")
    print(f"  Lines detected: {len(result.lines)}")
    print(f"  Avg confidence: {result.average_confidence:.1%}")
    
    print(f"\n  Full text:")
    print(f"  {'─' * 50}")
    for line in result.lines:
        print(f"    {line.text}")
    print(f"  {'─' * 50}")
    
    # Detailed word-level output
    print(f"\n  Word-level details:")
    for i, line in enumerate(result.lines):
        print(f"\n  Line {i+1}: \"{line.text}\"")
        if line.bounding_rect:
            b = line.bounding_rect
            print(f"    bbox: ({b.x1:.0f},{b.y1:.0f}) → ({b.x3:.0f},{b.y3:.0f})")
        for j, word in enumerate(line.words):
            conf_bar = "█" * int(word.confidence * 20) + "░" * (20 - int(word.confidence * 20))
            bbox_str = ""
            if word.bounding_rect:
                b = word.bounding_rect
                bbox_str = f" @ ({b.x1:.0f},{b.y1:.0f})→({b.x3:.0f},{b.y3:.0f})"
            print(f"    [{j}] \"{word.text}\" conf={word.confidence:.3f} {conf_bar}{bbox_str}")
    
    print(f"\n{'=' * 70}")
    print(f"  Summary: {len(result.lines)} lines, "
          f"{sum(len(l.words) for l in result.lines)} words, "
          f"conf={result.average_confidence:.1%}, "
          f"time={t_ocr:.2f}s")
    print(f"{'=' * 70}")


if __name__ == "__main__":
    main()