Advertisement
MrLunk

How to use Gstreamer pipeline in OpenCV ?

Oct 30th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. http://answers.opencv.org/question/202017/how-to-use-gstreamer-pipeline-in-opencv/?answer=202087#post-id-202087
  2.  
  3. How to use Gstreamer pipeline in OpenCV ?
  4.  
  5. I have OpenCV installed with ffmpeg and gstreamer support.
  6. I have a working Gstreamer pipeline from my raspberry pi 3b to Ubuntu 16.04.
  7.  
  8. This is my Gstreamer pipeline SEND script line on the Raspberry Pi 3b:
  9. gst-launch-1.0 -v v4l2src ! video/x-raw,width=320,height=240 ! videoconvert ! jpegenc ! rtpjpegpay ! udpsink host=192.168.1.101 port=5200
  10.  
  11. This is my Gstreamer pipeline RECEIVER script line at the Ubuntu 16.04 end:
  12. gst-launch-1.0 -v udpsrc port=5200 ! application/x-rtp, encoding-name=JPEG,payload=26 ! rtpjpegdepay ! jpegdec ! videoconvert ! autovideosink
  13.  
  14. This pipeline produces a good quality low latency video-stream.
  15.  
  16. -----------------------------------------------
  17. Test this :
  18.  
  19. import numpy as np import cv2 from multiprocessing import Process
  20. then:
  21.  
  22. def send():
  23. cap_send = cv2.VideoCapture('videotestsrc ! video/x-raw,framerate=20/1 ! videoscale ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
  24. out_send = cv2.VideoWriter('appsrc ! videoconvert ! x264enc tune=zerolatency bitrate=500 speed-preset=superfast ! rtph264pay ! udpsink host=127.0.0.1 port=5000',cv2.CAP_GSTREAMER,0, 20, (320,240), True)
  25.  
  26. if not cap_send.isOpened() or not out_send.isOpened():
  27. print('VideoCapture or VideoWriter not opened')
  28. exit(0)
  29.  
  30. while True:
  31. ret,frame = cap_send.read()
  32.  
  33. if not ret:
  34. print('empty frame')
  35. break
  36.  
  37. out_send.write(frame)
  38.  
  39. cv2.imshow('send', frame)
  40. if cv2.waitKey(1)&0xFF == ord('q'):
  41. break
  42.  
  43. cap_send.release()
  44. out_send.release()
  45.  
  46. def receive():
  47. cap_receive = cv2.VideoCapture('udpsrc port=5000 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96" ! rtph264depay ! decodebin ! videoconvert ! appsink', cv2.CAP_GSTREAMER)
  48.  
  49. if not cap_receive.isOpened():
  50. print('VideoCapture not opened')
  51. exit(0)
  52.  
  53. while True:
  54. ret,frame = cap_receive.read()
  55.  
  56. if not ret:
  57. print('empty frame')
  58. break
  59.  
  60. cv2.imshow('receive', frame)
  61. if cv2.waitKey(1)&0xFF == ord('q'):
  62. break
  63.  
  64. #cap_receive.release()
  65.  
  66. if __name__ == '__main__':
  67. s = Process(target=send)
  68. r = Process(target=receive)
  69. s.start()
  70. r.start()
  71. s.join()
  72. r.join()
  73.  
  74. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement