|
2 | 2 | """ |
3 | 3 | Predict the 6-character text of an Assam tenders captcha. |
4 | 4 |
|
5 | | -Library usage: |
| 5 | +Library API: |
6 | 6 | from predict import predict, predict_with_confidence |
7 | 7 |
|
8 | 8 | text = predict("samples/captcha_0001.png") |
|
11 | 11 | text = predict(pil_image) # PIL.Image |
12 | 12 | text, conf = predict_with_confidence(image) # conf is list[float], one per character |
13 | 13 |
|
14 | | -CLI usage: |
15 | | - ./predict.py samples/captcha_0001.png |
| 14 | +The CLI lives in `manage.py predict`. |
16 | 15 | """ |
17 | 16 |
|
18 | 17 | from __future__ import annotations |
19 | 18 |
|
20 | | -import argparse |
21 | 19 | import base64 |
22 | 20 | import binascii |
23 | 21 | import io |
24 | | -import sys |
25 | 22 | from functools import cache |
26 | 23 | from pathlib import Path |
27 | 24 |
|
@@ -121,23 +118,3 @@ def predict(image: ImageInput, model_path: Path = DEFAULT_MODEL_PATH) -> str: |
121 | 118 | def predict_with_confidence(image: ImageInput, model_path: Path = DEFAULT_MODEL_PATH) -> tuple[str, list[float]]: |
122 | 119 | """Return (prediction, per-character confidence) for the captcha image.""" |
123 | 120 | return _infer(_to_rgb(image), model_path) |
124 | | - |
125 | | - |
126 | | -def main() -> int: |
127 | | - parser = argparse.ArgumentParser(description=__doc__) |
128 | | - parser.add_argument("image", help="Path to a PNG captcha") |
129 | | - parser.add_argument("--model", type=Path, default=DEFAULT_MODEL_PATH) |
130 | | - parser.add_argument("--confidence", action="store_true", help="Also print per-char confidence") |
131 | | - args = parser.parse_args() |
132 | | - |
133 | | - if args.confidence: |
134 | | - text, conf = predict_with_confidence(args.image, args.model) |
135 | | - print(text) |
136 | | - print(" ".join(f"{c}={p:.2f}" for c, p in zip(text, conf, strict=False))) |
137 | | - else: |
138 | | - print(predict(args.image, args.model)) |
139 | | - return 0 |
140 | | - |
141 | | - |
142 | | -if __name__ == "__main__": |
143 | | - sys.exit(main()) |
0 commit comments