Jetson nano uses camera (CSI and USB) with opencv

Table of contents

1. Test whether the camera is available

Check if it is connected to the camera

 CSI camera test

USB camera test

2. If the above steps are normal, perform oepncv to read the camera data

(1) CSI camera

1. Install the v4l2-utils assistance tool

2. Check the detailed parameters of the camera--supported image size and corresponding frame rate

3. Install pipeline gstreamer library

4. Use opencv to read image routines

(2) USB camera


1. Test whether the camera is available

Check if it is connected to the camera

ls /dev/video*

 CSI camera test

nvgstcapture-1.0

USB camera test

#首先安装camorama库
sudo apt-get install camorama
#然后运行
camorama /dev/video0 #这里我只安装了USB摄像头

2. If the above steps are normal, perform oepncv to read the camera data

(1) CSI camera

1. Install the v4l2-utils assistance tool

sudo apt install v4l-utils 

2. Check the detailed parameters of the camera--supported image size and corresponding frame rate

v4l2-ctl --device=/dev/video0 --list-formats-ext

3. Install pipeline gstreamer library

sudo add-apt-repository universe
sudo add-apt-repository multiverse
sudo apt-get update
sudo apt-get install gstreamer1.0-tools gstreamer1.0-alsa gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav
sudo apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-good1.0-dev libgstreamer-plugins-bad1.0-dev

4. Use opencv to read image routines

#coding=utf-8
import cv2

# 设置gstreamer管道参数
def gstreamer_pipeline(
    capture_width=1280, #摄像头预捕获的图像宽度
    capture_height=720, #摄像头预捕获的图像高度
    display_width=1280, #窗口显示的图像宽度
    display_height=720, #窗口显示的图像高度
    framerate=60,       #捕获帧率
    flip_method=0,      #是否旋转图像
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )

if __name__ == "__main__":
    #根据摄像头参数设置
    capture_width = 1280   #
    capture_height = 720
    framerate = 60			# 帧数
    #展示图片大小
    display_width = 1280
    display_height = 720
    flip_method = 0			# 方向

    # 创建管道
    #管道与视频流绑定
    cap = cv2.VideoCapture(gstreamer_pipeline(capture_width,capture_height,display_width,display_height,framerate,flip_method), cv2.CAP_GSTREAMER)

    if cap.isOpened():
        window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)

        # 逐帧显示
        while cv2.getWindowProperty("CSI Camera", 0) >= 0:
            ret_val, img = cap.read()
            cv2.imshow("CSI img", img)

            keyCode = cv2.waitKey(30) & 0xFF
            if keyCode == 27:# ESC键退出
                break

        cap.release()
        cv2.destroyAllWindows()
    else:
        print("open error")

(2) USB camera

import cv2

cap = cv2.VideoCapture('./dev/video0') #用ls /dev/video* 可读取出来

while(1):
    ret,frame = cap.read()  
    if ret:
        cv2.imshow("USB_img",frame) 
        keyCode = cv2.waitKey(30) & 0xFF
        if keyCode == 27:# ESC键退出
            break 
    
cap.release()
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/A15768533126/article/details/129877274