V4l2src using OpenCV Gstreamer is not working in Jetson Xavier NX


I am using USB Cameras for streaming.
So, I tried the below command in the terminal and able to preview the video.

gst-launch-1.0 v4l2src device=/dev/video2 ! ‘video/x-raw, width=640, height=480’ ! xcimagesink

After that, I used the same pipeline in the code.

video_cap = cv2.VideoCapture("v4l2src device=/dev/video2 ! ‘video/x-raw, width=640, height=480’ ! xcimagesink’, cv2.CAP_GSTREAMER)

I am getting an error like this (Error Opening bin: Syntax Error).
It seems, gstreamer cannot open that camera.

When I use (video_cap = cv2.VideoCapture(’/dev/video2’, cv2.CAP_V4L), its working.

I don’t know what I did wrong.
Please let me know if you have any suggestions.

You may try this python code:

import sys
import cv2

def read_cam():
    cap = cv2.VideoCapture("v4l2src device=/dev/video2 ! video/x-raw, width=640, height=480 ! videoconvert ! video/x-raw,format=BGR ! appsink")
    if cap.isOpened():
        cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
        while True:
            ret_val, img = cap.read();
     print "camera open failed"


if __name__ == '__main__':

Thank you.