-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (53 loc) · 1.66 KB
/
main.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
import sys
import os
from subprocess import call
import argparse
valid_ext = [".jpg",".gif",".png",".tif",".bmp"]
FNULL = open(os.devnull, 'w')
class ArgumentMissingException(Exception):
def __init__(self):
print("usage: {} <dirname>".format(sys.argv[0]))
sys.exit(1)
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def check_path(path):
return bool(os.path.exists(path))
def main(input_path, output_path):
if call(['which', 'tesseract']):
print("tesseract is missing")
elif check_path(input_path):
cnt = 0
val = 0
for f in os.listdir(input_path):
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_ext:
val += 1
continue
else :
if cnt == 0:
create_dir(output_path)
cnt += 1
img_file = os.path.join(input_path, f)
filename = os.path.splitext(f)[0]
filename = ''.join(e for e in filename if e.isalnum() or e == '-')
text_file_path = os.path.join(output_path, filename)
call(["tesseract", img_file, text_file_path], stdout=FNULL)
print(str(cnt) + (" file" if cnt == 1 else " files") + " completed")
if cnt + val == 0:
print(" files not found at your given location")
else :
print(str(cnt) + " / " + str(cnt + val) + " output files")
else :
print("No directory found at " + format(input_path))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("input_dir")
parser.add_argument('output_dir', nargs='?')
args = parser.parse_args()
input_path = os.path.abspath(args.input_dir)
if args.output_dir:
output_path = os.path.abspath(args.output_dir)
else:
output_path = os.path.join(input_path,'converted-text')
main(input_path, output_path)