pycharmers.opencv.video_image_handler module

pycharmers.opencv.video_image_handler.mono_frame_generator(path, frame_no=0)[source]

Mono frame Generator which displays a single frame in a video or single image in a directory.

Parameters
  • path (str) – path/to/images/directory or path/to/video.mp4

  • frame_no (int) – If specified (>0), the image can be displayed from a specific positions.

Returns

generator

Examples

>>> from pycharmers.opencv import mono_frame_generator
>>> from pycharmers.opencv import PYCHARMERS_OPENCV_IMAGE_DIR
>>> for img in mono_frame_generator(path=PYCHARMERS_OPENCV_IMAGE_DIR):
...     print(img.shape)
(512, 512, 3)
pycharmers.opencv.video_image_handler.multi_frame_generator_sepa(*path, frame_no=0)[source]

Multiple frame generator. (separatory)

Parameters
  • path (str) – path/to/images/directory or path/to/video.mp4

  • frame_no (int) – If specified (>0), the image can be displayed from a specific positions.

Returns

generator

Examples

>>> from pycharmers.opencv import multi_frame_generator_sepa
>>> from pycharmers.opencv import PYCHARMERS_OPENCV_IMAGE_DIR
>>> gen = multi_frame_generator_sepa(PYCHARMERS_OPENCV_IMAGE_DIR, PYCHARMERS_OPENCV_IMAGE_DIR)
>>> for img in gen:
...     print(len(img), img[0].shape)
2 (512, 512, 3)
pycharmers.opencv.video_image_handler.multi_frame_generator_concat(*paths, frame_no=0, grid=None)[source]

Multiple frame generator. (In a connected state)

Parameters
  • path (str) – path/to/images/directory or path/to/video.mp4

  • frame_no (int) – If specified (>0), the image can be displayed from a specific positions.

  • grid (tuple) – How to concatenate the multiple frames. (ncols, nrows)

Returns

generator

Examples

>>> from pycharmers.opencv import multi_frame_generator_concat
>>> from pycharmers.opencv import PYCHARMERS_OPENCV_IMAGE_DIR
>>> gen = multi_frame_generator_concat(PYCHARMERS_OPENCV_IMAGE_DIR, PYCHARMERS_OPENCV_IMAGE_DIR, grid=(1,2))
>>> for img in gen:
...     print(img.shape)
(512, 1024, 3)
pycharmers.opencv.video_image_handler.count_frame_num(path)[source]

Count the number of frames.

Parameters

path (str) – path to video file, or directory which stores sequential images.

Examples

>>> from pycharmers.opencv import count_frame_num, SAMPLE_VTEST_VIDEO, PYCHARMERS_OPENCV_IMAGE_DIR
>>> count_frame_num(SAMPLE_VTEST_VIDEO)
795
>>> count_frame_num(PYCHARMERS_OPENCV_IMAGE_DIR)
1
pycharmers.opencv.video_image_handler.basenaming(path)[source]

Returns the final component of a pathname.

  • If path indicates video file (path/to/sample.mp4) -> sample

  • If path indicates directory (path/to/sample) -> sample

Parameters

path (str) – path to video file, or directory which stores sequential images.

Examples

>>> import os
>>> from pycharmers.opencv import basenaming
>>> os.path.exists("path/to/sample.mp4")
True
>>> basenaming("path/to/sample.mp4")
'sample'
>>> basenaming("path/to/sample")
'sample'
>>> os.path.exists("path/to/sample2.mp4")
False
>>> basenaming("path/to/sample_.mp4")
'sample_.mp4'
pycharmers.opencv.video_image_handler.VideoWriterCreate(input_path: Optional[str] = None, out_path: Optional[str] = None, codec: str = 'MP4V', fps: Optional[float] = None, size: Tuple[int, int] = None, None, verbose: bool = False, **kwargs) → Tuple[bool, cv2.VideoWriter, str][source]

Create a cv2.VideoWriter which creates a video whose option is same as that of input.

Parameters
  • input_path (Optional[str], optional) – Input media path for video/image file or image directory. Defaults to None.

  • out_path (Optional[str], optional) – Output path for the created video. Defaults to None.

  • codec (str, optional) – A video codec for the created output video.

  • fps (float, optional) – Frames Per Second. Defaults to None.

  • size (Tuple[int, int], optional) – frame size for the created video. Defaults to (None, None).

  • verbose (bool, optional) – Whether to print the created cv2.VideoWriter info. Defaults to False.

Returns

A tuple of three elements.
  • flag if VideoWriter is created correctly, , A instance of cv2.VideoWriter.

  • A instance of created VideoWriter.

  • Output path for VideoWriter.

Return type

Tuple[bool, cv2.VideoWriter, str]

Examples

>>> from pycharmers.opencv import VideoWriterCreate
>>> is_ok, VideoWriter = VideoWriterCreate(input_path=None, fps=30., height=360, width=480)
(True, <VideoWriter 0x12597def0>)
>>> is_ok, VideoWriter = VideoWriterCreate(input_path="path/to/video.mp4")
(True, <VideoWriter 0x125345050>)
>>> is_ok, VideoWriter = VideoWriterCreate(input_path="path/to/image_dir")
(True, <VideoWriter 0x125345d70>)
Raises

TypeError – When not enough information such as fps, width, or height.

pycharmers.opencv.video_image_handler.VideoCaptureCreate(path=None, cam=0)[source]

Create a VideoCapture (mimic) object.

Parameters
  • path (str) – path to video or image.

  • cam (int) – The ID of the web camera

Returns

VideoCapture (mimic) object.

Return type

cap (cv2.VideoCapture)

Examples

>>> from pycharmers.opencv import VideoCaptureCreate, cv2plot
>>> from pycharmers.opencv import SAMPLE_LENA_IMG, SAMPLE_VTEST_VIDEO
>>> for path in [SAMPLE_LENA_IMG, SAMPLE_VTEST_VIDEO, None]:
...     cap = VideoCaptureCreate(path=path, cam=0)
...     ret,frame = cap.read()
...     cv2plot(frame)
...     cap.release()
pycharmers.opencv.video_image_handler.videocodec2ext(*codec) → str[source]

Convert video codec to video extension.

Parameters

codec (Union[tuple, str]) – Video Codec.

Returns

Ideal file extension.

Return type

str

Examples

>>> from pycharmers.opencv import videocodec2ext
>>> videocodec2ext("MP4V")
'.mp4'
>>> videocodec2ext("mp4v")
'.mov'
>>> videocodec2ext("VP80")
'.webm'
>>> videocodec2ext("XVID")
'.avi
>>> videocodec2ext("☺️")
'.mp4'
Raises

KeyError – When the file extension cannot be inferred from the video codec.