[經驗]解決使用opencv 高分辨率下的攝像頭卡頓不流暢

      網友投稿 2638 2022-05-30

      目錄

      發現

      分析

      解決

      總結

      發現

      環境

      ubuntu 16.04

      python 2.7 (anaconda 的環境)

      cv 版本 3.4

      安裝方式 pip install opencv-contrib-python

      問題

      最近在做一個項目,需要使用opencv 打開攝像頭,然后錄制視頻,在默認的參數下,opencv打開攝像頭都是非常流暢的,但是在高分辨率下,攝像頭使用如下的代碼的時候就非常的不流暢,而且很卡,比如如下的代碼做演示就非常的卡

      import time import numpy as np import cv2 resv = (1920, 1080) def show_video(): cap = cv2.VideoCapture(1) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) # cap.set(cv2.CAP_PROP_BUFFERSIZE,10) while (True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here # gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame # print(frame.shape) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      import time

      import numpy as np

      import cv2

      resv = (1920, 1080)

      def show_video():

      cap = cv2.VideoCapture(1)

      cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)

      cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

      # cap.set(cv2.CAP_PROP_BUFFERSIZE,10)

      while (True):

      # Capture frame-by-frame

      ret, frame = cap.read()

      # Our operations on the frame come here

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

      # Display the resulting frame

      # print(frame.shape)

      cv2.imshow('frame', frame)

      if cv2.waitKey(1) & 0xFF == ord('q'):

      break

      # When everything done, release the capture

      cap.release()

      cv2.destroyAllWindows()

      在代碼中,我設定了視頻的分辨率為1080p , 這個時候演示出來的視頻畫面就非常的卡,是攝像頭本身就不支持嗎?我看了自己的設備參數 ,標明設備是支持1080p的

      分析

      我打開ubuntu系統的自帶camera cheese 工具,不論是錄制,還是拍攝,都是1080p無卡頓現象,這就說明本身不是設備的問題,這個時候我在想,是否有參數可以設置FPS,嘗試在代碼中加入

      cap.set(cv2.CAP_PROP_FPS, 30)

      1

      2

      cap.set(cv2.CAP_PROP_FPS, 30)

      將FPS設定在30幀,依然沒有效果?難道是opencv的問題,嘗試使用google 搜索關鍵字 opencv python set fps fake 得到答案

      CV_CAP_PROP_FPS is a NOT a fake. See cap_libv4l.cpp(1) in OpenCV github repo. The key is to make sure, you use libv4l over v4l while configuring OpenCV. For that, before running cmake, install libv4l-dev sudo apt-get install libv4l-dev Now while configuring OpenCV with cmake, enable option, WITH_LIBV4L. If all goes good, in configuration status, you will see some thing similar to below

      1

      2

      3

      4

      5

      CV_CAP_PROP_FPS is a NOT a fake. See cap_libv4l.cpp(1) in OpenCV github repo. The key is to make sure, you use libv4l over v4l while configuring OpenCV. For that, before running cmake, install libv4l-dev

      [經驗]解決使用opencv 高分辨率下的攝像頭卡頓不流暢

      sudo apt-get install libv4l-dev

      Now while configuring OpenCV with cmake, enable option, WITH_LIBV4L. If all goes good, in configuration status, you will see some thing similar to below

      從答案里面得知,我們需要使用libv4l庫,并且,在使用opencv 編譯版本的時候,要開啟這個feature ,很明顯 ,當前我使用的opencv應該是直裝版本,沒有經過編譯的,那知道問題在哪,我們進行嘗試

      解決

      安裝依賴項

      sudo apt-get install libv4l-dev

      1

      2

      sudo apt-get install libv4l-dev

      下載opencv 源碼

      https://codeload.github.com/opencv/opencv/tar.gz/3.4.2 ,本次使用的3.4.2的版本

      卸載之前的opencv

      pip uninstall opencv-contrib-python

      1

      2

      pip uninstall opencv-contrib-python

      編譯opencv

      進入opencv 目錄

      mkdir build cd build cmake -D WITH_LIBV4L=ON ../ make -j8 make install ldconfig cp lib/cv2.so /home/bruce/miniconda2/lib/python2.7/site-packages # 注意,這里是當前使用的python環境

      1

      2

      3

      4

      5

      6

      7

      8

      9

      mkdir build

      cd build

      cmake -D WITH_LIBV4L=ON ../

      make -j8

      make install

      ldconfig

      cp lib/cv2.so /home/bruce/miniconda2/lib/python2.7/site-packages # 注意,這里是當前使用的python環境

      驗證

      這次使用的是錄制視頻并輸出視頻的代碼

      def simple_video(minutes=5): start = int(time.time()) cap = cv2.VideoCapture(1) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) cap.set(cv2.CAP_PROP_FPS, 25) # cap.set(cv2.CV_CAP_PROP_BUFFERSIZE, 20) # Define the codec and create VideoWriter objectXVID # fourcc = cv2.VideoWriter_fourcc(*'XVID') fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('{}.avi'.format(str(start)), fourcc, 25.0, (1920, 1080)) while (cap.isOpened()): step = int(time.time()) ret, frame = cap.read() if ret == True: frame = cv2.flip(frame, 0) # img = cv2.resize(frame,(1920,1080)) # write the flipped frame out.write(frame) cv2.imshow('frame', frame) if (cv2.waitKey(1) & 0xFF == ord('q')) or ((step - start) >= minutes * 60): print("enter to the time used") break else: break # Release everything if job is finished cap.release() out.release() cv2.destroyAllWindows() if __name__ == '__main__': simple_video(1)

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      def simple_video(minutes=5):

      start = int(time.time())

      cap = cv2.VideoCapture(1)

      cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)

      cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

      cap.set(cv2.CAP_PROP_FPS, 25)

      # cap.set(cv2.CV_CAP_PROP_BUFFERSIZE, 20)

      # Define the codec and create VideoWriter objectXVID

      # fourcc = cv2.VideoWriter_fourcc(*'XVID')

      fourcc = cv2.VideoWriter_fourcc(*'XVID')

      out = cv2.VideoWriter('{}.avi'.format(str(start)), fourcc, 25.0, (1920, 1080))

      while (cap.isOpened()):

      step = int(time.time())

      ret, frame = cap.read()

      if ret == True:

      frame = cv2.flip(frame, 0)

      # img = cv2.resize(frame,(1920,1080))

      # write the flipped frame

      out.write(frame)

      cv2.imshow('frame', frame)

      if (cv2.waitKey(1) & 0xFF == ord('q')) or ((step - start) >= minutes * 60):

      print("enter to the time used")

      break

      else:

      break

      # Release everything if job is finished

      cap.release()

      out.release()

      cv2.destroyAllWindows()

      if __name__ == '__main__':

      simple_video(1)

      錄制了1分鐘的視頻,看了一下結果,還不錯,非常流暢,成功解決這個問題

      總結

      這次我們主要從,發現問題,假設問題,搜索問題的幾個角度 來成功解決我們在工程上的一些問題,突破點就是驗證自己的想法,使用正常的關鍵字去尋找問題的答案,OK,這次的分享就到這里

      參考文檔:

      – 在ubunt下安裝opencv

      OpenCV 視頻

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:excel表格按日期計算天數的方法步驟
      下一篇:qt5 中文亂碼解決
      相關文章
      久久国产亚洲精品| 亚洲中文无码a∨在线观看| 亚洲精品亚洲人成在线| 亚洲成人在线免费观看| 亚洲精彩视频在线观看| 久久亚洲日韩精品一区二区三区| 亚洲AV中文无码字幕色三| 亚洲狠狠婷婷综合久久久久| 伊人久久大香线蕉亚洲| 国产av无码专区亚洲av果冻传媒| 国产亚洲欧洲Aⅴ综合一区| 国产性爱在线观看亚洲黄色一级片| 亚洲午夜精品第一区二区8050| 亚洲不卡AV影片在线播放| 亚洲AV永久无码精品一区二区国产 | 亚洲第一第二第三第四第五第六| 亚洲夂夂婷婷色拍WW47| 亚洲啪AV永久无码精品放毛片| 亚洲国产精品自在自线观看| 国产亚洲欧美日韩亚洲中文色| 亚洲av成人中文无码专区| 亚洲成aⅴ人片久青草影院| 亚洲精品天堂成人片?V在线播放| 久久影院亚洲一区| 亚洲精品成人无码中文毛片不卡 | 一区二区亚洲精品精华液| 亚洲人成自拍网站在线观看| 亚洲AV无码专区在线观看成人| 人人狠狠综合久久亚洲| 亚洲第一页日韩专区| 亚洲一区AV无码少妇电影☆| 亚洲第一中文字幕| 亚洲女人初试黑人巨高清| 亚洲综合色婷婷在线观看| 日韩成人精品日本亚洲| 国产亚洲一区区二区在线 | 国产亚洲一区二区三区在线观看| 亚洲国产精品lv| 亚洲乱码中文字幕小综合| 亚洲色成人网站WWW永久四虎 | 亚洲欧洲一区二区三区|