-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_extractor.py
193 lines (159 loc) · 5.76 KB
/
text_extractor.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import base64
import gridfs
import json
import os
from time import time
from os import listdir, path
from pymongo import MongoClient
from pika import ConnectionParameters, \
BlockingConnection, \
BasicProperties
from bson import ObjectId
from tqdm import tqdm
from typing import List
_MODULE_PATH = os.path.dirname(os.path.abspath(__file__))
class TextExtractor:
CORRECTOR_QUERY = "corrector"
CORRECTOR_REPLY_QUERY = "corrector_reply"
OCR_QUERY = "ocr"
OCR_REPLY_QUERY = "ocr_reply"
DATA_PATH = os.path.join(_MODULE_PATH, "data")
def __init__(self, encoding):
self.client = MongoClient("localhost", 27017)
self.db = self.client["image_db"]
self.collection = self.db["image_collection"]
self.fs = gridfs.GridFS(self.db)
self.wait_for = None
self.progress = None
self.encoding = encoding
def add_image(self, path: str):
with open(path, "rb") as f:
img = f.read()
image_idx = self.fs.put(img)
meta = {
"imageID": image_idx,
"imagePath": path,
"recognizedText": None,
"correctedText": None
}
self.collection.insert_one(meta)
def get_unhandled(self):
return self.collection.find({"recognizedText": None})
def get_image(self, image_idx):
img = self.fs.get(image_idx)
return img.read()
def put_recognized_text(self, idx, recognized_text):
self.collection.update_one(
{"_id": ObjectId(idx)},
{
"$set": {"recognizedText": recognized_text}
}
)
def put_corrected_text(self, idx, recognized_text):
self.collection.update_one(
{"_id": ObjectId(idx)},
{
"$set": {"correctedText": recognized_text}
}
)
def reply_corrector_handler(self, ch, method, properties, body):
message = json.loads(body.decode())
self.put_corrected_text(message["_id"], message["correctedText"])
self.wait_for.remove(ObjectId(message["_id"]))
self.progress.update()
if len(self.wait_for) == 0:
ch.stop_consuming()
def reply_ocr_handler(self, ch, method, properties, body):
message = json.loads(body.decode())
if message["recognizedText"] is None:
self.wait_for.remove(ObjectId(message["_id"]))
self.progress.update()
if len(self.wait_for) == 0:
ch.stop_consuming()
return
self.put_recognized_text(message["_id"], message["recognizedText"])
ch.basic_publish(
exchange="",
routing_key=TextExtractor.CORRECTOR_QUERY,
body=json.dumps(message),
properties=BasicProperties(
reply_to=TextExtractor.CORRECTOR_REPLY_QUERY,
delivery_mode=2 # persistent message
))
def process_images(self):
connection = BlockingConnection(ConnectionParameters(
host="localhost"))
channel = connection.channel()
channel.queue_declare(
queue=TextExtractor.OCR_REPLY_QUERY,
auto_delete=True)
channel.queue_declare(
queue=TextExtractor.CORRECTOR_REPLY_QUERY,
auto_delete=True)
self.wait_for = set()
with connection, channel:
channel.basic_consume(
TextExtractor.CORRECTOR_REPLY_QUERY,
self.reply_corrector_handler,
auto_ack=True)
channel.basic_consume(
TextExtractor.OCR_REPLY_QUERY,
self.reply_ocr_handler,
auto_ack=True)
t = time()
for item in self.get_unhandled():
image = self.get_image(item["imageID"])
message = {
"_id": str(item["_id"]),
"image": base64.encodebytes(image).decode(self.encoding)
}
channel.basic_publish(
exchange="",
routing_key=TextExtractor.OCR_QUERY,
body=json.dumps(message),
properties=BasicProperties(
reply_to=TextExtractor.OCR_REPLY_QUERY))
self.wait_for.add(item["_id"])
if len(self.wait_for) > 0:
self.progress = tqdm(total=len(self.wait_for))
channel.start_consuming()
self.progress.close()
print("All images are recognized. Time: {:.2f}".format(time() - t))
def main(input_args: List[str] = None):
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers(dest='cmd')
subparser.add_parser(
"load",
help="load images from ./data to db")
subparser.add_parser(
"process",
help="recognize text from images in db")
subparser.add_parser(
"clear",
help="clear db")
if input_args is None:
args = parser.parse_args()
else:
args = parser.parse_args(input_args)
config_file_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'config.json'
)
config = json.loads(open(config_file_path, 'r').read())
extractor = TextExtractor(
config['image-bytes-encoding']
)
if args.cmd == "load":
for name in listdir(TextExtractor.DATA_PATH):
if name == '.DS_Store':
continue
extractor.add_image(path.join(TextExtractor.DATA_PATH, name))
elif args.cmd == "process":
extractor.process_images()
elif args.cmd == "clear":
extractor.collection.delete_many({})
for item in extractor.fs.find({}):
extractor.fs.delete(item._id)
if __name__ == "__main__":
main()