欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python利用opencv保存、播放視頻

 更新時(shí)間:2020年11月02日 09:52:55   作者:天喬巴夏丶  
這篇文章主要介紹了python利用opencv保存、播放視頻,幫助大家更好的利用python處理視頻,感興趣的朋友可以了解下

代碼已上傳至:https://gitee.com/tqbx/python-opencv/tree/master/Getting_started_videos

目標(biāo)

學(xué)習(xí)讀取視頻,播放視頻,保存視頻。
學(xué)習(xí)從相機(jī)中捕捉幀并展示。
學(xué)習(xí)cv2.VideoCapture(),cv2.VideoWriter()的使用

從相機(jī)中捕捉視頻

通過自帶攝像頭捕捉視頻,并將其轉(zhuǎn)化為灰度視頻顯示出來。

基本步驟如下:

1.首先創(chuàng)建一個(gè)VideoCapture對(duì)象,它的參數(shù)包含兩種:

  • 設(shè)備索引,指定攝像機(jī)的編號(hào)。
  • 視頻文件的名稱。

2.逐幀捕捉。

3.釋放捕捉物。

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
  print("Cannot open camera")
  exit()
while True:
  # Capture frame-by-frame
  ret, frame = cap.read()
  # if frame is read correctly ret is True
  if not ret:
    print("Can't receive frame (stream end?). Exiting ...")
    break
  # Our operations on the frame come here
  gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
  # Display the resulting frame
  cv.imshow('frame', gray)
  if cv.waitKey(1) == ord('q'):
    break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

其他:

  • cap.read()返回布爾值,如果frame讀取正確,為True,可以通過這個(gè)值判斷視頻是否已經(jīng)結(jié)束。
  • 有時(shí),cap可能會(huì)初始化捕獲失敗,可以通過cap.isOpened()來檢查其是否被初始化,如果為True那是最好,如果不是,可以使用cap.open()來嘗試打開它。
  • 當(dāng)然,你可以使用cap.get(propId)的方式獲取視頻的一些屬性,如幀的寬度,幀的高度,幀速等。propId是0-18的數(shù)字,每個(gè)數(shù)字代表一個(gè)屬性,對(duì)應(yīng)關(guān)系見底部附錄。
  • 既然可以獲取,當(dāng)然也可以嘗試設(shè)置,假設(shè)想要設(shè)置幀的寬度和高度為320和240:cap.set(3,320), cap.set(4,240)。

從文件中播放視頻

代碼和從相機(jī)中捕獲視頻基本相同,不同之處在于傳入VideoCapture的參數(shù),此時(shí)傳入視頻文件的名稱。

在顯示每一幀的時(shí)候,可以使用cv2.waitKey()設(shè)置適當(dāng)?shù)臅r(shí)間,如果值很小,視頻將會(huì)很快。正常情況下,25ms就ok。

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
  ret, frame = cap.read()

  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  cv2.imshow('frame',gray)
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

保存視頻

1.創(chuàng)建一個(gè)VideoWriter 對(duì)象,指定如下參數(shù):

  • 輸出的文件名,如output.avi。
  • FourCC code。
  • 每秒的幀數(shù)fps。
  • 幀的size。

2.FourCC code傳遞有兩種方式:

  • fourcc = cv2.VideoWriter_fourcc(*'XVID')
  • fourcc = cv2.VideoWriter_fourcc('X','V','I','D')

3.FourCC是一個(gè)用于指定視頻編解碼器的4字節(jié)代碼。

  • In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
  • In Windows: DIVX (More to be tested and added)
  • In OSX : (I don't have access to OSX. Can some one fill this?)
import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
  ret, frame = cap.read()
  if ret==True:
    frame = cv2.flip(frame,0)

    # write the flipped frame
    out.write(frame)

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
      break
  else:
    break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

附錄

  • CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
  • CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
  • CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
  • CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
  • CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
  • CV_CAP_PROP_FPS Frame rate.
  • CV_CAP_PROP_FOURCC 4-character code of codec.
  • CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
  • CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
  • CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
  • CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
  • CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
  • CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
  • CV_CAP_PROP_HUE Hue of the image (only for cameras).
  • CV_CAP_PROP_GAIN Gain of the image (only for cameras).
  • CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
  • CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
  • CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
  • CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)

