参考文章:How to Capture and Display Camera Video with Python on Jetson TX2
Most of the reference articles are similar, if not used to look at the English, I can see description below
In Jetson TX2 camera video capture and display, including IP CAM (webcam), USB webcam and onboard cameras. The code should also apply to Jetson TX1
prerequisites
OpenCV with GStreamer and python support built and installed in the Jetson TX2, I'm using OpenCV-3.4.0 and python3. On how to install OpenCV on Jetson TX2 (3.4.0) refer to the following article: install OpenCV (3.4.0) on Jetson TX2
If you want to test IP CAM, you need to set it and know that it's RTSP URI. For example: RTSP: // admin: [email protected]: 554.
Inserting a USB camera, usually a / dev / video1, using the onboard camera / dev / video0
Installation gstreamer1.0-plugin -bad-xxx comprising h264parse elements. This is the H.264 RTSP stream decoding from the IP CAM required.
sudo apt-get install gstreamer1.0-plugins-bad-faad \ gstreamer1.0-plugins-bad-videoparsers
Download and use
Download the code: tegra-cam.py Address: https://gist.github.com/jkjung-avt/86b60a7723b97da19f7bfa3cb7d2690e
Using a USB camera with a resolution set to 1280x720. '-Vid 1' meaning / de / video1
python3 tegra-cam.py --usb --vid 1 --width 1280 --height 720
Running error under my environment!
The reason may be my GStreamer of v4l2src for USB camera does not work, use the following code to replace open_cam_usb ()
def open_cam_usb(dev, width, height): return cv2.VideoCapture(dev)
Using the onboard camera
python3 tegra-cam.py
Use IP CAM, at the same time replace it with your own IP
python3 tegra-cam.py --rtsp --uri rtsp://admin:[email protected]:554
The key is to tegra-cam.py script calls cv2.VideoCapture () of GStreamer pipelines. According to experience, using nvvidconv image scaling and color format conversion can produce better results in a frame rate of BGRx
def open_cam_rtsp(uri, width, height, latency): gst_str = ('rtspsrc location={} latency={} ! ' 'rtph264depay ! h264parse ! omxh264dec ! ' 'nvvidconv ! ' 'video/x-raw, width=(int){}, height=(int){}, ' 'format=(string)BGRx ! ' 'videoconvert ! appsink').format(uri, latency, width, height) return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) #def open_cam_usb(dev, width, height): # # We want to set width and height here, otherwise we could just do: # # return cv2.VideoCapture(dev) # gst_str = ('v4l2src device=/dev/video{} ! ' # 'video/x-raw, width=(int){}, height=(int){} ! ' # 'videoconvert ! appsink').format(dev, width, height) # return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER) def open_cam_usb(dev, width, height): return cv2.VideoCapture(dev) def open_cam_onboard(width, height): # On versions of L4T prior to 28.1, add 'flip-method=2' into gst_str gst_str = ('nvcamerasrc ! ' 'video/x-raw(memory:NVMM), ' 'width=(int)2592, height=(int)1458, ' 'format=(string)I420, framerate=(fraction)30/1 ! ' 'nvvidconv ! ' 'video/x-raw, width=(int){}, height=(int){}, ' 'format=(string)BGRx ! ' 'videoconvert ! appsink').format(width, height) return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)