Getting image from usb camera with gstream and opencv

I want to capture images with opencv videocapture using gstreamer. This pipeline works when I type in terminal.

gst-launch-1.0 -v v4l2src device=/dev/video0 ! image/jpeg, width =1920, height=1800, framerate=15/1 ! jpegparse !! jpegdec ! videoconvert ! xvimagesing sync = false

but when i try the usage method i see from forums, i can’t get image.

import numpy as np
import cv2

cap = cv2.VideoCapture('gst-launch-1.0 v4l2src device=/dev/video0 ! image/jpeg, width=1920, height=1080, framerate=15/1 ! jpegparse ! jpegdec ! videoconvert ! xvimagesink sync=false', cv2.CAP_GSTREAMER)

while True:
    ret,frame = cap.read()
    if not ret:
        print('empty frame')
        continue

    cv2.imshow('receive', frame)
    if cv2.waitKey(1)&0xFF == ord('q'):
        break

cap.release()

What could be the problem, what am I doing wrong?

xvimagesink

you can’t just copy a pipeline that “works” in a terminal.

please read up on what appsink is.