视频讲解
OpenCV读取摄像头窗口变大且很卡的解决方法
测试过程
读取摄像头窗口变大且很卡的代码
import cv2
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("no stream!")
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
运行后,读取摄像头的窗口很大,而且很卡,出现如下的提示
python3 test-camera.py
[ WARN:0@0.986] global /home/jetson/Downloads/opencv-4.5.5/modules/videoio/src/cap_gstreamer.cpp (1405) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Gtk-Message: 21:12:47.677: Failed to load module "canberra-gtk-module"
安装libcanberra-gtk-module
sudo apt-get install libcanberra-gtk-module
继续运行,还是很卡,WARN还在,修改代码如下,增加cv2.CAP_V4L2
v4l2(video for linux two)是Linux中内核提供给应用层访问音视频驱动的统一接口。v4l2中获取摄像头的能力的是通过ioctl函数的VIDIOC_QUERYCAP命令获取。文章来源:https://www.toymoban.com/news/detail-824287.html
import cv2
cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("no stream!")
break
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
文章来源地址https://www.toymoban.com/news/detail-824287.html
到了这里,关于OpenCV读取摄像头窗口变大且很卡的解决方法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!