load image from url instead of reading from local file

  • 124 Views
  • Last Post 04 April 2017
jaamiadam posted this 03 April 2017

I do not want to use code for reading or writing to disk.

The Line in sample python code that open the file from local disk

recognizeFile( sourceFile, targetFile, language, outputFormat )

The  sourceFile parameter must be the image file on local disk that is uploaded to cloud OCR service. If i use url instead of the file path i get errors.

Please give some ideas to use url instead of file path.

 

Order By: Standard | Newest | Votes
jaamiadam posted this 04 April 2017

I changed code to give url instead of source file path but i receive following errors

Uploading..
Traceback (most recent call last):
File "process.py", line 94, in <module>
UploadFile( sourceFile, targetFile, language, outputFormat )
File "process.py", line 41, in UploadFile
task = processor.ProcessImage( filePath, settings )
File "C:\work\IBWORK\myProj\AbbyyOnlineSdk.py", line 55, in ProcessImage
response = self.getOpener().open(request, bodyParams).read()
File "C:\Python27\lib\urllib2.py", line 437, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 550, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 475, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 558, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

 

# I CHANGED AbbyyOnlineSdk.py

def ProcessImage( self, filePath, settings ):
   urlParams = urllib.urlencode({
      "language" : settings.Language,
      "exportFormat" : settings.OutputFormat
   })

   requestUrl = self.ServerUrl + "processImage?" + urlParams

   # I MARKED FOLLOWING LINE

   #bodyParams = { "file" : open( filePath, "rb" ) }

   # I USED FOLLOWING LINE 
   bodyParams = { "file" : urllib2.urlopen(filePath).read() }

   request = urllib2.Request( requestUrl, None, self.buildAuthInfo() )
   response = self.getOpener().open(request, bodyParams).read()
   if response.find( '<Error>' ) != -1 :
   return None

   # Any response other than HTTP 200 means error - in this case exception will be thrown

   # parse response xml and extract task ID
   task = self.DecodeResponse( response )
   return task

 

# IN process.py  I COMMENTED FOLLOWING LINES
'''if os.path.isfile( sourceFile ):
UploadFile( sourceFile, targetFile, language, outputFormat )
else:
print "No such file: %s" % sourceFile
'''

# I USED FOLLOWING LINE

UploadFile( sourceFile, targetFile, language, outputFormat )

 

# I EXECUTE PROGRAM WITH FOLLOWING PARAMETERS

python process.py http://iitp.org.pk/data/pass/rz-passport.png rzuk.txt -l Englih -txt

 

Oksana Serdyuk posted this 04 April 2017

I have answered you by email.

Close