forked from AlbertoCasasOrtiz/MediaPipeMotionLabeller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoProcessor.py
More file actions
77 lines (61 loc) · 2.58 KB
/
Copy pathVideoProcessor.py
File metadata and controls
77 lines (61 loc) · 2.58 KB
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
import cv2
from mediapipewrapper.MediaPipeWrapper import MediaPipeWrapper
class VideoProcessor:
def __init__(self, path=None):
# Reference to video.
self.video_path = path
self.video = None
self.video_width = None
self.video_height = None
self.num_frames = None
self.duration = None
self.frames_per_second = None
self.data = {}
# Media pipe wrapper
self.mediapipe_wrapper = None
# If path is provided.
if path is not None:
self.load_video(self.video_path)
def __del__(self):
if self.video is not None and self.video.isOpened():
self.video.release()
def load_video(self, path):
# Load video.
self.video_path = path
self.video = cv2.VideoCapture(path)
if not self.video.isOpened():
raise ValueError("Unable to open video source", self.video)
# Get width and height of the video.
self.video_width = self.video.get(cv2.CAP_PROP_FRAME_WIDTH)
self.video_height = self.video.get(cv2.CAP_PROP_FRAME_HEIGHT)
# Get num frames, duration and frames per second.
self.num_frames = int(self.video.get(cv2.CAP_PROP_FRAME_COUNT))
self.frames_per_second = self.video.get(cv2.CAP_PROP_FPS)
self.duration = round(self.num_frames / self.frames_per_second)
print("Num. Frames:", self.num_frames)
print("Frames per Second (fps):", self.frames_per_second)
print("Duration (s):", self.duration)
def get_frame(self):
if self.video is not None and self.video.isOpened():
ret, frame = self.video.read()
if ret:
# To improve performance, optionally mark the image as not writeable to pass by reference.
frame.flags.writeable = False
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Return a boolean success flag and the current frame converted to BGR
return ret, frame
else:
return ret, None
else:
return False, None
def get_current_frame_number(self):
if self.video is not None and self.video.isOpened():
return self.video.get(cv2.CAP_PROP_POS_FRAMES)
else:
return 0
def set_to_frame(self, frame):
self.video.set(cv2.CAP_PROP_POS_FRAMES, frame)
def process_full_video_mediapipe(self):
self.mediapipe_wrapper = MediaPipeWrapper(None, self.video_path)
self.mediapipe_wrapper.run(True)
return self.mediapipe_wrapper.get_captured_data()