Args: video_path (str): path to video frame_inds (List[int]): indices of frames to extract points (List[float]): values within [0, 1); multiply #frames to get frame indices Return: List[PIL.Image]
(
video_path,
frame_inds=None,
points=None,
backend="opencv",
return_length=False,
num_frames=None,
)
| 23 | |
| 24 | |
| 25 | def extract_frames( |
| 26 | video_path, |
| 27 | frame_inds=None, |
| 28 | points=None, |
| 29 | backend="opencv", |
| 30 | return_length=False, |
| 31 | num_frames=None, |
| 32 | ): |
| 33 | """ |
| 34 | Args: |
| 35 | video_path (str): path to video |
| 36 | frame_inds (List[int]): indices of frames to extract |
| 37 | points (List[float]): values within [0, 1); multiply #frames to get frame indices |
| 38 | Return: |
| 39 | List[PIL.Image] |
| 40 | """ |
| 41 | assert backend in ["av", "opencv", "decord"] |
| 42 | assert (frame_inds is None) or (points is None) |
| 43 | assert backend == "opencv" |
| 44 | |
| 45 | cap = cv2.VideoCapture(video_path) |
| 46 | if num_frames is not None: |
| 47 | total_frames = num_frames |
| 48 | else: |
| 49 | total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| 50 | |
| 51 | if points is not None: |
| 52 | frame_inds = [int(p * total_frames) for p in points] |
| 53 | |
| 54 | frames = [] |
| 55 | for idx in frame_inds: |
| 56 | if idx >= total_frames: |
| 57 | idx = total_frames - 1 |
| 58 | |
| 59 | success = cap.set(cv2.CAP_PROP_POS_FRAMES, idx) |
| 60 | if not success: |
| 61 | break |
| 62 | |
| 63 | try: |
| 64 | ret, frame = cap.read() |
| 65 | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) |
| 66 | frame = Image.fromarray(frame) |
| 67 | frames.append(frame) |
| 68 | except Exception: |
| 69 | continue |
| 70 | |
| 71 | if return_length: |
| 72 | return frames, total_frames |
| 73 | return frames |
| 74 | |
| 75 | |
| 76 | def merge_scores(gathered_list: list, meta: pd.DataFrame, column): |