Skip to navigation

Metadata Generation for Videos

In this tutorial we will cover metadata generation for videos.

The goal is to generate a table with one row for each video and one column for each tag indicating if that tag is present or not in the video. Coactive already provides the scores for different dynamic tags at the keyframe level in the table so generating metadata is basically an aggregation of those scores at the video level.

It is possible to create metadata from Dynamic Tags and Concepts.

Metadata Generation using Dynamic Tags

The SQL query below generates metadata for videos based on visual dynamic tag scores. It follows the rule to tag every video with K or more keyframes with dynamic tag scores above the percentile P.

The query reads the keyframe-level view dt_<dynamic_tag_group_name>_visual, which has no video ID, so it joins to coactive_table on coactive_image_id to get coactive_video_id. For the columns in each dynamic tag view, see Dynamic Tag Tables.

WITH
-- Step 1
keyframe_scores AS (
SELECT
dt.coactive_image_id,
t.coactive_video_id,
dt.tag_name,
dt.score
FROM dt_<dynamic_tag_group_name>_visual AS dt
JOIN coactive_table AS t
ON dt.coactive_image_id = t.coactive_image_id
),
-- Step 2
dt_count AS (
SELECT
tag_name,
COUNT(*) AS dt_count
FROM keyframe_scores
GROUP BY tag_name
),
-- Step 3
percentile_table AS (
SELECT
a.coactive_image_id,
a.tag_name,
a.score,
ROW_NUMBER() OVER (PARTITION BY a.tag_name ORDER BY a.score DESC)/b.dt_count AS percentile
FROM keyframe_scores AS a
LEFT JOIN dt_count AS b
ON a.tag_name = b.tag_name
),
-- Step 4
hit_table AS (
SELECT *,
1 AS hit
FROM percentile_table
-- Change here the percentile P of keyframes to be selected
WHERE percentile < P
),
-- Step 5
join_table AS (
SELECT
a.*,
COALESCE(b.hit, 0) AS hit
FROM keyframe_scores AS a
LEFT JOIN hit_table AS b
ON a.coactive_image_id = b.coactive_image_id
AND a.tag_name = b.tag_name
)
-- Step 6
SELECT
coactive_video_id,
tag_name,
-- Change here the required number of keyframes K above the threshold to tag a video
CASE WHEN SUM(hit) >= K THEN 1 ELSE 0 END AS num_hits
FROM join_table
GROUP BY
coactive_video_id,
tag_name

Explanation

  1. CTE (keyframe_scores): Join each keyframe’s tag scores to its video ID.
  2. CTE (dt_count): Count the total number of keyframes for each dynamic tag.
  3. CTE (percentile_table): Normalize the dynamic tags scores so that they are between 0 and 1 using the scores distribution for each tag.
  4. CTE (hit_table): Check which keyframes are above a desired threshold P (between 0 and 1). A P of 0.01 means that only the top 1% keyframes of a dynamic tag will be tagged.
  5. CTE (join_table): Join the table with the tags back to the original keyframe table.
  6. Final Query: Aggregate the table at the video level tagging all the videos with K or more tagged keyframes.

The same approach works for transcript-level tags: use dt_<dynamic_tag_group_name>_transcript joined to coactive_table_transcription on coactive_transcript_id = id. For the view’s columns, see Dynamic Tag Tables.

Metadata Generation using Concepts

The same exercise can also be done with concepts following the SQL query below. It follows the rule to tag videos with K or more keyframes with the concept score above a threshold T.

WITH
video_level_tab AS (
SELECT
COACTIVE_VIDEO_ID,
SUM(CASE WHEN <concept_name>_prob > T THEN 1 ELSE 0 END) AS NUM_LABELED_KFS
FROM
coactive_table_adv
GROUP BY
COACTIVE_VIDEO_ID
)
SELECT
*,
CASE WHEN NUM_LABELED_KFS >= K THEN 1 ELSE 0 END AS <concept_name>_tag
FROM
video_level_tab
ORDER BY 2 DESC;

Explanation

Define a threshold T to tag every keyframe with a concept score above it. The interpretation is that we are tagging every keyframe with a probability T or bigger of belonging to the concept;

  1. CTE (video_level_tab): count how many keyframes were tagged in a video.
  2. Final Query: Tag the videos with K or more tagged keyframes