|
import os |
|
import uuid |
|
|
|
from src.utils import makedirs, sanitize_filename |
|
|
|
|
|
def extract_unique_frames(urls=None, file=None, download_dir=None, export_dir=None, extract_frames=10): |
|
download_dir = download_dir or os.getenv('VID_DOWNLOADS', "viddownloads") |
|
download_dir = os.path.join(download_dir, str(uuid.uuid4())) |
|
|
|
if urls: |
|
import fiftyone.utils.youtube as fouy |
|
fouy.download_youtube_videos(urls, download_dir=download_dir) |
|
|
|
|
|
import fiftyone as fo |
|
if file: |
|
dataset = fo.Dataset.from_videos([file]) |
|
else: |
|
dataset = fo.Dataset.from_videos_dir(download_dir) |
|
|
|
|
|
frame_view = dataset.to_frames(sample_frames=True, fps=1) |
|
|
|
import fiftyone.brain as fob |
|
|
|
|
|
results = fob.compute_similarity(frame_view, brain_key="frame_sim") |
|
|
|
|
|
num_unique = extract_frames |
|
results.find_unique(num_unique) |
|
unique_view = frame_view.select(results.unique_ids) |
|
|
|
|
|
|
|
|
|
|
|
san_file = sanitize_filename(os.path.basename(file)) if file else None |
|
|
|
if san_file: |
|
export_dir = export_dir or "/tmp/gradio/extraction_%s" % san_file |
|
if os.path.isdir(export_dir): |
|
export_dir += "_%s" % str(uuid.uuid4()) |
|
else: |
|
export_dir = export_dir or "/tmp/gradio/extraction_%s" % str(uuid.uuid4()) |
|
makedirs(export_dir, exist_ok=True) |
|
unique_view.export(export_dir, dataset_type=fo.types.VideoDirectory) |
|
return export_dir |
|
|