Hello, world!

In this article, you will learn how to extract text from an image file using Aspose.OCR Cloud REST API.

You will need

  • Any system with Internet connection.
  • Some image with English text. You can take the one from this article.
  • A web browser.
  • Any tool that allows you to make REST API calls, such as cURL or Postman. This article provides cURL examples.
  • 15 minutes of spare time.

Get an access token

The Aspose.OCR Cloud API is fully compliant with industry security standards and best practices. Data transfer is carried out using JWT authentication, which eliminates all possibilities of data theft or disclosure.

To obtain a JWT token, get the Client ID and Client Secret credentials:

  1. Sign in to GroupDocs Cloud API Dashboard.
  2. Go to Applications page.
  3. Create the storage. Click the plus icon and follow the required steps.
    Internal storage with 24-hour retention will suffice.
  4. Provide the application name, for example, HelloWorld.
  5. Click Save button.
  6. Click the newly created HelloWorld application and copy the values from Client Id and Client Secret fields.

Now request an access token with the following API call:

curl --request POST --location 'https://api.aspose.cloud/connect/token' \
     --header 'Content-Type: application/x-www-form-urlencoded' \
     --data-urlencode 'grant_type=client_credentials' \
     --data-urlencode 'client_id=CLIENT-ID-VALUE' \
     --data-urlencode 'client_secret=CLIENT-SECRET-VALUE'

You should get a response that looks like this:

{
	"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...HaRYOxBcCRCPLnrFCVXpw7UA",
	"expires_in": 3600,
	"token_type": "Bearer"
}

The string in access_token property will be used to authorize further calls to Aspose.OCR Cloud API.

Encode the image to Base64

An image sent to the Aspose.OCR Cloud REST API must be Base64 encoded. You can use any online tool, for example, Aspose Base64 online converter:

  1. Download the sample image
    Image to recognize
  2. Drag the image inside the box and click Convert to Base64 button.
  3. Copy Base64 string to the clipboard.

Convert the image to text

Make the following API call:

curl --request POST --location 'https://api.aspose.cloud/v5.0/ocr/RecognizeImage' \
--header 'Accept: text/plain' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...HaRYOxBcCRCPLnrFCVXpw7UA' \
--data-raw '{
  "image": "iVBORw0KGgoAAAANSUh...d1iEAAAAASUVORK5CYII=",
  "settings": {
    "language": "English"
  }
}'
  • The authorization header must contain the access token obtained earlier.
  • The image property in the request body (the value of the --data-raw command-line parameter) must contain a Base64 encoded image.

Wait a moment. The request will be placed in the queue and you will get its unique identifier. For example:

48d50d5b-8ec3-4a8b-8403-e8cfa976f3ad

Fetch the recognition result

Recognition will take a second or two, depending on the size of the image and the current Aspose.Cloud load. After the recognition is completed, the result can be obtained using the following API call:

curl --request GET --location 'https://api.aspose.cloud/v5.0/ocr/RecognizeImage?id=48d50d5b-8ec3-4a8b-8403-e8cfa976f3ad' \
--header 'Accept: text/plain' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...HaRYOxBcCRCPLnrFCVXpw7UA' \
  • The authorization header must contain the access token obtained earlier.
  • The id query string parameter must contain the unique identifier of the recognition request returned in the previous step.

When the image is recognized, you will get the following response:

{
	"id": "a197aade-bba9-4c7a-92c7-46851b3dceaa",
	"responseStatusCode": "Ok",
	"taskStatus": "Completed",
	"results": [
		{
			"type": "Text",
			"data": "QWxsIG1lbiBsaXZlIGVudmVsb3BlZCBpbiB3aGFsZS1saW5lcy4="
		}
	],
	"error": null
}

The data property contains Base64 encoded text from the image. You can decode it with any online or on-premise decoder, such as Aspose Base64 online converter. The above code returns the following string:

All men live enveloped in whale-lines.

Live sample

Ready to recognize Recognizing Drop a file here or click to browse *

* By uploading your files or using the service you agree with our Terms of use and Privacy Policy.

Recognition result
 

What’s next?

Congratulations! You have successfully recognized the text in the image. Read the Developer’s reference and API Reference for details on creating advanced optical character recognition solutions with Aspose.OCR Cloud.

You can also check Aspose.OCR Cloud GitHub repositories for code examples in various programming languages, demonstrating advanced OCR API capabilities. If you like to add or improve an example, we encourage you to contribute to the project. Fork the repository, edit the source code and create a pull request. We will review the changes and include it in the repository if found helpful.