Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Vision OCR
  • Getting started
  • Access management
  • Pricing policy
  • Release notes
  • FAQ

In this article:

  • Getting started
  • Recognizing a text line using the OCR API
  • Recognizing a text line using the Vision API

Text line recognition

Written by
Yandex Cloud
Updated at March 28, 2025
  • Getting started
  • Recognizing a text line using the OCR API
  • Recognizing a text line using the Vision API

If you do not want to send an entire image to Vision OCR, you can cut out a single line and send it for recognition.

Getting startedGetting started

To use the examples, install cURL.

Get your account data for authentication:

Yandex or federated account
Service account
  1. Get an IAM token for your Yandex account or federated account.

  2. Get the ID of the folder for which your account has the ai.vision.user role or higher.

  3. When accessing Vision OCR via the API, provide the received parameters in each request:

    • For the Vision API and Classifier API:

      Specify the IAM token in the Authorization header as follows:

      Authorization: Bearer <IAM_token>
      

      Specify the folder ID in the request body in the folderId parameter.

    • For the OCR API:

      • Specify the IAM token in the Authorization header.
      • Specify the folder ID in the x-folder-id header.
      Authorization: Bearer <IAM_token>
      x-folder-id <folder_ID>
      

Vision OCR supports two authentication methods based on service accounts:

  • With an IAM token:

    1. Get an IAM token.

    2. Provide the IAM token in the Authorization header in the following format:

      Authorization: Bearer <IAM_token>
      
  • With API keys.

    Use API keys if requesting an IAM token automatically is not an option.

    1. Get an API key.

    2. Provide the API key in the Authorization header in the following format:

      Authorization: Api-Key <API_key>
      

Do not specify the folder ID in your requests, as the service uses the folder the service account was created in.

Recognizing a text line using the OCR APIRecognizing a text line using the OCR API

The OCR API allows you to recognize a separate line using the recognize method.

To recognize a single line of text in an image, follow the Text recognition in images guide.

Recognizing a text line using the Vision APIRecognizing a text line using the Vision API

Warning

The Vision OCR features listed below are legacy features discontinued on May 14, 2024.

The Vision API allows you to recognize a separate line using the recognize method.

In the configuration, specify the line model to recognize text lines more accurately.

Alert

The image must contain only one line of text and the text height must be at least 80% of the image height, otherwise the recognition results of the line model may be unpredictable. Example of an appropriate image:

image

To recognize a line of text:

  1. Encode the file as Base64:

    UNIX
    Windows
    PowerShell
    Python
    Node.js
    Java
    Go
    base64 -i input.jpg > output.txt
    
    C:> Base64.exe -e input.jpg > output.txt
    
    [Convert]::ToBase64String([IO.File]::ReadAllBytes("./input.jpg")) > output.txt
    
    # Import a library for encoding files in Base64.
    import base64
    
    # Create a function to encode a file and return the results.
    def encode_file(file_path):
      with open(file_path, "rb") as fid:
          file_content = fid.read()
      return base64.b64encode(file_content).decode("utf-8")
    
    // Read the file contents to memory.
    var fs = require('fs');
    var file = fs.readFileSync('/path/to/file');
    
    // Get the file contents in Base64 format.
    var encoded = Buffer.from(file).toString('base64');
    
    // Import a library for encoding files in Base64.
    import org.apache.commons.codec.binary.Base64;
    
    // Get the file contents in Base64 format.
    byte[] fileData = Base64.encodeBase64(yourFile.getBytes());
    
    import (
        "bufio"
        "encoding/base64"
        "io/ioutil"
        "os"
    )
    
    // Open the file.
    f, _ := os.Open("/path/to/file")
    
    // Read the file contents.
    reader := bufio.NewReader(f)
    content, _ := ioutil.ReadAll(reader)
    
    // Get the file contents in Base64 format.
    base64.StdEncoding.EncodeToString(content)
    
  2. Create a file with the request body, e.g., body.json:

    body.json:

    {
        "folderId": "b1gvmob95yys********",
        "analyze_specs": [{
            "content": "iVBORw0KGgo...",
            "features": [{
                "type": "TEXT_DETECTION",
                "text_detection_config": {
                    "language_codes": ["*"],
                    "model": "line"
                }
            }]
        }]
    }
    

    Where:

    • folderId: ID of any folder for which your account has the ai.vision.user role or higher.
    • content: Image file contents encoded as Base64.
    • model: Line model.
  3. Send a request using the batchAnalyze method and save the response to a file, e.g., output.json:

    Bash
    CMD
    PowerShell
    export IAM_TOKEN=<IAM_token>
    curl \
      --request POST \
      --header "Content-Type: application/json" \
      --header "Authorization: Bearer ${IAM_TOKEN}" \
      --data '@body.json' \
      https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze > output.json
    
    set IAM_TOKEN=<IAM_token>
    curl ^
      --request POST ^
      --header "Content-Type: application/json" ^
      --header "Authorization: Bearer %IAM_TOKEN%" ^
      --data "@body.json" ^
      https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze > output.json
    
    $Env:IAM_TOKEN="<IAM_token>"
    curl `
      --request POST `
      --header "Content-Type: application/json" `
      --header "Authorization: Bearer $Env:IAM_TOKEN" `
      --data '@body.json' `
      https://vision.api.cloud.yandex.net/vision/v1/batchAnalyze > output.json
    

Was the article helpful?

© 2025 Direct Cursus Technology L.L.C.