Return sorted image paths for a directory, or extract video frames to a temp dir. Returns: tuple[list[str], str | None]: image paths and a temporary directory to remove after loading. The temp directory is only used for video inputs.
(path: str)
| 34 | |
| 35 | |
| 36 | def parse_seq_path(path: str): |
| 37 | """Return sorted image paths for a directory, or extract video frames to a temp dir. |
| 38 | |
| 39 | Returns: |
| 40 | tuple[list[str], str | None]: image paths and a temporary directory to remove |
| 41 | after loading. The temp directory is only used for video inputs. |
| 42 | """ |
| 43 | if os.path.isdir(path): |
| 44 | img_paths = sorted(glob.glob(os.path.join(path, "*"))) |
| 45 | return img_paths, None |
| 46 | |
| 47 | cap = cv2.VideoCapture(path) |
| 48 | if not cap.isOpened(): |
| 49 | raise ValueError(f"Error opening video file {path}") |
| 50 | |
| 51 | video_fps = cap.get(cv2.CAP_PROP_FPS) |
| 52 | total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
| 53 | if video_fps == 0: |
| 54 | cap.release() |
| 55 | raise ValueError(f"Error: Video FPS is 0 for {path}") |
| 56 | |
| 57 | frame_indices = list(range(total_frames)) |
| 58 | print(f" - Video FPS: {video_fps}, Frame Interval: 1, Total Frames to Read: {len(frame_indices)}") |
| 59 | |
| 60 | tmpdirname = tempfile.mkdtemp() |
| 61 | img_paths = [] |
| 62 | for frame_idx in frame_indices: |
| 63 | cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx) |
| 64 | ok, frame = cap.read() |
| 65 | if not ok: |
| 66 | break |
| 67 | frame_path = os.path.join(tmpdirname, f"frame_{frame_idx:06d}.jpg") |
| 68 | cv2.imwrite(frame_path, frame) |
| 69 | img_paths.append(frame_path) |
| 70 | |
| 71 | cap.release() |
| 72 | return img_paths, tmpdirname |
| 73 | |
| 74 | |
| 75 | def _resize_pil_image(img, long_edge_size): |