christopi commited on
Commit ·
7a4e2aa
1
Parent(s): 6c28e20
add 4k to 8k workflow
Browse files
.gitignore
CHANGED
|
@@ -19,6 +19,9 @@ videos/
|
|
| 19 |
wandb/
|
| 20 |
IQA-PyTorch/
|
| 21 |
MDTVSFA/
|
|
|
|
|
|
|
|
|
|
| 22 |
vmaf_output.xml
|
| 23 |
services/video_scheduler/videos/
|
| 24 |
services/upscaling/videos/
|
|
|
|
| 19 |
wandb/
|
| 20 |
IQA-PyTorch/
|
| 21 |
MDTVSFA/
|
| 22 |
+
MANIQA/
|
| 23 |
+
frames/
|
| 24 |
+
video_quality_analyzer/
|
| 25 |
vmaf_output.xml
|
| 26 |
services/video_scheduler/videos/
|
| 27 |
services/upscaling/videos/
|
scripts/capture_video_frames.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import argparse
|
| 4 |
+
import numpy as np
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
def capture_frames(video_path, output_dir, num_frames=3):
|
| 8 |
+
"""
|
| 9 |
+
Captures the first num_frames frames from a video file at the video's native resolution.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
video_path (str): Path to the video file
|
| 13 |
+
output_dir (str): Directory to save the captured frames
|
| 14 |
+
num_frames (int): Number of frames to capture from the start
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
list: Paths to the saved frames
|
| 18 |
+
"""
|
| 19 |
+
# Check if video exists
|
| 20 |
+
if not os.path.isfile(video_path):
|
| 21 |
+
raise FileNotFoundError(f"Video file not found: {video_path}")
|
| 22 |
+
|
| 23 |
+
# Create output directory if it doesn't exist
|
| 24 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 25 |
+
|
| 26 |
+
# Get video filename without extension
|
| 27 |
+
video_name = Path(video_path).stem
|
| 28 |
+
|
| 29 |
+
# Open the video file
|
| 30 |
+
cap = cv2.VideoCapture(video_path)
|
| 31 |
+
|
| 32 |
+
# Check if video opened successfully
|
| 33 |
+
if not cap.isOpened():
|
| 34 |
+
raise Exception(f"Error opening video file: {video_path}")
|
| 35 |
+
|
| 36 |
+
# Get video properties
|
| 37 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 38 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 39 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 40 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 41 |
+
|
| 42 |
+
print(f"Video Info:")
|
| 43 |
+
print(f" Resolution: {width}x{height}")
|
| 44 |
+
print(f" FPS: {fps:.2f}")
|
| 45 |
+
print(f" Total Frames: {total_frames}")
|
| 46 |
+
|
| 47 |
+
# Check if we have enough frames
|
| 48 |
+
if total_frames < num_frames:
|
| 49 |
+
print(f"Warning: Video has only {total_frames} frames, which is less than requested {num_frames} frames.")
|
| 50 |
+
num_frames = total_frames
|
| 51 |
+
|
| 52 |
+
# Capture the first num_frames frames
|
| 53 |
+
saved_frames = []
|
| 54 |
+
for i in range(num_frames):
|
| 55 |
+
ret, frame = cap.read()
|
| 56 |
+
|
| 57 |
+
if not ret:
|
| 58 |
+
print(f"Error reading frame {i}")
|
| 59 |
+
break
|
| 60 |
+
|
| 61 |
+
# Save frame with its index, preserving original resolution
|
| 62 |
+
frame_path = os.path.join(output_dir, f"{video_name}_frame_{i:03d}.png")
|
| 63 |
+
cv2.imwrite(frame_path, frame)
|
| 64 |
+
saved_frames.append(frame_path)
|
| 65 |
+
|
| 66 |
+
print(f"Saved frame {i+1}/{num_frames}: {frame_path}")
|
| 67 |
+
|
| 68 |
+
# Release the video capture object
|
| 69 |
+
cap.release()
|
| 70 |
+
|
| 71 |
+
return saved_frames
|
| 72 |
+
|
| 73 |
+
def main():
|
| 74 |
+
parser = argparse.ArgumentParser(description="Capture frames from a video at original resolution")
|
| 75 |
+
parser.add_argument("--video", required=True, help="Path to the video file")
|
| 76 |
+
parser.add_argument("--output", default="frames", help="Directory to save the frames")
|
| 77 |
+
parser.add_argument("--frames", type=int, default=3, help="Number of frames to capture from the start")
|
| 78 |
+
|
| 79 |
+
args = parser.parse_args()
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
saved_frames = capture_frames(args.video, args.output, args.frames)
|
| 83 |
+
print(f"Successfully captured {len(saved_frames)} frames from the video")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error: {e}")
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
main()
|
services/video_scheduler/video_utils.py
CHANGED
|
@@ -33,7 +33,8 @@ def download_trim_downscale_video(
|
|
| 33 |
|
| 34 |
DOWNSCALE_HEIGHTS = {
|
| 35 |
"HD24K": 1080,
|
| 36 |
-
"4K28K": 2160
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
downscale_height = DOWNSCALE_HEIGHTS.get(task_type, 540)
|
|
@@ -57,7 +58,7 @@ def download_trim_downscale_video(
|
|
| 57 |
"SD2HD": (1920, 1080),
|
| 58 |
"4K28K": (7680, 4320),
|
| 59 |
}
|
| 60 |
-
expected_width, expected_height = EXPECTED_RESOLUTIONS.get(task_type, (
|
| 61 |
# Get video details
|
| 62 |
response = requests.get(url, headers=headers)
|
| 63 |
response.raise_for_status() # Raise exception for bad status codes
|
|
|
|
| 33 |
|
| 34 |
DOWNSCALE_HEIGHTS = {
|
| 35 |
"HD24K": 1080,
|
| 36 |
+
"4K28K": 2160,
|
| 37 |
+
|
| 38 |
}
|
| 39 |
|
| 40 |
downscale_height = DOWNSCALE_HEIGHTS.get(task_type, 540)
|
|
|
|
| 58 |
"SD2HD": (1920, 1080),
|
| 59 |
"4K28K": (7680, 4320),
|
| 60 |
}
|
| 61 |
+
expected_width, expected_height = EXPECTED_RESOLUTIONS.get(task_type, (3840, 2160))
|
| 62 |
# Get video details
|
| 63 |
response = requests.get(url, headers=headers)
|
| 64 |
response.raise_for_status() # Raise exception for bad status codes
|
services/video_scheduler/worker.py
CHANGED
|
@@ -52,15 +52,15 @@ def purge_cached_videos():
|
|
| 52 |
if os.path.exists(videos_dir):
|
| 53 |
shutil.rmtree(videos_dir)
|
| 54 |
os.makedirs(videos_dir)
|
| 55 |
-
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
os.makedirs(videos_dir)
|
| 59 |
-
|
| 60 |
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
-
|
| 64 |
|
| 65 |
async def get_synthetic_urls_with_retry(hotkey: str, max_retries: int = 2, initial_delay: float = 5.0, num_needed: int = 2) -> Optional[List[str]]:
|
| 66 |
"""Attempt to fetch synthetic request URLs with exponential backoff retry."""
|
|
@@ -130,7 +130,7 @@ def get_pexels_random_vids(
|
|
| 130 |
num_needed (int): Number of video IDs required.
|
| 131 |
min_len (int): Minimum video length in seconds.
|
| 132 |
max_len (int): Maximum video length in seconds.
|
| 133 |
-
width (int): Required video width (default:
|
| 134 |
height (int): Required video height (default: 2160).
|
| 135 |
max_results (int, optional): Max videos to fetch before selecting randomly (default: num_needed * 10).
|
| 136 |
task_type: The type of task
|
|
@@ -139,15 +139,15 @@ def get_pexels_random_vids(
|
|
| 139 |
"""
|
| 140 |
|
| 141 |
RESOLUTIONS = {
|
| 142 |
-
"HD24K": (
|
| 143 |
"SD2HD": (1920, 1080),
|
| 144 |
-
"SD24K": (
|
| 145 |
"4K28K": (7680, 4320),
|
| 146 |
"HD28K": (7680, 4320),
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
-
width, height = (width, height) if width and height else RESOLUTIONS.get(task_type, (
|
| 151 |
|
| 152 |
api_key = os.getenv("PEXELS_API_KEY")
|
| 153 |
if not api_key:
|
|
@@ -227,7 +227,12 @@ def get_pexels_random_vids(
|
|
| 227 |
logger.info(f"Time taken to get {num_needed} vids: {elapsed_time:.2f} seconds")
|
| 228 |
|
| 229 |
random.shuffle(valid_video_ids)
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
|
| 233 |
async def get_synthetic_requests_paths(num_needed: int, redis_conn: redis.Redis) -> List[Dict[str, str]]:
|
|
@@ -239,6 +244,9 @@ async def get_synthetic_requests_paths(num_needed: int, redis_conn: redis.Redis)
|
|
| 239 |
|
| 240 |
video_id_data = pop_pexels_video_id(redis_conn)
|
| 241 |
|
|
|
|
|
|
|
|
|
|
| 242 |
if video_id_data == None:
|
| 243 |
time.sleep(10)
|
| 244 |
continue
|
|
@@ -313,7 +321,7 @@ async def main():
|
|
| 313 |
needed = 5
|
| 314 |
|
| 315 |
ran_num = random.random()
|
| 316 |
-
ran_num = 0.
|
| 317 |
logger.info(f"Seleted random number: {ran_num}")
|
| 318 |
if ran_num < threshold_hd_to_4k:
|
| 319 |
task_type = "HD24K"
|
|
@@ -330,6 +338,8 @@ async def main():
|
|
| 330 |
|
| 331 |
needed_vids = get_pexels_random_vids(num_needed = needed, min_len = video_min_len, max_len = video_max_len, task_type = task_type)
|
| 332 |
|
|
|
|
|
|
|
| 333 |
needed_vids_dict = []
|
| 334 |
for vid in needed_vids:
|
| 335 |
needed_vids_dict.append({
|
|
|
|
| 52 |
if os.path.exists(videos_dir):
|
| 53 |
shutil.rmtree(videos_dir)
|
| 54 |
os.makedirs(videos_dir)
|
| 55 |
+
logger.info(f"Successfully purged all files from {videos_dir}")
|
| 56 |
else:
|
| 57 |
+
logger.info(f"Directory not found: {videos_dir}")
|
| 58 |
os.makedirs(videos_dir)
|
| 59 |
+
logger.info(f"Created new videos directory at {videos_dir}")
|
| 60 |
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
+
logger.info(f"Error while purging videos: {str(e)}")
|
| 64 |
|
| 65 |
async def get_synthetic_urls_with_retry(hotkey: str, max_retries: int = 2, initial_delay: float = 5.0, num_needed: int = 2) -> Optional[List[str]]:
|
| 66 |
"""Attempt to fetch synthetic request URLs with exponential backoff retry."""
|
|
|
|
| 130 |
num_needed (int): Number of video IDs required.
|
| 131 |
min_len (int): Minimum video length in seconds.
|
| 132 |
max_len (int): Maximum video length in seconds.
|
| 133 |
+
width (int): Required video width (default: 3840).
|
| 134 |
height (int): Required video height (default: 2160).
|
| 135 |
max_results (int, optional): Max videos to fetch before selecting randomly (default: num_needed * 10).
|
| 136 |
task_type: The type of task
|
|
|
|
| 139 |
"""
|
| 140 |
|
| 141 |
RESOLUTIONS = {
|
| 142 |
+
"HD24K": (3840, 2160),
|
| 143 |
"SD2HD": (1920, 1080),
|
| 144 |
+
"SD24K": (3840, 2160),
|
| 145 |
"4K28K": (7680, 4320),
|
| 146 |
"HD28K": (7680, 4320),
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
+
width, height = (width, height) if width and height else RESOLUTIONS.get(task_type, (3840, 2160))
|
| 151 |
|
| 152 |
api_key = os.getenv("PEXELS_API_KEY")
|
| 153 |
if not api_key:
|
|
|
|
| 227 |
logger.info(f"Time taken to get {num_needed} vids: {elapsed_time:.2f} seconds")
|
| 228 |
|
| 229 |
random.shuffle(valid_video_ids)
|
| 230 |
+
|
| 231 |
+
return_val = valid_video_ids[:num_needed]
|
| 232 |
+
|
| 233 |
+
logger.info(return_val)
|
| 234 |
+
|
| 235 |
+
return return_val
|
| 236 |
|
| 237 |
|
| 238 |
async def get_synthetic_requests_paths(num_needed: int, redis_conn: redis.Redis) -> List[Dict[str, str]]:
|
|
|
|
| 244 |
|
| 245 |
video_id_data = pop_pexels_video_id(redis_conn)
|
| 246 |
|
| 247 |
+
logger.info("downloading video data: ")
|
| 248 |
+
logger.info(video_id_data)
|
| 249 |
+
|
| 250 |
if video_id_data == None:
|
| 251 |
time.sleep(10)
|
| 252 |
continue
|
|
|
|
| 321 |
needed = 5
|
| 322 |
|
| 323 |
ran_num = random.random()
|
| 324 |
+
ran_num = 0.3
|
| 325 |
logger.info(f"Seleted random number: {ran_num}")
|
| 326 |
if ran_num < threshold_hd_to_4k:
|
| 327 |
task_type = "HD24K"
|
|
|
|
| 338 |
|
| 339 |
needed_vids = get_pexels_random_vids(num_needed = needed, min_len = video_min_len, max_len = video_max_len, task_type = task_type)
|
| 340 |
|
| 341 |
+
logger.info(f"needed video ids: f{needed_vids}")
|
| 342 |
+
|
| 343 |
needed_vids_dict = []
|
| 344 |
for vid in needed_vids:
|
| 345 |
needed_vids_dict.append({
|
vidaio_subnet_core/configs/video_scheduler.py
CHANGED
|
@@ -7,7 +7,7 @@ class VideoSchedulerConfig(BaseModel):
|
|
| 7 |
max_synthetic_queue_size: int = Field(default=50)
|
| 8 |
refill_threshold: int = Field(default=3)
|
| 9 |
refill_target: int = Field(default=10)
|
| 10 |
-
clip_duration: int = Field(default=
|
| 11 |
min_video_len: int = Field(default=10)
|
| 12 |
max_video_len: int = Field(default=30)
|
| 13 |
pexels_max_size: int = Field(default = 100)
|
|
|
|
| 7 |
max_synthetic_queue_size: int = Field(default=50)
|
| 8 |
refill_threshold: int = Field(default=3)
|
| 9 |
refill_target: int = Field(default=10)
|
| 10 |
+
clip_duration: int = Field(default=2)
|
| 11 |
min_video_len: int = Field(default=10)
|
| 12 |
max_video_len: int = Field(default=30)
|
| 13 |
pexels_max_size: int = Field(default = 100)
|