參考閱讀

Getting Started with Videos

作者:天喬巴夏丶
出處:https://www.cnblogs.com/summerday152/
本文已收錄至Gitee:https://gitee.com/tqbx/JavaBlog
若有興趣,可以來參觀本人的個(gè)人小站:https://www.hyhwky.com

以上就是python利用opencv保存、播放視頻的詳細(xì)內(nèi)容,更多關(guān)于python opencv的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Pandas中DataFrame的分組/分割/合并的實(shí)現(xiàn)

    Pandas中DataFrame的分組/分割/合并的實(shí)現(xiàn)

    這篇文章主要介紹了Pandas中DataFrame的分組/分割/合并的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python語音信號(hào)處理詳細(xì)教程

    python語音信號(hào)處理詳細(xì)教程

    在深度學(xué)習(xí)中,語音的輸入都是需要處理的,下面這篇文章主要給大家介紹了關(guān)于python語音信號(hào)處理的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Python命令行解析工具argparse模塊

    Python命令行解析工具argparse模塊

    這篇文章主要介紹了Python命令行解析工具argparse模塊,argparse模塊是一個(gè)python標(biāo)準(zhǔn)庫,它主要用于對(duì)用戶從客戶端輸入的命令進(jìn)行解析,這使得編寫用戶友好的命令行接口變得非常容易,需要的朋友可以參考下
    2023-05-05
  • 最近Python有點(diǎn)火? 給你7個(gè)學(xué)習(xí)它的理由!

    最近Python有點(diǎn)火? 給你7個(gè)學(xué)習(xí)它的理由!

    最近Python有點(diǎn)火?這篇文章主要為大家分享了7個(gè)你現(xiàn)在就該學(xué)習(xí)Python的理由,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 基于python純函數(shù)實(shí)現(xiàn)井字棋游戲

    基于python純函數(shù)實(shí)現(xiàn)井字棋游戲

    這篇文章主要介紹了基于python純函數(shù)實(shí)現(xiàn)井字棋游戲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • python解包用法詳解

    python解包用法詳解

    在本篇文章里小編給大家整理的是關(guān)于python解包用法詳解內(nèi)容,有需要的朋友們可以跟著一起學(xué)習(xí)下。
    2021-02-02
  • python基于numpy的線性回歸

    python基于numpy的線性回歸

    這篇文章主要為大家詳細(xì)介紹了python基于numpy的線性回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • 利用Python編寫一個(gè)簡單的緩存系統(tǒng)

    利用Python編寫一個(gè)簡單的緩存系統(tǒng)

    今天來做一個(gè)最簡單的例子,利用寫一個(gè)最簡單的緩存系統(tǒng),以key``value的方式保持?jǐn)?shù)據(jù),并且需要將內(nèi)容中的數(shù)據(jù)落地到文件,以便下次啟動(dòng)的時(shí)候,將文件的內(nèi)容加載進(jìn)內(nèi)存中來,感興趣的可以了解一下
    2023-04-04
  • Django實(shí)現(xiàn)圖片上傳功能步驟解析

    Django實(shí)現(xiàn)圖片上傳功能步驟解析

    這篇文章主要介紹了Django實(shí)現(xiàn)圖片上傳功能步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Python快速實(shí)現(xiàn)分列轉(zhuǎn)到行的示例代碼

    Python快速實(shí)現(xiàn)分列轉(zhuǎn)到行的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Python快速實(shí)現(xiàn)分列轉(zhuǎn)到行的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)一下
    2023-03-03

最新評(píng)論