-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.py
executable file
·66 lines (52 loc) · 1.81 KB
/
ocr.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
#!/usr/bin/env python3
import os
import subprocess
import logging
import uuid
from pyclowder.extractors import Extractor
import pyclowder.files
class Tesseract(Extractor):
def __init__(self):
Extractor.__init__(self)
self.setup()
logging.getLogger('pyclowder').setLevel(logging.DEBUG)
logging.getLogger('__main__').setLevel(logging.DEBUG)
def ocr(self, filename, tmpfilename):
text = ""
tmpfile = None
try:
subprocess.check_call(["tesseract", filename, tmpfilename])
tmpfile = "./" + tmpfilename + ".txt"
with open(tmpfile, 'r') as f:
text = f.read()
finally:
if tmpfile is not None and os.path.isfile(tmpfile):
os.remove(tmpfile)
return self.clean_text(text)
def clean_text(self, text):
t = ""
words = text.split()
for word in words:
w = self.clean_word(word)
if w != "":
t += w + " "
return t
def clean_word(self, word):
cw = word.strip('(){}[].,')
if cw.isalnum() and len(cw) >= 2:
return cw
else:
return ""
def process_message(self, connector, host, secret_key, resource, parameters):
inputfile = resource["local_paths"][0]
ocrtext = self.ocr(inputfile, str(uuid.uuid4())).strip()
if not ocrtext:
ocrtext = 'No text detected'
content = {'ocr_text': ocrtext}
metadata = self.get_metadata(content, "file", parameters['id'], host)
# upload metadata
pyclowder.files.upload_metadata(connector, host, secret_key, parameters['id'], metadata)
logging.info("Uploaded metadata %s", metadata)
if __name__ == "__main__":
extractor = Tesseract()
extractor.start()