2014-11-01 431 views
5

我想使用Python處理OpenCV中的mms視頻流。 流來自IP攝像機,我無法控制(流量監視器)。 流可以作爲彩信或MMST方案 - 兩個VLC和Windows Media Player如何使用OpenCV捕獲視頻流(Python)

mms://194.90.203.111/cam2 

戲劇。

mmst://194.90.203.111/cam2 

只適用於VLC。 我試圖通過使用FFmpeg和VLC重新流式傳輸將方案更改爲HTTP,但它不起作用。

據我所知,mms使用Windows Media Video來編碼流。沒有運氣在URI的末尾添加'.mjpeg'。我還沒有找到什麼類型的流被OpenCV接受。

這裏是我的代碼 -

import cv2, platform 
#import numpy as np 

cam = "mms://194.90.203.111/cam2" 
#cam = 0 # Use local webcam. 

cap = cv2.VideoCapture(cam) 
if not cap: 
    print("!!! Failed VideoCapture: invalid parameter!") 

while(True): 
    # Capture frame-by-frame 
    ret, current_frame = cap.read() 
    if type(current_frame) == type(None): 
     print("!!! Couldn't read frame!") 
     break 

    # Display the resulting frame 
    cv2.imshow('frame',current_frame) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

# release the capture 
cap.release() 
cv2.destroyAllWindows() 

的 我缺少什麼? OpenCV可以捕獲什麼類型的視頻流? 有沒有方案更改或轉碼的優雅解決方案?

謝謝!

Python ver 2.7.8,OpenCV的ver 2.4.9,兩個x86。 Win7 x64

+0

也許這將有助於:http://petrkout.com/electronics/low-latency-0-4-s-video-streaming-from-raspberry-pi-mjpeg-streamer-opencv/在客戶部分 – Ryan 2014-11-01 18:21:11

+0

謝謝@Ryan!該鏈接有很多很好的信息。最後的Python部分對它進行了固定。 – NoamR 2014-11-07 21:04:01

回答

5

使用FFmpeg和FFserver解決。注FFserver僅適用於Linux。 該解決方案使用here的Python代碼,如Ryan所示。

流程如下 - 利用期望的配置 (MJPEG在這種情況下)

  • 開始ffserver的後臺進程。
  • FFmpeg輸入是mmst流,輸出流 到localhost。
  • 運行python腳本來打開localhost流,並且逐幀解碼。

運行ffserver的

ffserver -d -f /etc/ffserver.conf 

上運行的FFmpeg

ffmpeg -i mmst://194.90.203.111/cam2 http://localhost:8090/cam2.ffm 

的Python代碼的第二端子。在這種情況下,代碼將打開一個視頻流的窗口。

import cv2, platform 
import numpy as np 
import urllib 
import os 

cam2 = "http://localhost:8090/cam2.mjpeg" 

stream=urllib.urlopen(cam2) 
bytes='' 
while True: 
    # to read mjpeg frame - 
    bytes+=stream.read(1024) 
    a = bytes.find('\xff\xd8') 
    b = bytes.find('\xff\xd9') 
    if a!=-1 and b!=-1: 
     jpg = bytes[a:b+2] 
     bytes= bytes[b+2:] 
    frame = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR) 
    # we now have frame stored in frame. 

    cv2.imshow('cam2',frame) 

    # Press 'q' to quit 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 

cv2.destroyAllWindows() 

ffserver.config -

Port 8090 
BindAddress 0.0.0.0 
MaxClients 10 
MaxBandWidth 50000 
CustomLog - 
#NoDaemon 

<Feed cam2.ffm> 
    File /tmp/cam2.ffm 
    FileMaxSize 1G 
    ACL allow 127.0.0.1 
    ACL allow localhost 
</Feed> 
<Stream cam2.mjpeg> 
    Feed cam2.ffm 
    Format mpjpeg 
    VideoFrameRate 25 
    VideoBitRate 10240 
    VideoBufferSize 20480 
    VideoSize 320x240 
    VideoQMin 3 
    VideoQMax 31 
    NoAudio 
    Strict -1 
</Stream> 
<Stream stat.html> 
    Format status 
    # Only allow local people to get the status 
    ACL allow localhost 
    ACL allow 192.168.0.0 192.168.255.255 
</Stream> 
<Redirect index.html> 
    URL http://www.ffmpeg.org/ 
</Redirect> 

注意,這ffserver.config需要更多的微調,但他們的工作相當出色,併產生非常接近的框架,只有一點點幀凍結源。

+0

我很高興我找到了這個。感謝分享! – sascha 2017-01-20 23:49:21