簡體   English   中英

如何使用OpenCV(Python)捕獲視頻流

[英]How to Capture Video Stream with OpenCV (Python)

我想使用Python處理帶有OpenCV的mms視頻流。 該流來自我無法控制的IP攝像頭(流量監控器)。 流可用作mms或mmst方案 -

mms://194.90.203.111/cam2

在VLC和Windows Media Player上播放。

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

使用FFmpeg和FFserver解決。 注意FFserver僅適用於Linux。 該解決方案采用Python代碼從這里通過的建議瑞安

流量如下 -

  • 使用所需的配置啟動FFserver后台進程(在本例中為mjpeg)。
  • FFmpeg輸入是最小的流,輸出流到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需要更多的微調,但它們工作得相當好,並且產生一個非常接近源的幀,只有一點幀凍結。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM