File size: 2,176 Bytes
5eef839 |
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 |
import cv2
import json
import os
from tqdm import tqdm
def extract_first_and_last_frames(video_file):
# Create base directory path
base_dir = 'frames/' + os.path.splitext(os.path.basename(video_file))[0]
# Check if both 'first.jpg' and 'last.jpg' already exist
first_frame_path = os.path.join(base_dir, 'first.jpg')
last_frame_path = os.path.join(base_dir, 'last.jpg')
if os.path.exists(first_frame_path) and os.path.exists(last_frame_path):
print(f"Skipping {video_file} as frames already exist.")
return
cap = cv2.VideoCapture(video_file)
if not cap.isOpened():
print(f"Error: Could not open video {video_file}.")
return
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
if total_frames == 1:
print(f"Warning: Video {video_file} has only 1 frame.")
# Extract and save first frame
extract_and_save_frame(cap, video_file, 0, 'first', base_dir)
# Extract and save last frame
if total_frames > 1:
extract_and_save_frame(cap, video_file, total_frames - 1, 'last', base_dir)
cap.release()
def extract_and_save_frame(cap, video_file, frame_number, frame_type, base_dir):
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
ret, frame = cap.read()
attempts = 0
while not ret and attempts < 10:
frame_number -= 1
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_number)
ret, frame = cap.read()
attempts += 1
if not ret:
print(f"Error: Could not read frame after several attempts at {frame_number} in {video_file}.")
return
if not os.path.exists(base_dir):
os.makedirs(base_dir)
frame_file_name = f"{base_dir}/{frame_type}.jpg"
cv2.imwrite(frame_file_name, frame)
print(f"Saved {frame_file_name}")
# Read JSON file
json_file = 'valid.json'
with open(json_file, 'r') as file:
videos = json.load(file)
# Process each video file in the JSON
for video in tqdm(videos):
video_file_name = f"{video['id']}.webm" # Assuming webm format; adjust if needed
video_file_path = os.path.join('./videos', video_file_name)
extract_first_and_last_frames(video_file_path)
|