Classify

The Resources.Classify class provides methods to classify a set of videos or entire indexes.

Methods

Classify a set of videos

Description: This method classifies a set of videos based on the provided parameters and returns the first page of results.

Function signature and example:

async videos(
  { conversationOption = 'semantic', ...restBody }: ClassifyVideosOptions,
  options: RequestOptions = {},
): Promise<Models.ClassifyPageResult>
const CLASSES = [
  {
    name: 'DanceTok',
    prompts: ['Dance tutorial', 'Dance group', 'Dance competition'],
  },
  {
    name: 'CookTok',
    prompts: ['Cooking tutorial', 'Cooking ustensils review'],
  },
];

function printClassifyPageResult(result) {
  console.log("Classification results:");
  result.data.forEach(videoData => printClassifyVideoData(videoData));
  
  console.log("\nPage information:");
  printTokenPageInfo(result.pageInfo);
}

function printClassifyVideoData(videoData) {
  console.log(`Video ID: ${videoData.videoId}`);
  videoData.classes.forEach(classData => printClassifyClass(classData));
  console.log(); // Add a blank line between videos
}

function printClassifyClass(classData) {
  console.log(`  Class name: ${classData.name}`);
  console.log(`  Score: ${classData.score}`);
  console.log(`  Duration ratio: ${classData.durationRatio}`);
  if (classData.clips && classData.clips.length > 0) {
    console.log("  Clips:");
    classData.clips.forEach(clip => printClassifyClip(clip));
  }
  if (classData.detailedScores) {
    printClassifyDetailedScore(classData.detailedScores);
  }
}

function printClassifyClip(clip) {
  console.log(`    Start: ${clip.start}`);
  console.log(`    End: ${clip.end}`);
  console.log(`    Score: ${clip.score}`);
  console.log(`    Option: ${clip.option}`);
  console.log(`    Prompt: ${clip.prompt}`);
  console.log(`    Thumbnail URL: ${clip.thumbnailUrl || 'N/A'}`);
}

function printClassifyDetailedScore(detailedScore) {
  console.log("  Detailed scores:");
  console.log(`    Max score: ${detailedScore.maxScore}`);
  console.log(`    Avg score: ${detailedScore.avgScore}`);
  console.log(`    Normalized score: ${detailedScore.normalizedScore}`);
}

function printTokenPageInfo(pageInfo) {
  console.log(`Limit per page: ${pageInfo.limitPerPage}`);
  console.log(`Total results: ${pageInfo.totalResults}`);
  console.log(`Page expired at: ${pageInfo.pageExpiredAt}`);
  console.log(`Next page token: ${pageInfo.nextPageToken || 'N/A'}`);
  console.log(`Previous page token: ${pageInfo.prevPageToken || 'N/A'}`);
}

VIDEO_IDS=["VIDEO_1", "VIDEO_2", "VIDEO_3"];

const result = await client.classify.videos({
    videoIds: VIDEO_IDS,
    options: ["visual"],
    classes: CLASSES,
    pageLimit: 2,
    // includeClips: true,
    showDetailedScore: true,
});

printClassifyPageResult(result);

Parameters:

NameTypeRequiredDescription
paramsClassifyVideosOptionsYesParameters for classifying the videos.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

The ClassifyVideosOptions interface defines the parameters for classifying a set of videos:

NameTypeRequiredDescription
videoIdsstring[]YesAn array containing the unique identifiers of the videos that you want to classify.

The following properties are inherited from the ClassifyOptions interface:

NameTypeRequiredDescription
options('visual' | 'conversation' | 'text_in_video')[]YesAn array that specifies the sources of information the platform uses when it categorizes a video.
classesArray<{ name: string; prompts: string[]; options?: ('visual' | 'conversation' | 'text_in_video')[]; conversationOption?: 'semantic' | 'exact_match' }>YesAn array of objects containing the names and the definitions of entities or actions that the platform must identify.
conversationOption'semantic' | 'exact_match'NoSpecifies the type of match the platform will perform.
pageLimitnumberNoThe maximum number of results per page.
includeClipsbooleanNoSet this parameter to true to retrieve detailed information about each matching video clip.
thresholdRecord<string, any>NoThresholds for different classification options.
showDetailedScorebooleanNoWhether to show detailed scores in the result.

Return value: Returns a Promise that resolves to an Models.ClassifyPageResult object containing the classification results.

API Reference: For a description of each field in the request and response, see the Classify a set of videos page.

Related guides:

Classify all the videos within an index

Description: This method classifies all the videos within a specific index based on the provided parameters and returns the first page of results.

Function signature and example:

