zoukankan      html  css  js  c++  java
  • python-opencv 读取摄像头并保存为.mp4视频 及 VideoCapture()的使用

    import cv2
    import sys
    import time
    
    dt = "2019-01-23 15:29:00"
    #转换成时间数组
    timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
    #转换成时间戳
    timestamp = time.mktime(timeArray)
    print(timeArray)
    print(timestamp)
    
    
    cap_1 = cv2.VideoCapture(1)
    cap_1.set(3,1920)
    cap_1.set(4,1080)
    # cap_2 = cv2.VideoCapture(2)
    # cap_3 = cv2.VideoCapture(3)
    # cap_4 = cv2.VideoCapture(4)
    
    write_ok = False
    
    sz = (int(cap_1.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(cap_1.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    fps = 30
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    # fourcc = cv2.VideoWriter_fourcc(*'mpeg')
    
    
    vout_1 = cv2.VideoWriter()
    vout_1.open('./1/output.mp4',fourcc,fps,sz,True)
    # vout_2 = cv2.VideoWriter()
    # vout_2.open('./2/output.mp4',fourcc,fps,sz,True)
    # vout_3 = cv2.VideoWriter()
    # vout_3.open('./3/output.mp4',fourcc,fps,sz,True)
    
    
    cnt = 0
    while(True):
        if(write_ok):
            # print("video")
            #获取当前时间
            time_now = int(time.time())
            #转换成localtime
            # time_local = time.localtime(time_now)
            print(time_now)
            if time_now >= timestamp:
                while(cnt < 900):
                    cnt += 1
                    print(cnt)
    
                    ret_1, frame_1 = cap_1.read()
                    vout_1.write(frame_1)
    
                    # ret_2, frame_2 = cap_2.read()
                    # vout_2.write(frame_2)
    
                    # ret_3, frame_3 = cap_3.read()
                    # vout_3.write(frame_3)
    
                vout_1.release()
                # vout_2.release()
                # vout_3.release()
                sys.exit()
        else:
            print("stop")
            ret_1, frame_1 = cap_1.read()
            cv2.imshow("cam_1", frame_1)
            # ret_2, frame_2 = cap_2.read()
            # cv2.imshow("cam_2", frame_2)
            # ret_3, frame_3 = cap_3.read()
            # cv2.imshow("cam_3", frame_3)
    
    
    
        if cv2.waitKey(1) & 0xFF==ord("w"):
            write_ok = write_ok is not True

    VideoCapture()的使用 

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    # @Time    : 19-4-21 上午10:31
    # @Author  : chen
    
    """
    VideoCapture()的使用
    """
    import cv2
    import argparse
    import os
    import pdb
    
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--videoPath", default="./video_1.mp4", help="path to input video")
    ap.add_argument("-o", "--outputPath", default="grabImages", help="path to output frames")
    
    args = vars(ap.parse_args())
    
    # 初始化,并读取第一帧
    # rval表示是否成功获取帧
    # frame是捕获到的图像
    vc = cv2.VideoCapture(args["videoPath"])
    rval, frame = vc.read()
    
    # 获取视频fps
    fps = vc.get(cv2.CAP_PROP_FPS)
    # 获取视频总帧数
    frame_all = vc.get(cv2.CAP_PROP_FRAME_COUNT)
    print("[INFO] 视频FPS: {}".format(fps))
    print("[INFO] 视频总帧数: {}".format(frame_all))
    print("[INFO] 视频时长: {}s".format(frame_all/fps))
    
    outputPath = os.path.sep.join([args["outputPath"]])
    if os.path.exists(outputPath) is False:
        print("[INFO] 创建文件夹,用于保存提取的帧")
        os.mkdir(outputPath)
    
    # 每隔100帧保存一张图片
    frame_interval = 100
    # 统计当前帧
    frame_count = 1
    # 保存图片个数
    count = 0
    while rval:
        rval, frame = vc.read()
        if frame_count % frame_interval == 0:
            filename = os.path.sep.join([outputPath, "test_{}.jpg".format(count)])
            cv2.imwrite(filename, frame)
            count += 1
            print("保存图片:{}".format(filename))
        frame_count += 1
    
    # 关闭视频文件
    vc.release()
    print("[INFO] 总共保存:{}张图片".format(count))
  • 相关阅读:
    《算法竞赛进阶指南》0x07贪心 POJ2054 color the tree树的缩点与合并
    《算法竞赛进阶指南》0x07 贪心 NOIP2012 vector模拟高精度计算
    《算法竞赛进阶指南》0x07贪心 POJ1328
    《算法竞赛进阶指南》0x07贪心 POJ3190
    《算法竞赛进阶指南》0x07贪心 POJ3614
    《算法竞赛进阶指南》0x06倍增 Acwing GeniusACM
    8.装饰模式(Decorator Pattern)
    7.桥接模式(Bridge Pattern)
    6.适配器模式(Adapter Pattern)
    5.原型模式(Prototype)
  • 原文地址:https://www.cnblogs.com/Archger/p/12774648.html
Copyright © 2011-2022 走看看