Skip to content

Instantly share code, notes, and snippets.

@maulikmadhavi
Created December 7, 2024 08:17
Show Gist options
  • Select an option

  • Save maulikmadhavi/51b783f99685431120ea3e309932e985 to your computer and use it in GitHub Desktop.

Select an option

Save maulikmadhavi/51b783f99685431120ea3e309932e985 to your computer and use it in GitHub Desktop.
Frame selection program
# %%
import os
from pathlib import Path
import cv2
import decord
import numpy as np
import pandas as pd
from decord import VideoReader, cpu
from tqdm import tqdm
# %%
def time_to_seconds(time_str):
hours, minutes, seconds = map(int, time_str.split(":"))
return 3600 * hours + 60 * minutes + seconds
vdict = {k: [] for k in df["VideoName"].unique()}
for row in df.iterrows():
vdict[row[1].VideoName].append([
time_to_seconds(row[1].Start_time),
time_to_seconds(row[1].End_time),
])
def detect_islands(sequence):
result = []
island_counter = 1
in_island = False
for i, val in enumerate(sequence):
if val == 1:
# If entering a new island, increment island counter and set flag
if not in_island:
in_island = True
result.append(f"h{island_counter}")
else:
# If leaving an island, increment island counter and reset flag
if in_island:
in_island = False
island_counter += 1
result.append(f"l{island_counter}")
return result
def select_frames(labels):
"""
Select frames for training by including all positive frames and a dynamic number of negative frames around transitions.
Args:
labels (list of str): Labels from detect_islands function.
Returns:
list of int: Indices of selected frames.
"""
selected_indices = set()
length = len(labels)
h_islands = []
current_label = None
start_idx = 0
# Identify all h-islands with their start and end indices
for i, label in enumerate(labels):
if label.startswith("h"):
if current_label != label:
if current_label and current_label.startswith("h"):
h_islands.append((current_label, start_idx, i - 1))
current_label = label
start_idx = i
else:
if current_label and current_label.startswith("h"):
h_islands.append((current_label, start_idx, i - 1))
current_label = label
start_idx = i
# Append the last island if it ends with h
if current_label and current_label.startswith("h"):
h_islands.append((current_label, start_idx, length - 1))
for h_label, h_start, h_end in h_islands:
h_length = h_end - h_start + 1
num_negatives = math.ceil(h_length / 2)
# Select negatives from up-edge (preceding l-island)
up_start = h_start - 1
while up_start >= 0 and labels[up_start].startswith("l"):
up_start -= 1
up_start += 1
up_end = h_start
up_length = up_end - up_start
selected_up = range(max(up_end - num_negatives, up_start), up_end)
for idx in selected_up:
selected_indices.add(idx)
# Select negatives from down-edge (following l-island)
down_start = h_end + 1
while down_start < length and labels[down_start].startswith("l"):
down_start += 1
down_end = down_start
down_start = h_end + 1
down_length = down_end - down_start
selected_down = range(down_start, min(down_start + num_negatives, down_end))
for idx in selected_down:
selected_indices.add(idx)
# Include all positive frames
for idx in range(h_start, h_end + 1):
selected_indices.add(idx)
return sorted(list(selected_indices))
# Example usage
if __name__ == "__main__":
# sequence = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1]
# sequence = [1,1,1,1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1]
sequence = [
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
0,
0,
0,
0,
0,
1,
1,
1,
1,
1,
1,
1,
1,
0,
0,
0,
0,
]
labels = detect_islands(sequence)
selected = select_frames(labels)
selection_mask = [1 if idx in selected else 0 for idx in range(len(sequence))]
print("Labels:", labels)
print("Selected Frames:", selection_mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment