Extracting text recognized in video (OCR)

At a high level, extracting text recognized in a video (OCR) involves the following steps:

  • Upload and index a video. When you upload a video by calling the /tasks endpoint, the API service creates a video indexing task and returns its unique identifier.
  • Retrieve the unique identifier of your video. Once the API service finishes indexing your video you can retrieve the unique identifier of the video by calling the /tasks/{task-id} endpoint and passing it the unique identifier of your video indexing task.
  • Extract the text that appears in your video. Use the /indexes/{index-id}/videos/{video-id}/text-in-video endpoint passing it the unique identifiers of your index and video. For a description of each field in the request and response, see the API Reference > Retrieve text recognized in a video (OCR) page.

Note that, although the example code in this guide is written in Python and Node.js, the API is compatible with most programming languages, and you can also use Postman or other REST clients to send requests and view responses.

Prerequisites

  • A valid Twelve Labs account. For details about creating an account and retrieving your API key, see the Authentication page.
  • You’re familiar with the concepts that are described on the Quickstart page.
  • You’ve created at least one index, and the unique identifier of your index is stored in a variable named INDEX_ID. For details, see the Creating indexes page.
  • You've uploaded a video, and the API service has finished indexing it. The unique identifier of your video is stored in a variable named VIDEO_ID. For details, see the Uploading videos page.

Retrieve text recognized in a video

  1. Construct the URL for retrieving the text recognized in your video based on the INDEX_ID and VIDEO_ID variables:

    TEXT_IN_VIDEO_URL = f"{API_URL}/indexes/{INDEX_ID}/videos/{VIDEO_ID}/text-in-video
    
    const TEXT_IN_VIDEO_URL = `${API_URL}/indexes/${INDEX_ID}/videos/${VIDEO_ID}/text-in-video
    
  2. Retrieve the text and print it out to the console:

    response = requests.get(TEXT_IN_VIDEO_URL, headers=headers)
    print (f"Status code: {response.status_code}")
    pprint (response.json())
    
    config = {
      method: 'get',
      url: TEXT_IN_VIDEO_URL, 
      headers: headers,
    }
    resp = await axios(config)
    response = await resp.data
    console.log(`Status code: ${resp.status}`)
    console.log(response)
    

    The following example output was truncated for brevity:

    Status code: 200
    {'_id': '62aeb771154f59c87660ce91',
     'data': [{'end': 77,
               'start': 76,
               'value': 'DID YOU GET THAT ON VIDEO?'},
              {'end': 79, 'start': 78, 'value': 'YEAH'},
              {'end': 102, 'start': 101, 'value': 'ALRIGHTY HERE WE GO'},
              {'end': 295,
               'start': 294,
               'value': 'GOT LIKE A BIG STICK OR SOMETHING'}],
     'index_id': '629deb409ea24f052b971993',
    

    Note that you can use the start and end query parameters to specify the time range for which you want to retrieve the text. The following example URL retrieves the text for the first 10 seconds of the video:

    TEXT_IN_VIDEO_URL = f"{API_URL}/indexes/{INDEX_ID}/videos/{VIDEO_ID}/text-in-video?start=0&end=10"
    
    const TEXT_IN_VIDEO_URL = `${API_URL}/indexes/${INDEX_ID}/videos/${VIDEO_ID}/text-in-video?start=0&end=10`
    

Related Topics