zoukankan      html  css  js  c++  java
  • Python-OpenCV中VideoCapture类的使用

    目录


        主要记录Python-OpenCV中的VideoCapture类的使用;官方文档

      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))
      

    • 相关阅读:
      含有打印、统计DataGridView(1)
      数字金额转换大写人民币
      文件加密解密全解
      正则表达式之全部符号对照表
      C#程序集引入无效的解决方法
      TreeView 的简单实用
      Win7下用IIS发布网站
      C#做完一个网站怎么发布?
      c# 如何获取项目的根目录
      判断控件是否出现了滚动条
    • 原文地址:https://www.cnblogs.com/chenzhen0530/p/10746734.html
    Copyright © 2011-2022 走看看