将一个文件夹中多个视频的视频帧保存在多个文件夹下

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 25 10:21:18 2019

@author: 车路历程
"""
import cv2
from skimage import io
import os

# 视频所在文件夹的路径位置
videos_path = 'F:/workit/videos'

# 从视频文件夹中获取各个视频的名字
videos_name = os.listdir(videos_path)

# 视频帧图片的起始编号
a = 0

# 遍历所有的视频
for j, i in enumerate(videos_name):
    # 以下两行代码的作用是为每段视频帧创建一个文件夹
    path = 'F:/workit/picture/picture_%s'%(j+1)
    os.makedirs(path)
    # 获取每个视频的路径
    video_path = os.path.join(videos_path, i)
    # 如果遇见文件夹就跳过
    if os.path.isdir(video_path):
        continue
    
    camera = cv2.VideoCapture(video_path) # 参数0表示第一个摄像头

    # 判断视频是否打开
    if (camera.isOpened()):
        print('Open')
    else:
        print('摄像头未打开')
    
    # 测试用,查看视频size
    size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    print('size:'+repr(size))
    
    while True:
        # 读取视频流
        grabbed, frame_lwpCV = camera.read()
        # 当从视频中读取不到视频帧就退出
        if frame_lwpCV is None:
            break
        
        # 保存图片
        io.imsave("F:/workit/picture/picture_%s/%s.jpg"%(j+1, a), frame_lwpCV)
        a += 1
        
        # 显示图片
        cv2.imshow('contours', frame_lwpCV)
     
        key = cv2.waitKey(1) & 0xFF
        # 按'q'健退出循环
        if key == ord('q'):
            break
    
# When everything done, release the capture
camera.release()
cv2.destroyAllWindows()

 

posted @ 2019-02-27 15:43  车路历程  阅读(542)  评论(0编辑  收藏  举报