videoSlice.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:Enchain 作者: Zhehua-Hu 项目源码 文件源码
def videoSlice(video_path, save_path, progressbarsetter=None, save_type="png", img_comp=0, start_idx=1):
    """

    :param video_path:
    :param save_path:
    :param save_type:
    :param img_comp: default0:
                    None Higher number increase compressive level
                    png[0-9], jpg[0-100]
    :return:
    """

    # For read Chinease-name video
    vid_handle = cv2.VideoCapture(video_path)
    # vid_handle = cv2.VideoCapture(video_path.encode('utf-8'))
    fps = vid_handle.get(cv2.CAP_PROP_FPS)
    count = vid_handle.get(cv2.CAP_PROP_FRAME_COUNT)
    size = (int(vid_handle.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(vid_handle.get(cv2.CAP_PROP_FRAME_HEIGHT)))

    prefix = os.path.basename(save_path)
    idx = start_idx  # start from 000001.xxx
    cnt_idx = 1

    params = None
    suffix = None
    if save_type.upper() == "JPEG" or save_type.upper() == "JPG":
        img_type = int(cv2.IMWRITE_JPEG_OPTIMIZE)
        suffix = ".jpg"
        params = [img_type, img_comp]
    elif save_type.upper() == "PNG":
        img_type = int(cv2.IMWRITE_PNG_COMPRESSION)
        suffix = ".png"
        params = [img_type, img_comp]
    else:
        print("Do not support %s format!" % save_type)

    while True:
        ret, frame = vid_handle.read()
        if ret:
            cur_progress = cnt_idx/(count/100.0)
            if progressbarsetter is not None:
                progressbarsetter(cur_progress)
            print("Progress %.2f%%" % cur_progress)
            img_name = save_path + "/" + ("%06d" % idx) + suffix
            # print img_name
            print params
            cv2.imwrite(img_name, frame, params)
            idx += 1
            cnt_idx += 1
        else:
            break
    print("Slicing Done!")
    return count
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号