目录
三 jupyter 里通过 opencv 打开摄像头采集,并将视频显示在浏览器中
一官方摄像头检测工具
shell 运行
nvgstcapture-1.0 # 打开摄像头采集视频数据并在 hdmi 屏显示
如果这里报错
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:543 Failed to create CaptureSession
systemctl restart nvargus-daemon
二 通过 gstreamer 工具打开摄像头并显示
# 连接键盘,hdmi 屏, ctrl + alt +t 打开桌面环境的 shell, 然后执行
gst-launch-1.0 nvarguscamerasrc ! video/x-raw\(memory:NVMM\), width=3280, height=2464, \
format=\(string\)NV12, framerate=\(fraction\)21/1 ! nvvidconv \
! video/x-raw, width=\(int\)640, height=\(int\)480, format=\(string\)BGRx \
! videoconvert ! ximagesink
# 以下输出显示 我的 camera sensor 支持的格式
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;
# 如果要在 ssh 的 shell 环境执行 x windows 窗口程序 , 需要在 桌面环境的 shell 里执行 xhost + , 然后 ssh的 shell 环境里 先 export DISPLAY=:0 , 再执行 gst-launch-1.0 , 否则没有打开X window 权限
可以把 xhost + 执行加到 开机启动里如下:
# 编辑启动脚本
echo "xhost +" > /opt/x11_auto.sh
chmod +x /opt/x11_auto.sh
# 添加开机执行, 编辑 /etc/lightdm/lightdm.conf 增加如下:
[SeatDefaults]
session-setup-script= /opt/x11_auto.sh
# 重启, 重新 ssh登陆后使用 xrandr 验证
export DISPLAY=:0 # 必必须有
root@walle-desktop:/workspace/ssh# xrandr
Screen 0: minimum 8 x 8, current 1024 x 600, maximum 16384 x 16384
HDMI-0 connected primary 1024x600+0+0 (normal left inverted right x axis y axis) 410mm x 260mm
1024x600 60.04*+
1920x1080 60.00 59.95 50.00
1440x900 74.99 59.89
1280x1024 75.03 60.00
1280x720 60.00 59.94 50.00
1152x864 75.00
1024x768 75.03 70.07 60.01
832x624 75.05
800x600 75.00 72.19 60.32 56.25
720x576 50.00
720x480 59.94
720x400 70.04
640x480 75.00 72.81 59.94
DP-0 disconnected (normal left inverted right x axis y axis)
三 jupyter 里通过 opencv 打开摄像头采集,并将视频显示在浏览器中
import cv2
import traceback
# Camera 类的定义
class JetCamera():
def __init__(self, cap_w, cap_h, cap_fps):
#self.cap_orig_w, self_cap_orig_h = 3264, 2464 # 21 fps
#self.cap_orig_w, self_cap_orig_h = 1920, 1080 # 30 fps
self.cap_orig_w, self.cap_orig_h = 1280, 720 # 60/120 fps
self.cap_orig_fps = 60
self.cap_out_w = cap_w
self.cap_out_h = cap_h
self.cap_out_fps = cap_fps
# 通过 gstreamer 打开摄像头
self.cap_str = 'nvarguscamerasrc ! video/x-raw(memory:NVMM), width=%d, height=%d, format=(string)NV12, framerate=(fraction)%d/1 '\
'! nvvidconv ! video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx '\
'! videorate ! video/x-raw, framerate=(fraction)%d/1 '\
'! videoconvert ! appsink' \
% (self.cap_orig_w, self.cap_orig_h, self.cap_orig_fps, self.cap_out_w, self.cap_out_h, self.cap_out_fps)
self.cap = None
def open(self):
if self.cap:
return True
try:
#print(self.cap_str)
self.cap = cv2.VideoCapture(self.cap_str, cv2.CAP_GSTREAMER)
except:
traceback.print_exc()
return self.cap is not None
def read(self):
if not self.cap:
return None, None
try:
ret, img = self.cap.read()
except:
traceback.print_exc()
return ret, img
def close(self):
try:
if self.cap:
self.cap.release()
self.cap = None
except:
pass
def bgr8_to_jpeg(value, quality=75):
return bytes(cv2.imencode('.jpg', value)[1])
cap_w = 640
cap_h = 360
cap_fps = 30
cam = JetCamera(cap_w, cap_h, cap_fps)
print(cam.cap_str)
cam.open()
# jupyter 显示控件
import traitlets
import ipywidgets.widgets as widgets
from IPython.display import display
color_image = widgets.Image(format='jpeg', width=cap_w, height=cap_h)
display(color_image)
# 主循环
while True:
ret, frame = cam.read()
if not ret:
print('cam read failed')
break
color_image.value = cam.bgr8_to_jpeg(frame) #更新显示
cam.close()