OpenCV中VideoCapture類的使用詳解
更新時(shí)間:2020年02月14日 11:38:56 作者:chenzhen0530
這篇文章主要介紹了OpenCV中VideoCapture類的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
主要記錄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) # 獲取視頻總幀數(shù) frame_all = vc.get(cv2.CAP_PROP_FRAME_COUNT) print("[INFO] 視頻FPS: {}".format(fps)) print("[INFO] 視頻總幀數(shù): {}".format(frame_all)) print("[INFO] 視頻時(shí)長: {}s".format(frame_all/fps)) outputPath = os.path.sep.join([args["outputPath"]]) if os.path.exists(outputPath) is False: print("[INFO] 創(chuàng)建文件夾,用于保存提取的幀") os.mkdir(outputPath) # 每隔100幀保存一張圖片 frame_interval = 100 # 統(tǒng)計(jì)當(dāng)前幀 frame_count = 1 # 保存圖片個(gè)數(shù) 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 # 關(guān)閉視頻文件 vc.release() print("[INFO] 總共保存:{}張圖片".format(count))
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
Python3中對(duì)json格式數(shù)據(jù)的分析處理
這篇文章主要介紹了Python3中對(duì)json格式數(shù)據(jù)的分析處理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Django實(shí)現(xiàn)drf搜索過濾和排序過濾
當(dāng)我們需要對(duì)后臺(tái)的數(shù)據(jù)進(jìn)行過濾的時(shí)候,drf有兩種,搜索過濾和排序過濾。本文就詳細(xì)的介紹這兩種的實(shí)現(xiàn),感興趣的可以了解一下2021-06-06在django中實(shí)現(xiàn)choices字段獲取對(duì)應(yīng)字段值
這篇文章主要介紹了在django中實(shí)現(xiàn)choices字段獲取對(duì)應(yīng)字段值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07python用quad、dblquad實(shí)現(xiàn)一維二維積分的實(shí)例詳解
今天小編大家分享一篇python用quad、dblquad實(shí)現(xiàn)一維二維積分的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11