Classify

The resources.Classify class provides methods to classify a set of videos or all the videos within an index.

Related quickstart notebook: Open In Colab

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:

def videos(
    self,
    video_ids: List[str],
    options: List[Union[str, Literal["visual", "conversation", "text_in_video"]]],
    classes: List[models.ClassifyClassParams],
    *,
    conversation_option: Optional[Union[str, Literal["semantic", "exact_match"]]] = "semantic",
    page_limit: Optional[int] = None,
    include_clips: Optional[bool] = None,
    threshold: Optional[Dict[str, Any]] = None,
    show_detailed_score: Optional[bool] = None,
    **kwargs
) -> models.ClassifyPageResult
CLASSES = [
    {
        "name": "DanceTok",
        "prompts": [
            "Dance tutorial",
            "Dance group",
            "Dance competition"
        ]
    },
    {
        "name": "CookTok",
        "prompts": [
            "Cooking tutorial",
            "Cooking ustensils review"
        ]
    }
]

def print_classify_page_result(result):
    print("Classification results:")
    for video_data in result.data:
        print_classify_video_data(video_data)

    print("\nPage information:")
    print_token_page_info(result.page_info)
    
def print_classify_video_data(video_data):
    print(f"Video ID: {video_data.video_id}")
    for class_data in video_data.classes:
        print_classify_class(class_data)
    print()  # Add a blank line between videos

def print_classify_class(class_data):
    print(f"  Class name: {class_data.name}")
    print(f"  Score: {class_data.score}")
    print(f"  Duration ratio: {class_data.duration_ratio}")
    if class_data.clips:
        print("  Clips:")
        for clip in class_data.clips:
            print_classify_clip(clip)

def print_classify_clip(clip):
    print(f"    Start: {clip.start}")
    print(f"    End: {clip.end}")
    print(f"    Score: {clip.score}")
    print(f"    Option: {clip.option}")
    print(f"    Prompt: {clip.prompt}")
    print(f"    Thumbnail URL: {clip.thumbnail_url or 'N/A'}")
    if clip.detailed_scores:
        print_classify_detailed_score(clip.detailed_scores)

def print_classify_detailed_score(detailed_score):
    print(f"      Max score: {detailed_score.max_score}")
    print(f"      Avg score: {detailed_score.avg_score}")
    print(f"      Normalized score: {detailed_score.normalized_score}")

def print_token_page_info(page_info):
    print(f"Limit per page: {page_info.limit_per_page}")
    print(f"Total results: {page_info.total_results}")
    print(f"Page expired at: {page_info.page_expired_at}")
    print(f"Next page token: {page_info.next_page_token or 'N/A'}")
    print(f"Previous page token: {page_info.prev_page_token or 'N/A'}")

VIDEO_IDS = ["VIDEO_ID_1", "VIDEO_ID_2", "VIDEO_ID_3"]   
result = client.classify.videos(
    video_ids=VIDEO_IDS,
    options=["visual", "conversation"],
    classes=CLASSES,
    include_clips=True,
    show_detailed_score=True,
    page_limit=1
)

print_classify_page_result(result)

Parameters:

NameTypeRequiredDescription
video_idsList[str]YesA list containing the unique identifiers of the videos that you want to classify
optionsList[Union[str, Literal["visual", "conversation", "text_in_video"]]]YesA list that specifies the sources of information the platform uses when it categorizes a video.
classesList[models.ClassifyClassParams]YesAn array of ClassifyClassParams objects containing the names and the definitions of entities or actions that the platform must identify.
conversation_optionOptional[Union[str, Literal["semantic", "exact_match"]]]NoSpecifies the type of match the platform will perform.
page_limitOptional[int]NoThe maximum number of results per page.
include_clipsOptional[bool]NoSet this parameter to true to retrieve detailed information about each matching video clip.
thresholdOptional[Dict[str, Any]]NoThresholds for different classification options.
show_detailed_scoreOptional[bool]NoWhether to show detailed scores in the result.
**kwargsdictNoAdditional keyword arguments for the request.

