Queue - Python

This tutorial shows how to queue tasks using the LEADTOOLS Cloud Services in a Python application.

Overview  
Summary This tutorial covers how to make Queue requests and process the results using the LEADTOOLS Cloud Services in a Python application.
Completion Time 30 minutes
Project Download tutorial project (7 KB)
Platform LEADTOOLS Cloud Services API
IDE Visual Studio
Language Python
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Be sure to review the following sites for information about LEADTOOLS Cloud Services API.

Application ID and Password

Create an Account with LEADTOOLS Hosted Cloud Services to obtain both Application ID and Password strings.

Service Plans

LEADTOOLS Service Plan offerings:

Service Plan Description
Free Trial Free Evaluation
Page Packages Prepaid Page Packs
Subscriptions Prepaid Monthly Processed Pages

To further explore the offerings, refer to the LEADTOOLS Hosted Cloud Services page.

To obtain the necessary Application ID and Application Password, refer to Create an Account and Application with the LEADTOOLS Hosted Cloud Services.

Add the Queue Code

With the project created and the requests package added, coding can begin.

In the Solution Explorer, open Queue.py. Add the following variables at the top.

Note

Where it states Replace with Application ID and Replace with Application Password, be sure to place your Application ID and Password accordingly.

# Simple script to make and process the results of a Queue request to the LEADTOOLS CloudServices. 
import requests 
import sys 
import time 
 
servicesUrl = "https://azure.leadtools.com/api/" 
 
# The application ID. 
appId = "Replace with Application ID" 
 
# The application password. 
password = "Replace with Application Password" 
 
# The first page in the file to mark for processing 
firstPage = 1 
 
# Sending a value of -1 will indicate to the services that the rest of the pages in the file should be processed. 
lastPage = -1 
 
# Enum corresponding to the output format for the file. We will be converting a file to tif. 
fileFormat = 4 
 
# We will be uploading the file via a URl.  Files can also be passed by adding a PostFile to the request.  Only 1 file will be accepted per request. 
# The services will use the following priority when determining what a request is trying to do GUID > URL > Request Body Content 
fileURL = 'http://demo.leadtools.com/images/cloud_samples/ocr1-4.tif' 
baseUploadUrl = '{}UploadFile?fileurl={}' 
formattedUploadUrl = baseUploadUrl.format(servicesUrl, fileURL) 

Add a request.post call to process an UploadFile request and capture the GUID from the resulting request.text and provide it to the next section. This sends an UploadFile request to the LEADTOOLS Cloud Services API, if successful, a unique identifier (GUID) will be returned and then a query using this GUID will be made.

request = requests.post(formattedUploadUrl, auth=(appId, password)) 
 
# If uploading a file alongside the HTTP request 
#baseUploadUrl ='{}UploadFile' 
#formattedUploadUrl = baseUploadUrl.format(servicesUrl) 
#file = {'file' : open('path/to/file', 'rb')} 
#request = requests.post( 
#    formattedRecognitionUrl, auth=(appId, password), files = file) 
 
if request.status_code != 200: 
    print("Error sending the conversion request") 
    print(request.text) 
    sys.exit() 
 
# The File has been uploaded successfully.  Now that we have the GUID, we can use it to queue up requests for the file 
guid = request.text 
print("Unique ID returned by the services: " + guid + "\n") 

Next, confirm the file uploaded by submitting a Query request utilizing the saved GUID.

baseQueryUrl = '{}Query?id={}' 
formattedQueryUrl = baseQueryUrl.format(servicesUrl, guid) 
while True:  # Poll the services to determine if the request has finished processing 
    request = requests.post(formattedQueryUrl, auth=(appId, password)) 
    returnedData = request.json() 
    if returnedData['FileStatus'] != 123: 
        break 
    time.sleep(5) 

Once confirmed, actions can be queued up to perform against the uploaded file. Submit ExtractText and Convert requests to be queued with the provided GUID.

baseRecognitionUrl = '{}Recognition/ExtractText?firstPage={}&lastPage={}&guid={}' 
formattedRecognitionUrl = baseRecognitionUrl.format( 
    servicesUrl, firstPage, lastPage, guid) 
request = requests.post(formattedRecognitionUrl, auth=(appId, password)) 
if request.status_code != 200: 
    print("Error sending the conversion request") 
    print(request.text) 
    sys.exit() 
print("ExtractText queued successfully") 
 
baseConversionUrl = '{}Conversion/Convert?firstPage={}&lastPage={}&format={}&guid={}' 
formattedConversionUrl = baseConversionUrl.format( 
    servicesUrl, firstPage, lastPage, fileFormat, guid) 
request = requests.post(formattedConversionUrl, auth=(appId, password)) 
if request.status_code != 200: 
    print("Error sending the conversion request") 
    print(request.text) 
    sys.exit() 
 
print("Conversion queued successfully") 

Then initialize the queue for processing with a Run request, again utilizing the GUID.

baseRunUrl = '{}Run?id={}' 
formattedRunUrl = baseRunUrl.format(servicesUrl, guid) 
request = requests.post(formattedRunUrl, auth=(appId, password)) 
if request.status_code != 200: 
    print("Error sending the conversion request") 
    print(request.text) 
    sys.exit() 
 
print("File has successfully been marked to run") 

To confirm the Run request completed successfully, create a Query. If successful the response will contain all the results of the ExtractText and Convert requests in JSON format.

while True:  # Poll the services to determine if the request has finished processing 
    request = requests.post(formattedQueryUrl, auth=(appId, password)) 
    returnedData = request.json() 
    if returnedData['FileStatus'] != 100: 
        break 
    time.sleep(5) 
 
print("File finished processing with file status: " + 
      str(returnedData['FileStatus'])) 
 
if returnedData['FileStatus'] != 200: 
    sys.exit() 

Finally, parse the JSON data into a readable format.

try: 
    print("Results:") 
    returnedJson = returnedData['RequestData'] 
    for requestObject in returnedJson: 
        print("Service Type: " + requestObject['ServiceType']) 
        if requestObject['ServiceType'] == 'Conversion': 
            print("Returned URLS:") 
            for url in requestObject['urls']: 
                print(url) 
        elif requestObject['ServiceType'] == 'Recognition' and requestObject['RecognitionType'] == 'Text': 
            print("Extract Text URL: " + requestObject['data']) 
    sys.exit() 
except Exception as e: 
    print("Failed to Parse JSON") 
    print(str(e)) 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application displays the extracted text information and converted file link from the returned JSON data.

Queued Tasks Information

Wrap-up

This tutorial showed how to queue tasks via the LEADTOOLS Cloud Services API.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.