コード例 #1
0
def frames2video(frame_dir,
                 video_file,
                 fps=30,
                 fourcc='XVID',
                 filename_digit=6,
                 ext='jpg',
                 start=0,
                 end=0):
    """read the frame images from a directory and join them as a video
    """
    if end == 0:
        max_idx = len([name for name in scandir(frame_dir, ext)]) - 1
    else:
        max_idx = end
    first_file = path.join(frame_dir,
                           '{0:0{1}d}.{2}'.format(start, filename_digit, ext))
    check_file_exist(first_file, 'The start frame not found: ' + first_file)
    img = cv2.imread(first_file)
    height, width = img.shape[:2]
    vwriter = cv2.VideoWriter(video_file,
                              VideoWriter_fourcc(*fourcc), fps,
                              (width, height))
    idx = start
    while idx <= max_idx:
        filename = path.join(frame_dir,
                             '{0:0{1}d}.{2}'.format(idx, filename_digit, ext))
        img = cv2.imread(filename)
        vwriter.write(img)
        idx += 1
    vwriter.release()
コード例 #2
0
 def __init__(self, filename, cache_capacity=0):
     check_file_exist(filename, 'Video file not found: ' + filename)
     self._vcap = cv2.VideoCapture(filename)
     self._cache = Cache(cache_capacity) if cache_capacity > 0 else None
     self._position = 0
     # get basic info
     self._width = int(self._vcap.get(CAP_PROP_FRAME_WIDTH))
     self._height = int(self._vcap.get(CAP_PROP_FRAME_HEIGHT))
     self._fps = int(round(self._vcap.get(CAP_PROP_FPS)))
     self._frame_cnt = int(self._vcap.get(CAP_PROP_FRAME_COUNT))
     self._fourcc = self._vcap.get(CAP_PROP_FOURCC)
コード例 #3
0
def read_img(img_or_path):
    """Read an image

    Args:
        img_or_path(ndarray or str): either an image or path of an image
    Output:
        ndarray: image array
    """
    if isinstance(img_or_path, np.ndarray):
        return img_or_path
    elif isinstance(img_or_path, str):
        check_file_exist(img_or_path,
                         'img file does not exist: {}'.format(img_or_path))
        return cv2.imread(img_or_path)
    else:
        raise TypeError('"img" must be a numpy array or a filename')
コード例 #4
0
ファイル: image.py プロジェクト: elviswf/cvbase
def read_img(img_or_path, flag=IMREAD_COLOR):
    """Read an image

    Args:
        img_or_path(ndarray or str): either an image or path of an image
        flag(int): flags specifying the color type of a loaded image

    Returns:
        ndarray: image array
    """
    if isinstance(img_or_path, np.ndarray):
        return img_or_path
    elif isinstance(img_or_path, six.string_types):
        check_file_exist(img_or_path,
                         'img file does not exist: {}'.format(img_or_path))
        return cv2.imread(img_or_path, flag)
    else:
        raise TypeError('"img" must be a numpy array or a filename')
コード例 #5
0
def frames2video(frame_dir,
                 video_file,
                 fps=30,
                 fourcc='XVID',
                 filename_tmpl='{:06d}.jpg',
                 start=0,
                 end=0,
                 show_progress=True):
    """Read the frame images from a directory and join them as a video

    Args:
        frame_dir(str): frame directory
        video_file(str): output video filename
        fps(int): fps of the output video
        fourcc(str): foutcc of the output video, this should be compatible with the output file type
        filename_tmpl(str): filename template, with the index as the variable
        start(int): starting frame index
        end(int): ending frame index
        show_progress(bool): whether to show a progress bar
    """
    if end == 0:
        ext = filename_tmpl.split('.')[-1]
        end = len([name for name in scandir(frame_dir, ext)])
    first_file = path.join(frame_dir, filename_tmpl.format(start))
    check_file_exist(first_file, 'The start frame not found: ' + first_file)
    img = cv2.imread(first_file)
    height, width = img.shape[:2]
    resolution = (width, height)
    vwriter = cv2.VideoWriter(video_file, VideoWriter_fourcc(*fourcc), fps,
                              resolution)

    def write_frame(file_idx):
        filename = path.join(frame_dir, filename_tmpl.format(file_idx))
        img = cv2.imread(filename)
        vwriter.write(img)

    if show_progress:
        track_progress(write_frame, range(start, end))
    else:
        for i in range(start, end):
            filename = path.join(frame_dir, filename_tmpl.format(i))
            img = cv2.imread(filename)
            vwriter.write(img)
    vwriter.release()