最近玩jetson nano的時候發(fā)現(xiàn)openCv打開USB攝像頭幀率只有5幀,但是windows下能跑30幀,在網(wǎng)絡(luò)搜了一下原因發(fā)現(xiàn)是攝像頭默認讀取格式不對,需要改成MJPG格式幀率才能上去,于是使用CSDN上找的代碼,但是都不能工作,折騰了好久沒搞定,突然想到上 stacoverflow上看看,沒想到真的解決了,這里做一下記錄。
首先檢查攝像頭設(shè)備是否存在
1.安裝工具
sudo apt install v4l-utils
2.查看攝像頭
ls /dev/video*
3.查看攝像頭詳細參數(shù),看是否支持
v4l2-ctl --device=/dev/video0 --list-formats-ext
下面是我的攝像頭的參數(shù),openCV默認打開的是YUYV格式的,所以幀率低,只有5幀。
默認打開模式下的幀率
下面貼出參考網(wǎng)站的連接解決方式
完整代碼
給出完整代碼,測試能用,最大30幀,因為我的攝像頭只能支持30幀
import cv2
import time
width = 640
height = 480
gs_pipeline = f"v4l2src device=/dev/video0 io-mode=2 " \
f"! image/jpeg, width={width}, height={height}, framerate=30/1, format=MJPG " \
f"! nvv4l2decoder mjpeg=1 " \
f"! nvvidconv " \
f"! video/x-raw, format=BGRx " \
f"! videoconvert " \
f"! video/x-raw, format=BGR " \
f"! appsink drop=1"
cap = cv2.VideoCapture(gs_pipeline, cv2.CAP_GSTREAMER)
# 每0.1S計算一次幀率
t = 0.1
counter = 0
fps = 0
start_time = time.time()
while (True):
ret, frame = cap.read()
img = frame.copy()
# 測幀率
counter += 1
if (time.time() - start_time) > t:
fps = counter / (time.time() - start_time)
fps = str(fps)
counter = 0
start_time = time.time()
cv2.putText(frame, "FPS {0}".format(fps), (10, 30), 1, 1.5, (255, 0, 255), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
這段代碼很關(guān)鍵
gs_pipeline = f"v4l2src device=/dev/video0 io-mode=2 " \
f"! image/jpeg, width={width}, height={height}, framerate=30/1, format=MJPG " \
f"! nvv4l2decoder mjpeg=1 " \
f"! nvvidconv " \
f"! video/x-raw, format=BGRx " \
f"! videoconvert " \
f"! video/x-raw, format=BGR " \
f"! appsink drop=1"
大神給出的解釋:
nvvidconv doesn’t support BGR, only BGRx (thus the videoconvert for BGRx->BGR). Caps also lacks a comma. Last, videoconvert only supports system memory, so have nvvidconv to output into system memory rather than NVMM memory.文章來源:http://www.zghlxwxcb.cn/news/detail-739583.html
幀率計算是參考CSDN上的代碼
現(xiàn)在能跑30幀了,大功告成!文章來源地址http://www.zghlxwxcb.cn/news/detail-739583.html
到了這里,關(guān)于jetson nano USB攝像頭使用openCv打開幀率低的問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!