-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample_ocr_file.py
More file actions
52 lines (40 loc) · 1.39 KB
/
Copy pathexample_ocr_file.py
File metadata and controls
52 lines (40 loc) · 1.39 KB
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
"""最基础的 OCR 示例:加载预设并识别图片文件。"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import liteocr
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="LiteOCR 基础图片识别示例")
parser.add_argument(
"image",
nargs="?",
default="test.png",
help="待识别图片路径(默认:test.png)",
)
parser.add_argument(
"--preset",
default="PP-OCRv5_mobile",
help="使用的 OCR 预设(默认:PP-OCRv5_mobile)",
)
parser.add_argument(
"--model-dir",
default="models",
help="模型文件存放目录(默认:models)",
)
args = parser.parse_args(argv)
image_path = Path(args.image)
if not image_path.exists():
print(f"图片不存在:{image_path}", file=sys.stderr)
return 1
print(f"加载预设:{args.preset}")
engine = liteocr.Engine()
engine.load_preset(args.preset, model_dir=args.model_dir)
print(f"识别图片:{image_path}")
result = engine.recognize(str(image_path))
print(f"检测到 {len(result.boxes)} 个文本框,识别出 {len(result.lines)} 行文本:\n")
for i, line in enumerate(result.lines, 1):
print(f"{i}. {line.text}")
return 0
if __name__ == "__main__":
sys.exit(main())