[經驗]解決使用opencv 高分辨率下的攝像頭卡頓不流暢
目錄
發現
分析
解決
總結
發現
環境
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
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小時內刪除侵權內容。