-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_interface.py
69 lines (58 loc) · 2.11 KB
/
cli_interface.py
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
import argparse
from pathlib import Path
from PIL import Image, UnidentifiedImageError
import logging
import json
from ocr_detection.image_handler import ImageTextDetector
def parse_args():
"""
Parse command line arguments
"""
cli = argparse.ArgumentParser()
cli.add_argument("--model", "-m", default="", help="OCR model.")
cli.add_argument("--output_dir", "-o", default="", help="Directory to store JSON results.")
cli.add_argument("--images", "-i", nargs="+", type=str, help="Filepaths to image(s) from which to extract text.")
return cli.parse_args()
if __name__ == "__main__":
args = parse_args()
if not args.model:
print("Must specify --model.")
exit(1)
if not args.images:
print("Must specify at least one image.")
exit(1)
output_dir = Path(args.output_dir)
try:
detector = ImageTextDetector(logger=logging)
except ValueError as e:
print(e)
exit(1)
for image in args.images:
# Check if image exists and can be opened
image_path = Path(image)
error = None
if not image_path.exists():
print(f"Image does not exist: {image}")
error = "Image does not exist"
else:
try:
with Image.open(image):
pass
except Exception as e:
print(f"Error opening image: {image}")
error = str(e)
if error:
if output_dir:
with open(output_dir.joinpath(image_path.with_suffix(".json").name), "w") as out_file:
out_file.write(json.dumps({
"filename": image_path.name,
'model_type': args.model,
"success": False,
"error": error
}))
continue
# Process image
prediction = detector.process_image(image, args.model, local=True)
if output_dir:
with open(output_dir.joinpath(image_path.with_suffix(".json").name), "w") as out_file:
out_file.write(json.dumps(prediction))