Learn how to manage keyframes in video projects using the Labellerr Python SDK, including adding, updating, and deleting keyframes for video annotation workflows.
Keyframes in video annotation are specific frames selected for annotation within a video file. Instead of annotating every frame, you can select key moments that represent important changes or events in the video.This approach:
Important: Keyframe operations are only available for video projects. The LabellerrProject class automatically detects video projects and provides these methods.
Remove keyframes by specifying their frame numbers:
Delete Keyframes
Copy
from labellerr.client import LabellerrClientfrom labellerr.core.projects import LabellerrProjectclient = LabellerrClient( api_key='your_api_key', api_secret='your_api_secret', client_id='your_client_id')try: project = LabellerrProject(client=client, project_id="video_project_id") # Delete specific keyframes by frame number result = project.delete_keyframes( file_id="video_file_id", keyframes=[0, 100, 200] # List of frame numbers to delete ) print(f"Keyframes deleted successfully: {result}")except LabellerrError as e: print(f"Failed to delete keyframes: {str(e)}")
Caution: Deleting keyframes also removes any annotations associated with those frames. This action cannot be undone.
from labellerr.core.exceptions import LabellerrErrortry: project = LabellerrProject(client=client, project_id="video_project_id") keyframes = [ KeyFrame(frame_number=100, is_manual=True, method="manual", source="manual") ] result = project.add_or_update_keyframes( file_id="video_file_id", keyframes=keyframes )except LabellerrError as e: if "not found" in str(e).lower(): print("Error: Video file or project not found") elif "invalid frame" in str(e).lower(): print("Error: Frame number is invalid") else: print(f"Error: {str(e)}")
def select_keyframes_with_threshold(video_length_frames, min_interval=30, max_interval=90): """ Intelligently select keyframes based on video length. """ keyframes = [] if video_length_frames < 3000: # Short video interval = min_interval elif video_length_frames < 10000: # Medium video interval = 60 else: # Long video interval = max_interval for frame_num in range(0, video_length_frames, interval): keyframes.append( KeyFrame(frame_number=frame_num, is_manual=True, method="manual", source="manual") ) return keyframes# Use the custom logickeyframes = select_keyframes_with_threshold(video_length_frames=18000)project.add_or_update_keyframes(file_id="video_file_id", keyframes=keyframes)