Jetson OpenCV 通过 GStreamer 调用 CSI 摄像头

import cv2


def gstreamer_pipeline(capture_width=1280, capture_height=720,
                       fps=60, flip_method=0,
                       display_width=1280, display_height=720):
    ret1 = 'nvarguscamerasrc ! '
    ret2 = 'video/x-raw(memory:NVMM), width=(int){}, height=(int){} '.format(capture_width, capture_height)
    ret3 = 'format=(string)NV12, framerate=(fraction){}/1 ! '.format(fps)
    ret4 = 'nvvidconv flip-method={} ! '.format(flip_method)
    ret5 = 'video/x-raw, width=(int){}, height=(int){} '.format(display_width, display_height)
    ret6 = 'format=(string)BGRx ! '
    ret7 = 'videoconvert ! '
    ret8 = 'video/x-raw, format=(string)BGR ! appsink'

    return ret1 + ret2 + ret3 + ret4 + ret5 + ret6 + ret7 + ret8


def show_camera(func=None):
    cap = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
    while cap.isOpened():
        flag, img = cap.read()

        if func:
            img = func(img)

        cv2.imshow('CSI Camera', img)
        key = cv2.waitKey(1)
        if key == ord('q'):
            break
    cv2.destroyAllWindows()
    cap.release()


if __name__ == '__main__':
    show_camera()

你可能感兴趣的:(NVIDIA,Jetson,nvidia,opencv,gstreamer)