Opencv 和 Gstreamer

Opencv and Gstreamer

我正在尝试发送 opencv 视频 cv2.VideoCapture() bash 代码工作得很好,但是当我将它复制到 appsrc 时它不想工作

bash:

send `gst-launch-1.0 -v v4l2src device='/dev/video0' ! video/x-raw,width=640,height=480,framerate=30/1,encoding-name=JPEG! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000`

recv `gst-launch-1.0 -v udpsrc port=5000 ! "application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string)JPEG" ! rtpjpegdepay ! jpegdec ! videorate ! autovideosink sync=false

python代码:

import cv2

cap = cv2.VideoCapture(0)

four = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter()
out.open("appsrc ! video/x-raw,width=640,height=480,framerate=30/1,encoding-name=JPEG! jpegenc ! rtpjpegpay ! udpsink host=127.0.0.1 port=5000",four,30.0,(640,480))

if(out.isOpened()):
    print('d')
while True:
    ret, frame = cap.read()
    if ret:
        out.write(frame)

由于数据格式而发生变化。您的相机似乎以 JPEG 格式捕捉图像。然而,OpenCv 矩阵是原始 BGR 图像,VideoCapture 元素将 jpeg 图像从相机转换为原始 BGR OpenCv 矩阵。

需要进行两项更改:

  • 您需要将 0 作为您的 fourcc 传递。
  • 你的上限 video/x-raw,width=640,height=480,framerate=30/1,encoding-name=JPEG 将无法 link 因为你的 appsrc 是原始 BGR 图像。您需要从大写中删除 encoding-name 参数。新上限为:video/x-raw,width=640,height=480,framerate=30/1.