Return value: Returns a 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:

def index(
    self,
    index_id: str,
    options: List[Union[str, Literal["visual", "conversation", "text_in_video"]]],
    classes: List[models.ClassifyClassParams],
    *,
    conversation_option: Optional[Union[str, Literal["semantic", "exact_match"]]] = "semantic",
    page_limit: Optional[int] = None,
    include_clips: Optional[bool] = None,
    threshold: Optional[Dict[str, Any]] = None,
    show_detailed_score: Optional[bool] = None,
    **kwargs
) -> models.ClassifyPageResult
CLASSES = [
    {
        "name": "DanceTok",
        "prompts": [
            "Dance tutorial",
            "Dance group",
            "Dance competition"
        ]
    },
    {
        "name": "CookTok",
        "prompts": [
            "Cooking tutorial",
            "Cooking ustensils review"
        ]
    }
]

def print_classify_page_result(result):
    print("Classification results:")
    for video_data in result.data:
        print_classify_video_data(video_data)

    print("\nPage information:")
    print_token_page_info(result.page_info)

def print_classify_video_data(video_data):
    print(f"Video ID: {video_data.video_id}")
    for class_data in video_data.classes:
        print_classify_class(class_data)
    print()  # Add a blank line between videos

def print_classify_class(class_data):
    print(f"  Class name: {class_data.name}")
    print(f"  Score: {class_data.score}")
    print(f"  Duration ratio: {class_data.duration_ratio}")
    if class_data.clips:
        print("  Clips:")
        for clip in class_data.clips:
            print_classify_clip(clip)

def print_classify_clip(clip):
    print(f"    Start: {clip.start}")
    print(f"    End: {clip.end}")
    print(f"    Score: {clip.score}")
    print(f"    Option: {clip.option}")
    print(f"    Prompt: {clip.prompt}")
    print(f"    Thumbnail URL: {clip.thumbnail_url or 'N/A'}")
    if clip.detailed_scores:
        print_classify_detailed_score(clip.detailed_scores)

def print_classify_detailed_score(detailed_score):
    print(f"      Max score: {detailed_score.max_score}")
    print(f"      Avg score: {detailed_score.avg_score}")
    print(f"      Normalized score: {detailed_score.normalized_score}")

def print_token_page_info(page_info):
    print(f"Limit per page: {page_info.limit_per_page}")
    print(f"Total results: {page_info.total_results}")
    print(f"Page expired at: {page_info.page_expired_at}")
    print(f"Next page token: {page_info.next_page_token or 'N/A'}")
    print(f"Previous page token: {page_info.prev_page_token or 'N/A'}")

result = client.classify.index(
    index_id="<YOUR_INDEX_ID>",
    options=["visual", "conversation"],
    classes=CLASSES,
    include_clips=True,
    show_detailed_score=True,
    page_limit=1
)

print_classify_page_result(result)

NameTypeRequiredDescription
index_idstrYesThe unique identifier of the index containing the videos you want to classify.
optionsList[Union[str, Literal["visual", "conversation", "text_in_video"]]]YesAn array that specifies the sources of information the platform uses when it categorizes a video.
classesList[models.ClassifyClassParams]YesAn array of ClassifyClassParams objects containing the names and the definitions of entities or actions that the platform must identify.
conversation_optionOptional[Union[str, Literal["semantic", "exact_match"]]]NoSpecifies the type of match the platform will perform.
page_limitOptional[int]NoThe maximum number of results per page.
include_clipsOptional[bool]NoSet this parameter to true to retrieve detailed information about each matching video clip.
thresholdOptional[Dict[str, Any]]NoThresholds for different classification options.
show_detailed_scoreOptional[bool]NoWhether to show detailed scores in the result.
**kwargsdictNoAdditional keyword arguments for the request.

Return value: Returns a 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:

def by_page_token(
    self,
    page_token: str,
    **kwargs
) -> models.ClassifyPageResult
def print_classify_page_result(result):
    print("Classification results:")
    for video_data in result.data:
        print_classify_video_data(video_data)

    print("\nPage information:")
    print_token_page_info(result.page_info)

