forked from bansii1/Invoice-Data-Extraction-using-ABBYY
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
113 lines (85 loc) · 3.45 KB
/
Copy pathprocess.py
File metadata and controls
113 lines (85 loc) · 3.45 KB
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
#!/usr/bin/python
# Usage: process.py <input file> <output file> [-l <Language>] [-pdf|-txt|-rtf|-docx|-xml]
import argparse
import os
import time
from AbbyyOnlineSdk import *
processor = None
def setup_processor():
if "ABBYY_APPID" in os.environ:
processor.ApplicationId = os.environ["ABBYY_APPID"]
print("ABBYY_APPID", processor.ApplicationId)
if "ABBYY_PWD" in os.environ:
processor.Password = os.environ["ABBYY_PWD"]
print("ABBYY_PWD",processor.Password)
# Proxy settings
if "http_proxy" in os.environ:
proxy_string = os.environ["http_proxy"]
print("Using http proxy at {}".format(proxy_string))
processor.Proxies["http"] = proxy_string
if "https_proxy" in os.environ:
proxy_string = os.environ["https_proxy"]
print("Using https proxy at {}".format(proxy_string))
processor.Proxies["https"] = proxy_string
# Recognize a file at filePath and save result to resultFilePath
def recognize_file(file_path, result_file_path, language, output_format):
print("Uploading..")
settings = ProcessingSettings()
settings.Language = language
settings.OutputFormat = output_format
task = processor.process_image(file_path, settings)
if task is None:
print("Error")
return
if task.Status == "NotEnoughCredits":
print("Not enough credits to process the document. Please add more pages to your application's account.")
return
print("Id = {}".format(task.Id))
print("Status = {}".format(task.Status))
# Wait for the task to be completed
print("Waiting..")
# Note: it's recommended that your application waits at least 2 seconds
# before making the first getTaskStatus request and also between such requests
# for the same task. Making requests more often will not improve your
# application performance.
# Note: if your application queues several files and waits for them
# it's recommended that you use listFinishedTasks instead (which is described
# at http://ocrsdk.com/documentation/apireference/listFinishedTasks/).
while task.is_active():
time.sleep(5)
print(".")
task = processor.get_task_status(task)
print("Status = {}".format(task.Status))
if task.Status == "Completed":
if task.DownloadUrl is not None:
processor.download_result(task, result_file_path)
print("Result was written to {}".format(result_file_path))
else:
print("Error processing task")
def create_parser():
parser = argparse.ArgumentParser(description="Recognize a file via web service")
parser.add_argument('source_file')
parser.add_argument('target_file')
parser.add_argument('-l', '--language', default='English', help='Recognition language (default: %(default)s)')
group = parser.add_mutually_exclusive_group()
group.add_argument('-txt', action='store_const', const='txt', dest='format', default='txt')
group.add_argument('-pdf', action='store_const', const='pdfSearchable', dest='format')
group.add_argument('-rtf', action='store_const', const='rtf', dest='format')
group.add_argument('-docx', action='store_const', const='docx', dest='format')
group.add_argument('-xml', action='store_const', const='xml', dest='format')
return parser
def main():
global processor
processor = AbbyyOnlineSdk()
setup_processor()
args = create_parser().parse_args()
source_file = args.source_file
target_file = args.target_file
language = args.language
output_format = args.format
if os.path.isfile(source_file):
recognize_file(source_file, target_file, language, output_format)
else:
print("No such file: {}".format(source_file))
if __name__ == "__main__":
main()