async index(
  { conversationOption = 'semantic', ...restBody }: ClassifyIndexOptions,
  options: RequestOptions = {},
): Promise<Models.ClassifyPageResult> 
}
const CLASSES = [
  {
    name: 'DanceTok',
    prompts: ['Dance tutorial', 'Dance group', 'Dance competition'],
  },
  {
    name: 'CookTok',
    prompts: ['Cooking tutorial', 'Cooking ustensils review'],
  },
];

function printClassifyPageResult(result) {
  console.log("Classification results:");
  result.data.forEach(videoData => printClassifyVideoData(videoData));
  
  console.log("\nPage information:");
  printTokenPageInfo(result.pageInfo);
}

function printClassifyVideoData(videoData) {
  console.log(`Video ID: ${videoData.videoId}`);
  videoData.classes.forEach(classData => printClassifyClass(classData));
  console.log(); // Add a blank line between videos
}

function printClassifyClass(classData) {
  console.log(`  Class name: ${classData.name}`);
  console.log(`  Score: ${classData.score}`);
  console.log(`  Duration ratio: ${classData.durationRatio}`);
  if (classData.clips && classData.clips.length > 0) {
    console.log("  Clips:");
    classData.clips.forEach(clip => printClassifyClip(clip));
  }
  if (classData.detailedScores) {
    printClassifyDetailedScore(classData.detailedScores);
  }
}

function printClassifyClip(clip) {
  console.log(`    Start: ${clip.start}`);
  console.log(`    End: ${clip.end}`);
  console.log(`    Score: ${clip.score}`);
  console.log(`    Option: ${clip.option}`);
  console.log(`    Prompt: ${clip.prompt}`);
  console.log(`    Thumbnail URL: ${clip.thumbnailUrl || 'N/A'}`);
}

function printClassifyDetailedScore(detailedScore) {
  console.log("  Detailed scores:");
  console.log(`    Max score: ${detailedScore.maxScore}`);
  console.log(`    Avg score: ${detailedScore.avgScore}`);
  console.log(`    Normalized score: ${detailedScore.normalizedScore}`);
}

function printTokenPageInfo(pageInfo) {
  console.log(`Limit per page: ${pageInfo.limitPerPage}`);
  console.log(`Total results: ${pageInfo.totalResults}`);
  console.log(`Page expired at: ${pageInfo.pageExpiredAt}`);
  console.log(`Next page token: ${pageInfo.nextPageToken || 'N/A'}`);
  console.log(`Previous page token: ${pageInfo.prevPageToken || 'N/A'}`);
}

const result = await client.classify.index({
    indexId: "<YOUR_INDEX_ID>",
    options: ["visual"],
    classes: CLASSES,
    pageLimit: 2,
    // includeClips: true,
    showDetailedScore: true,
});

printClassifyPageResult(result);

Parameters:

NameTypeRequiredDescription
paramsClassifyIndexOptionsYesParameters for classifying the videos.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

The ClassifyIndexOptions interface defines the parameters for classifying a set of videos:

NameTypeRequiredDescription
indexIdstringYesThe unique identifier of the index containing the videos that you want to classify.

The following properties are inherited from the ClassifyOptions interface:

NameTypeRequiredDescription
options('visual' | 'conversation' | 'text_in_video')[]YesAn array that specifies the sources of information the platform uses when it categorizes a video.
classesArray<{ name: string; prompts: string[]; options?: ('visual' | 'conversation' | 'text_in_video')[]; conversationOption?: 'semantic' | 'exact_match' }>YesAn array of objects containing the names and the definitions of entities or actions that the platform must identify.
conversationOption'semantic' | 'exact_match'NoSpecifies the type of match the platform will perform.
pageLimitnumberNoThe maximum number of results per page.
includeClipsbooleanNoSet this parameter to true to retrieve detailed information about each matching video clip.
thresholdRecord<string, any>NoThresholds for different classification options.
showDetailedScorebooleanNoWhether to show detailed scores in the result.

Return value: Returns a Promise that resolves to an Models.ClassifyPageResult object containing the classification results.

API Reference: For a description of each field in the request and response, see the Classify all the videos within an index page.

Related guides:

Retrieve a specific page of results

Description: This method retrieves a specific page of results. This method provides direct pagination. Choose it mainly when the total number of items is manageable, or you must fetch a single page of results.

Function signature and example:

async byPageToken(pageToken: string, options: RequestOptions = {}): Promise<Models.ClassifyPageResult>
function printClassifyPageResult(result) {
  console.log("Classification results:");
  result.data.forEach(videoData => printClassifyVideoData(videoData));
  
  console.log("\nPage information:");
  printTokenPageInfo(result.pageInfo);
}

function printClassifyVideoData(videoData) {
  console.log(`Video ID: ${videoData.videoId}`);
  videoData.classes.forEach(classData => printClassifyClass(classData));
  console.log(); // Add a blank line between videos
}

