最近玩jetson nano的时候发现openCv打开USB摄像头帧率只有5帧,但是windows下能跑30帧,在网络搜了一下原因发现是摄像头默认读取格式不对,需要改成MJPG格式帧率才能上去,于是使用CSDN上找的代码,但是都不能工作,折腾了好久没搞定,突然想到上 stacoverflow上看看,没想到真的解决了,这里做一下记录。
首先检查摄像头设备是否存在
1.安装工具
sudo apt install v4l-utils
2.查看摄像头
ls /dev/video*
3.查看摄像头详细参数,看是否支持
v4l2-ctl --device=/dev/video0 --list-formats-ext
下面是我的摄像头的参数,openCV默认打开的是YUYV格式的,所以帧率低,只有5帧。
默认打开模式下的帧率
下面贴出参考网站的连接解决方式
完整代码
给出完整代码,测试能用,最大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()
这段代码很关键
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.文章来源:https://www.toymoban.com/news/detail-739583.html
帧率计算是参考CSDN上的代码
现在能跑30帧了,大功告成!文章来源地址https://www.toymoban.com/news/detail-739583.html
到了这里,关于jetson nano USB摄像头使用openCv打开帧率低的问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!