Spaces:
Running
on
Zero
Running
on
Zero
File size: 5,569 Bytes
142a1ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import requests
import subprocess
import json
import pandas as pd
import zipfile
import cv2
from pathlib import Path
from tqdm import tqdm
from .video_base import VideoDataset
class SomethingSomethingDataset(VideoDataset):
"""
Something Something Dataset from https://arxiv.org/abs/1706.04261
"""
def download(self):
self.data_root.mkdir(parents=True, exist_ok=True)
urls = [
"https://apigwx-aws.qualcomm.com/qsc/public/v1/api/download/software/dataset/AIDataset/Something-Something-V2/20bn-something-something-v2-00",
"https://apigwx-aws.qualcomm.com/qsc/public/v1/api/download/software/dataset/AIDataset/Something-Something-V2/20bn-something-something-v2-01",
"https://softwarecenter.qualcomm.com/api/download/software/dataset/AIDataset/Something-Something-V2/20bn-something-something-download-package-labels.zip",
]
for url in urls:
filename = Path(url).name
filepath = self.data_root / filename
print(f"Downloading {filename}...")
response = requests.get(url, stream=True)
response.raise_for_status()
with open(filepath, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
# Use shell command to concatenate and extract tar video files
print("Concatenating and extracting tar files...")
cmd = f"cd {self.data_root} && cat 20bn-something-something-v2-0? | tar -xvzf -"
subprocess.run(cmd, shell=True, check=True)
print(f"Deleting zip files for video data...")
for zip_file in self.data_root.glob("20bn-something-something-v2-0*"):
print(f"Deleting {zip_file.name}...")
zip_file.unlink()
# Unzip the labels package
labels_zip_path = (
self.data_root / "20bn-something-something-download-package-labels.zip"
)
if labels_zip_path.exists():
print(f"Extracting {labels_zip_path.name}...")
with zipfile.ZipFile(labels_zip_path, "r") as zip_ref:
zip_ref.extractall(self.data_root)
print(f"Deleting zip file for labels...")
labels_zip_path.unlink()
# Create metadata CSV from labels
print("Creating metadata CSV file for Something Something Dataset")
json_files = {
"training": "labels/train.json",
"validation": "labels/validation.json",
}
records = []
for split, json_file in json_files.items():
with open(self.data_root / json_file, "r") as f:
labels = json.load(f)
for item in tqdm(labels, desc=f"Creating metadata for {split}"):
webm_video_path = f"20bn-something-something-v2/{item['id']}.webm"
mp4_video_path = f"20bn-something-something-v2/{item['id']}.mp4"
total_videos = len(labels)
successful_conversions = 0
if (self.data_root / webm_video_path).exists():
# Convert webm to mp4 using ffmpeg
input_path = str(self.data_root / webm_video_path)
output_path = str(self.data_root / mp4_video_path)
cmd = f'ffmpeg -i {input_path} -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -c:v libx264 -c:a aac {output_path}'
try:
subprocess.run(
cmd,
shell=True,
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
# Delete the webm file after successful conversion
(self.data_root / webm_video_path).unlink()
# Get video metadata using cv2
cap = cv2.VideoCapture(output_path)
if not cap.isOpened():
continue
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.release()
caption = item["label"].replace("pretending to ", "")
records.append(
{
"video_path": mp4_video_path,
"caption": caption,
"height": height,
"width": width,
"fps": fps,
"n_frames": n_frames,
"split": split,
}
)
successful_conversions += 1
except subprocess.CalledProcessError:
print(f"Conversion failed for {webm_video_path}")
conversion_rate = (successful_conversions / total_videos) * 100
print(f"Conversion success rate: {conversion_rate:.2f}%")
# Save as CSV
metadata_path = self.data_root / self.metadata_path
metadata_path.parent.mkdir(parents=True, exist_ok=True)
df = pd.DataFrame.from_records(records)
df.to_csv(metadata_path, index=False)
print(f"Created metadata CSV with {len(records)} videos")
|