function printClassifyClass(classData) {
  console.log(`  Class name: ${classData.name}`);
  console.log(`  Score: ${classData.score}`);
  console.log(`  Duration ratio: ${classData.durationRatio}`);
  if (classData.clips && classData.clips.length > 0) {
    console.log("  Clips:");
    classData.clips.forEach(clip => printClassifyClip(clip));
  }
  if (classData.detailedScores) {
    printClassifyDetailedScore(classData.detailedScores);
  }
}

function printClassifyClip(clip) {
  console.log(`    Start: ${clip.start}`);
  console.log(`    End: ${clip.end}`);
  console.log(`    Score: ${clip.score}`);
  console.log(`    Option: ${clip.option}`);
  console.log(`    Prompt: ${clip.prompt}`);
  console.log(`    Thumbnail URL: ${clip.thumbnailUrl || 'N/A'}`);
}

function printClassifyDetailedScore(detailedScore) {
  console.log("  Detailed scores:");
  console.log(`    Max score: ${detailedScore.maxScore}`);
  console.log(`    Avg score: ${detailedScore.avgScore}`);
  console.log(`    Normalized score: ${detailedScore.normalizedScore}`);
}

function printTokenPageInfo(pageInfo) {
  console.log(`Limit per page: ${pageInfo.limitPerPage}`);
  console.log(`Total results: ${pageInfo.totalResults}`);
  console.log(`Page expired at: ${pageInfo.pageExpiredAt}`);
  console.log(`Next page token: ${pageInfo.nextPageToken || 'N/A'}`);
  console.log(`Previous page token: ${pageInfo.prevPageToken || 'N/A'}`);
}

const result = await client.classify.byPageToken("<YOUR_PAGE_TOKEN>");
printClassifyPageResult(result); 

Parameters:

NameTypeRequiredDescription
pageTokenstringYesA token that identifies the page to retrieve.
optionsRequestOptionsNoAdditional options for the request. Defaults to {}.

Return value: Returns a Promise that resolves to an Models.ClassifyPageResult object containing the classification results.

API Reference: For a description of each field in the request and response, see the Retrieve a specific page of results page.

Related guides:


Iterative pagination

If your application must retrieve a large number of items, use iterative pagination. To retrieve the first page of results, invoke the videos or index method of the classify object. To retrieve subsequent pages of results, use the async iterator protocol.

const CLASSES = [
  {
    name: 'DanceTok',
    prompts: ['Dance tutorial', 'Dance group', 'Dance competition'],
  },
  {
    name: 'CookTok',
    prompts: ['Cooking tutorial', 'Cooking ustensils review'],
  },
];

function printClassifyPageResult(result, pageNumber) {
  console.log(`Page ${pageNumber}`);
  console.log("Classification results:");
  const data = result.data || result;
  data.forEach(videoData => printClassifyVideoData(videoData));
}

function printClassifyVideoData(videoData) {
  console.log(`Video ID: ${videoData.videoId}`);
  videoData.classes.forEach(classData => printClassifyClass(classData));
  console.log(); // Add a blank line between videos
}

function printClassifyClass(classData) {
  console.log(`  Class name: ${classData.name}`);
  console.log(`  Score: ${classData.score}`);
  console.log(`  Duration ratio: ${classData.durationRatio}`);
  if (classData.clips && classData.clips.length > 0) {
    console.log("  Clips:");
    classData.clips.forEach(clip => printClassifyClip(clip));
  }
  if (classData.detailedScores) {
    printClassifyDetailedScore(classData.detailedScores);
  }
}

function printClassifyClip(clip) {
  console.log(`    Start: ${clip.start}`);
  console.log(`    End: ${clip.end}`);
  console.log(`    Score: ${clip.score}`);
  console.log(`    Option: ${clip.option}`);
  console.log(`    Prompt: ${clip.prompt}`);
  console.log(`    Thumbnail URL: ${clip.thumbnailUrl || 'N/A'}`);
}

function printClassifyDetailedScore(detailedScore) {
  console.log("  Detailed scores:");
  console.log(`    Max score: ${detailedScore.maxScore}`);
  console.log(`    Avg score: ${detailedScore.avgScore}`);
  console.log(`    Normalized score: ${detailedScore.normalizedScore}`);
}

let classifyResults = await client.classify.index({
  indexId: "<YOUR_INDEX_ID>",
  options: ["visual"],
  classes: CLASSES,
  pageLimit: 2,
  showDetailedScore: true,
});

let pageNumber = 1;
printClassifyPageResult(classifyResults, pageNumber);

while (true) {
  const nextPage = await classifyResults.next();
  if (nextPage === null) break;
  pageNumber++;
  printClassifyPageResult(nextPage, pageNumber);
}

console.log("No more results.");
}