python cv2把视频转化为 图片

convert video to pic

def video_pic(path):
    cap = cv2.VideoCapture(path)
    sucess = cap.isOpened()
    frame_count = 0
    i = 0
    while sucess:
        frame_count += 1
        sucess, frame = cap.read()
        if (frame_count % 20 == 0):
            i += 1
            cv2.imwrite('/home/sy/s/s%d.jpg' % i, frame)
    cap.release()

   if __name__ == '__main__':
    path = "/home/sy/data/vison/C1-11F(开发者).mp4"
    video_pic(path)

产生pic相对应的,空的.txt 文件

def generate_empty_lable(src):
    files = os.listdir(src)
    for file in files:
        file = file.split('.')
        txt_file_path = src + file[0] + '.txt'
        txt_file = open(txt_file_path, 'w')
        txt_file.write(' ')

移动确定后缀的文件到指定文件夹中

def move_format_file(src, dst, format):
    files = os.listdir(src)
    for file in files:
        file = file.split('.')
        if (file[-1] == format):
            src_file = src + file[0] + '.' + file[1]
            dst_file = dst + file[0] + '.' + format
            shutil.move(src_file, dst_file)
        else:
            continue

产生训练pic的路径文件

def generate_train(pic_path, dst_file):
    f = open(dst_file, 'w')
    files = os.listdir(pic_path)
    for file in files:
        file = file.split('.')
        if file[-1] == 'jpg':
            line = pic_path + file[0] + '.jpg\n'
            f.writelines(line)
    f.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值