This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Twelve Labs provides multimodal embeddings that project text, images,
audio, and video into the same semantic space. This enables true
cross-modal search - the most powerful feature of this integration.What makes this special? You can search a video index using any
modality:This notebook demonstrates this cross-modal capability with video, then
shows how to apply the same embeddings to other modalities.
import getpassimport osif 'TWELVELABS_API_KEY' not in os.environ: os.environ['TWELVELABS_API_KEY'] = getpass.getpass( 'Enter your Twelve Labs API key: ' )
import pixeltable as pxtimport pixeltable.functions as pxtf# Create a fresh directory for our demopxt.drop_dir('twelvelabs_demo', force=True)pxt.create_dir('twelvelabs_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘twelvelabs_demo’.
<pixeltable.catalog.dir.Dir at 0x138c2c150>
# Create a table for videosvideo_t = pxt.create_table('twelvelabs_demo/videos', {'video': pxt.Video})# Insert a sample videovideo_url = 'https://github.com/pixeltable/pixeltable/raw/main/docs/resources/The-Pursuit-of-Happiness.mp4'video_t.insert([{'video': video_url}])
Created table ‘videos’.
Inserted 1 row with 0 errors in 1.60 s (0.63 rows/s)
1 row inserted.
# Create a view that segments the video into searchable chunks# Twelve Labs requires minimum 4 second segmentsvideo_chunks = pxt.create_view( 'twelvelabs_demo/video_chunks', video_t, iterator=pxtf.video.video_splitter( video=video_t.video, duration=5.0, min_segment_duration=4.0 ),)# Add embedding index for cross-modal searchvideo_chunks.add_embedding_index( 'video_segment', embedding=pxtf.twelvelabs.embed.using(model_name='marengo3.0'),)
Let’s look at the index we just added in the table metadata:
video_chunks
The iterator created a larger table from our single video: