简体   繁体   中英

Opencv and Gstreamer

i'm trying to send opencv video cv2.VideoCapture() bash code work excelent, but when I copy it to appsrc it don't want to work

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 code:

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)

The change occurs because of the data format. Your camera seems to capture images in JPEG format. However OpenCv matrices are raw BGR images, and VideoCapture element converts the jpeg images from camera to raw BGR OpenCv matrices.

There are two changes needed:

  • You need to pass 0 as your fourcc .
  • Your caps video/x-raw,width=640,height=480,framerate=30/1,encoding-name=JPEG will fail to link since your appsrc is raw BGR image. You need to remove encoding-name parameter from the caps. New caps will be: video/x-raw,width=640,height=480,framerate=30/1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM