-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.py
36 lines (29 loc) · 980 Bytes
/
video.py
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
import cv2
from PIL import Image
from tqdm import tqdm
HOUR_IN_SECONDS = 3600
def get_frames_from_video(video_path: str, n_frames: int):
video_capture = cv2.VideoCapture(video_path)
frame_count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT) or video_capture.get(
cv2.cv.CV_CAP_PROP_FRAME_COUNT
)
step_size_in_frames = frame_count // n_frames
if step_size_in_frames < 1:
step_size_in_frames = 1
pbar = tqdm(total=frame_count)
else:
pbar = tqdm(total=n_frames)
current_frame = 0
frames = []
while len(frames) < n_frames:
success, frame = video_capture.read()
if not success:
break
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frames.append(Image.fromarray(rgb_frame).convert("RGB"))
current_frame += step_size_in_frames
video_capture.set(1, current_frame)
pbar.update(1)
pbar.close()
video_capture.release()
return frames