视频讲解
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命令获取。
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()