Use OpenCV to open a video source from either file or capture device. Args: video_source: filename or index referring to capture device. Raises: RuntimeError: Source is a file but file not found. RuntimeError: Failed to open source.
(video_source: str | int)
| 111 | |
| 112 | @staticmethod |
| 113 | def open_video(video_source: str | int): |
| 114 | """ |
| 115 | Use OpenCV to open a video source from either file or capture device. |
| 116 | |
| 117 | Args: |
| 118 | video_source: filename or index referring to capture device. |
| 119 | |
| 120 | Raises: |
| 121 | RuntimeError: Source is a file but file not found. |
| 122 | RuntimeError: Failed to open source. |
| 123 | """ |
| 124 | if isinstance(video_source, str) and not os.path.isfile(video_source): |
| 125 | raise RuntimeError("Video file does not exist: " + video_source) |
| 126 | with SuppressStderr(): |
| 127 | cap = cv2.VideoCapture(video_source) |
| 128 | if not cap.isOpened(): |
| 129 | raise RuntimeError(f"Failed to open video: {video_source}") |
| 130 | return cap |
| 131 | |
| 132 | def _get_cap(self): |
| 133 | """Return the cap. If multiprocessing, create a new one. Else return the one from construction time.""" |
no test coverage detected