Analyze images
This guide shows how to analyze one or more images with a prompt. Upload your images as assets and reference their identifiers, or pass them inline by URL or base64-encoded data. You can analyze up to twenty images in a single request.
On the Free plan, each image you analyze counts as 5 minutes toward a shared limit that also covers indexing. For example, 120 images use the entire 10-hour allowance.
For details on how your usage is measured and billed, see the Pricing page.
Key concepts
This section explains the key concepts and terminology used in this guide:
- Asset: Your uploaded content. Once created, you can reference the same asset across multiple operations without uploading the file again.
- Analysis task: An asynchronous operation for processing your content and generating text. Contains a status and the resulting text when complete.
Prerequisites
-
To use the platform, you need an API key:
-
Depending on the programming language you are using, install the TwelveLabs SDK by entering one of the following commands:
-
Your images must meet the following requirements:
- Number of images: one to twenty per request
- Formats: JPEG, PNG, WebP, GIF, and BMP
- File size: ≤ 20 MB per image
- Pixel count: ≤ 16,777,216 pixels per image (width × height)
Complete example
Copy and paste the code below, replacing the placeholders surrounded by <> with your values. The example analyzes two images - the first uploaded as an asset, the second passed inline by URL - and references both in the prompt.
Code explanation
Python
Node.js
Import the SDK and initialize the client
Create a client instance to interact with the TwelveLabs Video Understanding Platform.
Function call: You call the constructor of the TwelveLabs class.
Parameters:
api_key: The API key to authenticate your requests to the platform.
Return value: An object of type TwelveLabs configured for making API calls.
Upload an image
Upload the first image to create an asset. Image analysis requires the asset to be in the ready state before you use it.
Function call: You call the assets.create method.
Parameters:
method: How to provide the image. Useurlwith a publicly accessible image URL, ordirectwith a local file.
Check the status of the asset
Asset processing is asynchronous. Poll the status of the asset until it is ready before you use it.
Analyze your images
Create an analysis task to start processing your images. This operation is asynchronous.
Function call: You call the analyze_async.tasks.create method.
Parameters:
-
image: A list ofAnalyzeImageInputobjects, one per image. You can analyze up to twenty images per request. For each image, set:name: The name you use to reference the image in the prompt. Can contain only letters, digits, and underscores.- Exactly one source:
asset_id,url, orbase_64_string.
This example uses the asset identifier from the previous step for the first image and passes the second image inline by URL.
-
prompt: The instructions for the analysis. To refer to a specific image in your prompt, use its name as the<@name>placeholder. Reference every image or none of them. If the prompt references none of them, the platform analyzes every image you provided. If it references only some of them, the platform returns a400error. -
(Optional)
temperature: Controls the randomness of the text output. A higher value generates more creative text, while a lower value produces more deterministic output. -
(Optional)
max_tokens: The maximum response length, in tokens. -
(Optional)
response_format: Use this parameter to request structured JSON responses. For instructions, examples, and best practices, see the Structured responses page.
Return value: An object of type CreateAnalyzeTaskResponse containing a field named task_id, which represents the unique identifier of your analysis task. You can use this identifier to track the status of your task.
Monitor the status
The platform requires some time to process images. Poll the status of the analysis task until processing completes. This example uses a loop to check the status every 5 seconds.
Function call: You repeatedly call the analyze_async.tasks.retrieve method until the task completes.
Parameters:
task_id: The unique identifier of your analysis task.
Return value: An object of type AnalyzeTaskResponse containing, among other information, the following fields:
status: The current status of the task. The possible values are:queued: The task is waiting to be processed.pending: The task is queued and waiting to start.processing: The platform is analyzing your images.ready: Processing is complete. Results are available in theresultfield.failed: The task failed.
result: When the status isready, this field contains the generated text and usage information.
Synchronous analysis
For immediate results, use the synchronous method instead of creating an analysis task. It supports the same image requirements and returns (or streams) the generated text directly in the response.
Response methods
Streaming responses
Streaming responses deliver text fragments in real time as they are generated, enabling immediate processing and feedback. This method is the default behavior of the platform and is ideal for applications requiring incremental updates.
- Response format: A stream of JSON objects in NDJSON format, with three event types:
stream_start: Marks the beginning of the stream.text_generation: Delivers a fragment of the generated text.stream_end: Signals the end of the stream.
- Response handling:
- Iterate over the stream to process text fragments as they arrive.
- Iterate over the stream to process text fragments as they arrive.
- Advantages:
- Real-time processing of partial results.
- Reduced perceived latency.
- Use case: Live captioning, real-time analysis, or applications needing instant updates.
Non-streaming responses
Non-streaming responses deliver the complete generated text in a single response, simplifying processing when the full result is needed.
- Response format: A single string containing the full generated text.
- Response handling:
- Access the complete text directly from the response.
- Advantages:
- Simplicity in handling the full result.
- Immediate access to the entire text.
- Use case: Generating reports, descriptions, or any scenario where the whole text is required at once.
Copy and paste the code below, replacing the placeholders surrounded by <> with your values.
Streaming responses
Non-streaming responses
The image parameter and all optional parameters (model_name, temperature, max_tokens, response_format, etc.) function the same as in the asynchronous approach above.