Assertion Error in Python Code sample

  • 52 Views
  • Last Post 4 days ago
viji posted this 2 weeks ago

I have download the sample Python code from

https://ocrsdk.com

It's for python version 2.7, I have made necessary changes to work with python version 3.6.

But I'm getting an error with my password

Below is the line in which I'm getting error:

{ "Authorization" : "Basic %s" %("%s:%s" % base64.b64encode(bytes(self.ApplicationId,"utf-8"), bytes(self.Password,'utf-8')) ) }

 

The error message that I am getting is as follows: 

  File "D:\Anaconda3\lib\base64.py", line 60, in b64encode

    assert len(altchars) == 2, repr(altchars)

   AssertionError: b'Password'

Order By: Standard | Newest | Votes
Koen de Leijer posted this 2 weeks ago

Hi

I/we did not have any trouble getting the samples to work in both Java (your other post) and Python,
can you provide the complete source-code (excl license information) you are using?

Best regards
Koen de Leijer

viji posted this 2 weeks ago

Hi,

I tried attaching the code but its throwing error saying that file format not supported.

I downloaded the code from the sample code provided(https://ocrsdk.com/documentation/code-samples/?icon). In that I have made some changes.

The changes are as follows:

In PYTHON CODE

In AbbyyOnlineSdk.py:

In line 36:

ServerUrl = "https://cloud.ocrsdk.com/"

In line 97:

return { "Authorization" : "Basic %s" %("%s:%s" % base64.b64encode(bytes(self.ApplicationId,"utf-8"), bytes(self.Password,'utf-8')) ) }

Instead of urllib2 package, I have used urllib3 package

 

In JAVA CODE

 

In TestApp.java

 

Added the below line

String[] args1 = {"recognize","<path to pdf>","<path to docx>"};

 

In Client Settings.java:

Given the required credentials and proxy details.

 

Koen de Leijer posted this 2 weeks ago

Hi

This does not seems to be an issue with your ABBYY but with your setup.
You should not encode the username/password and it's not neccessary to use URLLib or Anaconda
We use something like the following Python-snippet (using the Python requests-library):

from __future__ import absolute_import, unicode_literals

import requests
from ABBYY import CloudOCR


class ABBYYWrapper(object):

    def __init__(self, pdf_, language_='en', settings_=None):
        self._pdf = pdf_
        self._language = language_
        self._exportFormat = 'pdfSearchable'
        self._cloudurl = "http://cloud.ocrsdk.com"
        self._cloudapplicationid = "abbyy_cloud_id"
        self._cloudpassword = "abbyy_cloud_password"

    def perform_cloud_ocr(self):
        # set file-pointer to first byte of the file
        self._pdf.seek(0)

        # create a dictionary holding  the PDF
        post_file = {'ocred_pdf': self._pdf.read()}

        # get handle to ABBYYs CloudOCR
        ocr_engine = CloudOCR(
            application_id=self._cloudapplicationid,
            password=self._cloudpassword)

        # override URL of ABBYYs CloudOCR
        ocr_engine.base_url = self._cloudurl

        # process the PDF and download/return the result
        result = ocr_engine.process_and_download(
            file=post_file,
            exportFormat=self._exportFormat,
            language=self._language)
        return result

viji posted this 2 weeks ago

Hi,

I tried the code snippet given by you. But I'm getting 401 Client error

I had given the necessary proxy settings.

The error is as follows:

 File "temp.py", line 47, in perform_cloud_ocr

    language=self._language)

  File "D:\Anaconda3\lib\site-packages\ABBYY\cloud_ocr.py", line 104, in process_and_download

    task = self.processImage(file=file, **kwargs)

  File "D:\Anaconda3\lib\site-packages\ABBYY\cloud_ocr.py", line 20, in processImage

    return self._postfile('processImage', file, **kwargs).pop()

  File "D:\Anaconda3\lib\site-packages\ABBYY\cloud_ocr.py", line 60, in _postfile

    return self._process_reply(reply)

  File "D:\Anaconda3\lib\site-packages\ABBYY\cloud_ocr.py", line 68, in _process_reply

    reply.raise_for_status()

  File "D:\Anaconda3\lib\site-packages\requests\models.py", line 935, in raise_for_status

    raise HTTPError(http_error_msg, response=self)

requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://cloud.ocrsdk.com/processImage?exportFormat=pdfSearchable&language=en

Koen de Leijer posted this 2 weeks ago

Hi

Please check your credentials (abbyy_cloud_id and abbyy_cloud_password)

Best regards

Koen de Leijer

viji posted this 4 days ago

Hi,

I have checked my credentials and that is right. But still I'm getting the below error:

  HTTPError: 401 Client Error: Unauthorized for url: http://cloud.ocrsdk.com/processImage?exportFormat=doc&language=en

 

Oksana Serdyuk posted this 4 days ago

Please send us to CloudOCRSDK@abbyy.com the logs from the Fiddler debugger (you can download the Fiddler debugger for free here: www.telerik.com/fiddler).

Close