-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr_manifest.py
151 lines (113 loc) · 4.02 KB
/
ocr_manifest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# /// script
# requires-python = ">=3.10"
# dependencies = []
# ///
import logging
import tempfile
from shutil import copyfileobj
from pathlib import Path
from collections import namedtuple
import argparse
import requests
import subprocess
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
base_dir = Path("/tmp/images")
Image = namedtuple('Image', ['label', 'uri'])
def create_image_cache() -> tempfile.TemporaryDirectory:
"""Creates temporary directory for image files."""
tmp_dir = tempfile.TemporaryDirectory()
logging.info(f"Image cache created: {tmp_dir}")
return tmp_dir
def manifest_from_uri(uri) -> dict:
try:
response = requests.get(uri)
if response.status_code != 200:
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
raise RuntimeError(f"{e}")
def tiff_from_canvas(canvas:dict) -> Image:
renderings = canvas['rendering']
label = canvas['label']
tiff_renderings = [rendering for rendering in renderings
if rendering['format'] == 'image/tiff']
try:
uri = tiff_renderings[0]['@id']
return Image(label, uri)
except IndexError as e:
raise IndexError(f"No tiffs: {e}")
def tiff_images(manifest:dict) -> list:
return [tiff_from_canvas(canvas) for canvas in
manifest['sequences'][0]['canvases']]
def download_image(image:Image, target_dir) -> Path | None:
if image.uri:
fname = Path(f"{image.label}.tif")
logging.info(f"Downloading {image.uri} as {fname}")
try:
response = requests.get(image.uri, stream=True)
response.raise_for_status()
except requests.exceptions.RequestException as e:
logging.info("Download failed.")
raise SystemExit(e)
response.raw.decode_content = True
try:
image_path = target_dir / fname
image_file = image_path.open("wb")
copyfileobj(response.raw, image_file)
image_file.close()
logging.info(f"Downloaded {fname}.")
return image_path
except OSError as e:
logging.exception(f"An OSError occured: {e}")
def download_images(manifest_uri:dict, target_dir:Path):
manifest = manifest_from_uri(manifest_uri)
if manifest:
for tiff_image in tiff_images(manifest):
download_image(tiff_image, target_dir)
def ocr_images(manifest_uri:dict, target_dir:Path):
manifest = manifest_from_uri(manifest_uri)
for tiff_image in tiff_images(manifest):
img_path = download_image(tiff_image, Path("/tmp"))
ocr_image(img_path, target_dir)
def ocr_image(image_path, target_dir):
outfile = f"{target_dir}/{image_path.stem}.txt"
command = [
"kraken",
"-i",
image_path,
outfile,
"segment",
"-bl",
"ocr",
"-m",
"catmus-print-fondue-large.mlmodel"
]
try:
logging.info(f"Beginning OCR of {image_path}")
result = subprocess.run(command, check=True, text=True, capture_output=True)
logging.info("OCR complete.")
except subprocess.CalledProcessError as e:
print(e.stderr)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("uri", help="URI of the manifest")
parser.add_argument("target_dir", help="directory in which to store image files")
args = parser.parse_args()
uri = args.uri
target_dir = args.target_dir
image_cache = create_image_cache()
try:
manifest = manifest_from_uri(uri)
tiff_uris = tiff_images(manifest)
for tiff in tiff_uris:
tiff_file_path = download_image(tiff, image_cache.name)
ocr_image(tiff_file_path, target_dir)
finally:
image_cache.cleanup()
logging.info("Image cache cleaned up.")
if __name__ == "__main__":
main()