|
|
""" |
|
|
OneOCR — Cross-platform OCR using Microsoft OneOCR engine. |
|
|
|
|
|
Available backends (auto-selected): |
|
|
1. OcrEngine: Windows-only DLL wrapper (100% accuracy, fastest) |
|
|
2. OcrEngineUnified: Auto-selects best backend (DLL → Wine → ONNX) |
|
|
3. OcrEngineOnnx: Cross-platform ONNX reimplementation (~53% match rate) |
|
|
|
|
|
Usage: |
|
|
python main.py <image_path> |
|
|
python main.py # uses test.png |
|
|
python main.py --backend dll # force DLL backend |
|
|
python main.py --backend wine # force Wine backend (Linux) |
|
|
python main.py --backend onnx # force ONNX backend |
|
|
""" |
|
|
|
|
|
import argparse |
|
|
import sys |
|
|
from pathlib import Path |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
def main(): |
|
|
parser = argparse.ArgumentParser(description="OneOCR — Cross-platform OCR") |
|
|
parser.add_argument("image", nargs="?", default="test.png", help="Image path") |
|
|
parser.add_argument("--backend", "-b", choices=["dll", "wine", "onnx", "auto"], |
|
|
default="auto", help="OCR backend (default: auto)") |
|
|
parser.add_argument("--output", "-o", help="Save results to JSON file") |
|
|
args = parser.parse_args() |
|
|
|
|
|
if not Path(args.image).exists(): |
|
|
print(f"Image not found: {args.image}") |
|
|
print(f"Usage: python main.py <image_path>") |
|
|
sys.exit(1) |
|
|
|
|
|
img = Image.open(args.image) |
|
|
print(f"Image: {args.image} ({img.size[0]}x{img.size[1]})") |
|
|
print() |
|
|
|
|
|
|
|
|
from ocr.engine_unified import OcrEngineUnified |
|
|
|
|
|
force = args.backend if args.backend != "auto" else None |
|
|
engine = OcrEngineUnified(force_backend=force) |
|
|
result = engine.recognize_pil(img) |
|
|
|
|
|
print(f"=== Backend: {engine.backend_name.upper()} ===") |
|
|
print(f"Text: {result.text}") |
|
|
print(f"Lines: {len(result.lines)}, Confidence: {result.average_confidence:.1%}") |
|
|
if result.text_angle is not None: |
|
|
print(f"Angle: {result.text_angle:.1f}") |
|
|
print() |
|
|
|
|
|
for i, line in enumerate(result.lines): |
|
|
words = " | ".join( |
|
|
f"{w.text} ({w.confidence:.0%})" for w in line.words |
|
|
) |
|
|
print(f" L{i}: {words}") |
|
|
|
|
|
|
|
|
if args.output: |
|
|
import json |
|
|
data = { |
|
|
"backend": engine.backend_name, |
|
|
"text": result.text, |
|
|
"text_angle": result.text_angle, |
|
|
"lines": [ |
|
|
{ |
|
|
"text": line.text, |
|
|
"words": [ |
|
|
{"text": w.text, "confidence": w.confidence} |
|
|
for w in line.words |
|
|
] |
|
|
} |
|
|
for line in result.lines |
|
|
], |
|
|
} |
|
|
Path(args.output).write_text(json.dumps(data, indent=2, ensure_ascii=False)) |
|
|
print(f"\nResults saved to {args.output}") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|