2017-08-07 305 views
1

我想使用gstreamer將opencv中的一些圖像流化,並且得到了與管道有關的問題。我是gstreamer和opencv的新手。我編譯的OpenCV 3.2的GStreamer爲python3在樹莓派3.我有我raspivid用一個小bash腳本在python中從opencv寫入Gstreamer管道

raspivid -fps 25 -h 720 -w 1080 -vf -n -t 0 -b 2000000 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=192.168.1.27 port=5000 

我想這條管道,以便從OpenCV的使用它,並反饋到它的圖像轉換我的算法操縱。我做了一些研究和揣摩,我可以使用videoWriter與appsrc代替fdsrc但我得到以下錯誤

GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error. 

,我想出了用方式 進口CV2了以下的Python腳本

cap = cv2.VideoCapture(0) 


# Define the codec and create VideoWriter object 
fourcc = cv2.VideoWriter_fourcc(*'MJPG') 
out = cv2.VideoWriter('appsrc ! h264parse ! ' 
         'rtph264pay config-interval=1 pt=96 ! ' 
         'gdppay ! tcpserversink host=192.168.1.27 port=5000 ', 
         fourcc, 20.0, (640, 480)) 

while cap.isOpened(): 
    ret, frame = cap.read() 
    if ret: 
     frame = cv2.flip(frame, 0) 

     # write the flipped frame 
     out.write(frame) 

     if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 
    else: 
     break 

# Release everything if job is finished 
cap.release() 
out.release() 
cv2.destroyAllWindows() 

管道中是否有任何錯誤?我不明白這個錯誤。我已經有了一個可以從bash管道讀取的Python客戶端,從延遲角度和消耗的資源來看,結果非常好。

回答

1

我遇到了解決方案,我希望這可以幫助遇到同樣問題的其他人。 管道錯誤安排,需要videoconvert。 另一方面,延遲非常相關,但設置速度。即使沒有太多的壓縮,超快速解決了問題,這是一個很好的折衷。這是我的解決方案。

import cv2 

cap = cv2.VideoCapture(0) 

framerate = 25.0 

out = cv2.VideoWriter('appsrc ! videoconvert ! ' 
         'x264enc noise-reduction=10000 speed-preset=ultrafast tune=zerolatency ! ' 
         'rtph264pay config-interval=1 pt=96 !' 
         'tcpserversink host=192.168.1.27 port=5000 sync=false', 
         0, framerate, (640, 480)) 

while cap.isOpened(): 
    ret, frame = cap.read() 
    if ret: 

     out.write(frame) 

     if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 
    else: 
     break 

# Release everything if job is finished 
cap.release() 
out.release()