|
|
"""Test the cross-platform ONNX OCR engine.""" |
|
|
import sys |
|
|
import time |
|
|
from pathlib import Path |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
img = Image.open(image_path) |
|
|
print(f"\n Image size: {img.size[0]}x{img.size[1]}, mode: {img.mode}") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
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() |
|
|
|