-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_impl.py
54 lines (38 loc) · 1.34 KB
/
video_impl.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from abc import abstractmethod
from typing import Optional
from cv2 import VideoCapture
import cv2
from numpy import ndarray
from media_library import PosePosition, VideoMetadata, get_video_file_path
class VideoFramesBase:
@abstractmethod
def is_open(self) -> bool:
pass
@abstractmethod
def read_frame(self) -> Optional[ndarray]:
pass
class RecordedCV2VideoFrames(VideoFramesBase):
def __init__(self, metadata: VideoMetadata):
self.metadata = metadata
self.video = cv2.VideoCapture(get_video_file_path(metadata.id))
self.last_frame = -1
def is_open(self) -> bool:
return self.video.isOpened()
def read_frame(self) -> Optional[ndarray]:
success, frame = self.video.read()
if not success:
return None
self.last_frame += 1
return frame
def last_frame_pose(self) -> list[PosePosition]:
return self.metadata.positions[self.last_frame]
class CV2VideoFrames(VideoFramesBase):
def __init__(self, camera_capture: VideoCapture):
self.camera_capture = camera_capture
def is_open(self) -> bool:
return self.camera_capture.isOpened()
def read_frame(self) -> Optional[ndarray]:
success, frame = self.camera_capture.read()
if not success:
return None
return frame