def print_classify_video_data(video_data):
    print(f"Video ID: {video_data.video_id}")
    for class_data in video_data.classes:
        print_classify_class(class_data)
    print()  # Add a blank line between videos

def print_classify_class(class_data):
    print(f"  Class name: {class_data.name}")
    print(f"  Score: {class_data.score}")
    print(f"  Duration ratio: {class_data.duration_ratio}")
    if class_data.clips:
        print("  Clips:")
        for clip in class_data.clips:
            print_classify_clip(clip)

def print_classify_clip(clip):
    print(f"    Start: {clip.start}")
    print(f"    End: {clip.end}")
    print(f"    Score: {clip.score}")
    print(f"    Option: {clip.option}")
    print(f"    Prompt: {clip.prompt}")
    print(f"    Thumbnail URL: {clip.thumbnail_url or 'N/A'}")
    if clip.detailed_scores:
        print_classify_detailed_score(clip.detailed_scores)

def print_classify_detailed_score(detailed_score):
    print(f"      Max score: {detailed_score.max_score}")
    print(f"      Avg score: {detailed_score.avg_score}")
    print(f"      Normalized score: {detailed_score.normalized_score}")

def print_token_page_info(page_info):
    print(f"Limit per page: {page_info.limit_per_page}")
    print(f"Total results: {page_info.total_results}")
    print(f"Page expired at: {page_info.page_expired_at}")
    print(f"Next page token: {page_info.next_page_token or 'N/A'}")
    print(f"Previous page token: {page_info.prev_page_token or 'N/A'}")

result = client.classify.by_page_token("<YOUR_PAGE_TOKEN>")

print_classify_page_result(result)

Parameters:

NameTypeRequiredDescription
pageTokenstrYesA token that identifies the page to retrieve.
**kwargsdictNoAdditional keyword arguments for the request.

Return value: Returns a models.ClassifyPageResult object containing the classification results for the specified page.

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 index or videos methods of the classify object. To retrieve subsequent pages of results, use the iterator protocol.

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

def print_classify_page_result(result):
    print("Classification results:")
    data = getattr(result, 'data', result)
    if isinstance(data, list):
        for video_data in data:
            print_classify_video_data(video_data)
    else:
        print_classify_video_data(data)

def print_classify_video_data(video_data):
    print(f"Video ID: {video_data.video_id}")
    for class_data in video_data.classes:
        print_classify_class(class_data)
    print()  # Add a blank line between videos

def print_classify_class(class_data):
    print(f"  Class name: {class_data.name}")
    print(f"  Score: {class_data.score}")
    print(f"  Duration ratio: {class_data.duration_ratio}")
    if class_data.clips:
        print("  Clips:")
        for clip in class_data.clips:
            print_classify_clip(clip)

def print_classify_clip(clip):
    print(f"    Start: {clip.start}")
    print(f"    End: {clip.end}")
    print(f"    Score: {clip.score}")
    print(f"    Option: {clip.option}")
    print(f"    Prompt: {clip.prompt}")
    print(f"    Thumbnail URL: {clip.thumbnail_url or 'N/A'}")
    if clip.detailed_scores:
        print_classify_detailed_score(clip.detailed_scores)

def print_classify_detailed_score(detailed_score):
    print(f"      Max score: {detailed_score.max_score}")
    print(f"      Avg score: {detailed_score.avg_score}")
    print(f"      Normalized score: {detailed_score.normalized_score}")


result = client.classify.index(
    index_id="<YOUR_INDEX_ID>",
    options=["visual", "conversation"],
    classes=CLASSES,
    include_clips=True,
    show_detailed_score=True,
    page_limit=1
)

print_classify_page_result(result)

page_number = 2
while True:
    try:
        print(f"Page {page_number}")
        print_classify_page_result(next(result))
        page_number += 1
    except StopIteration:
        break

print("No more results.")