opencv-python-headless
Wraps the OpenCV Python bindings.
- Downloads / mo
- 64.1M
- Stars
- 5.5K
- Last release
- 5.1.0.95
- License
- Apache-2.0
Quickstart
import cv2 capture = cv2.VideoCapture('input.mp4') ok, frame = capture.read() capture.release()
opencv-python-headless examples
def _open_capture(self, frame_rate: FrameRate | None = None): """Opens capture referenced by this object and resets internal state.""" if self._is_device: assert isinstance(self._path_or_device, int) if self._path_or_device < 0: raise ValueError("Invalid/negative device ID specified.") input_is_video_file = False capture_api = cv2.CAP_ANY else: assert isinstance(self._path_or_device, str) capture_api = _get_capture_api(self._path_or_device) input_is_video_file = not any( identifier in self._path_or_device for identifier in NON_VIDEO_FILE_INPUT_IDENTIFIERS ) # We don't have a way of querying why opening a video fails (errors are logged at # least), so provide a better error message if we try to open a missing file. if input_is_video_file and not os.path.exists(self._path_or_device): raise OSError("Video file not found.") cap = cv2.VideoCapture(self._path_or_device, capture_api) if not cap.isOpened(): raise VideoOpenFailure( "Ensure file is valid video and system dependencies are up to date.\n" ) # Display an error if the video codec type seems unsupported (#86) as this indicates # potential video corruption, or may explain missing frames. We only perform this check # for video files on-disk (skipped for devices, image sequences, streams, etc...). codec_unsupported: bool = int(abs(cap.get(cv2.CAP_PROP_FOURCC))) == 0 if codec_unsupported and input_is_video_file: logger.error( "Video codec detection failed. If output is incorrect:\n" " - Re-encode the input video with ffmpeg\n" " - Update OpenCV (pip install --upgrade opencv-python)\n" " - Use the PyAV backend (--backend pyav)\n" "For details, see https://github.com/Breakthrough/PySceneDetect/issues/86" ) # Ensure the framerate is correct to avoid potential divide by zero errors. This can be # addressed in the PyAV backend if required since it supports integer timebases. assert frame_rate is None or frame_rate > MAX_FPS_DELTA, ( "Frame rate must be validated if set!" ) if frame_rate is None: frame_rate = cap.get(cv2.CAP_PROP_FPS) if frame_rate < MAX_FPS_DELTA: raise FrameRateUnavailable() self._cap: cv2.VideoCapture = cap self._frame_rate: Fraction = framerate_to_fraction(frame_rate) self._has_grabbed = False self._eof_timecode: Timecode | None = None cap.set(cv2.CAP_PROP_ORIENTATION_AUTO, 1.0) # https://github.com/opencv/opencv/issues/26795