-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.py
94 lines (72 loc) · 2.91 KB
/
process.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
#!/usr/bin/python
# Usage: process.py <input file> <output file> [-language <Language>] [-pdf|-txt|-rtf|-docx|-xml]
import argparse
import base64
import getopt
import MultipartPostHandler
import os
import re
import sys
import time
import urllib2
import urllib
from AbbyyOnlineSdk import *
processor = AbbyyOnlineSdk()
if "ABBYY_APPID" in os.environ:
processor.ApplicationId = os.environ["ABBYY_APPID"]
if "ABBYY_PWD" in os.environ:
processor.Password = os.environ["ABBYY_PWD"]
# Proxy settings
if "http_proxy" in os.environ:
proxyString = os.environ["http_proxy"]
print "Using proxy at %s" % proxyString
processor.Proxy = urllib2.ProxyHandler( { "http" : proxyString })
# Recognize a file at filePath and save result to resultFilePath
def recognizeFile( filePath, resultFilePath, language, outputFormat ):
print "Uploading.."
settings = ProcessingSettings()
settings.Language = language
settings.OutputFormat = outputFormat
task = processor.ProcessImage( filePath, settings )
if task == None:
print "Error"
return
print "Id = %s" % task.Id
print "Status = %s" % task.Status
# Wait for the task to be completed
sys.stdout.write( "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.IsActive() == True :
time.sleep( 5 )
sys.stdout.write( "." )
task = processor.GetTaskStatus( task )
print "Status = %s" % task.Status
if task.Status == "Completed":
if task.DownloadUrl != None:
processor.DownloadResult( task, resultFilePath )
print "Result was written to %s" % resultFilePath
else:
print "Error processing task"
parser = argparse.ArgumentParser( description="Recognize a file via web service" )
parser.add_argument( 'sourceFile' )
parser.add_argument( 'targetFile' )
parser.add_argument( '-l', '--language', default='English', help='Recognition language (default: %(default))' )
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' )
args = parser.parse_args()
sourceFile = args.sourceFile
targetFile = args.targetFile
language = args.language
outputFormat = args.format
if os.path.isfile( sourceFile ):
recognizeFile( sourceFile, targetFile, language, outputFormat )