如何使用 openCV 和 python 从流中逐帧获取视频

How to get video frame by frame from stream using openCV and python

我正在使用 VLC 媒体播放器通过 http 流式传输 .mp4 视频。流媒体工作正常(我能够使用另一个 VLC 实例附加到此流)。 现在我想使用带有 python 2.7 的 OpenCV 连接到这个流并逐帧获取视频。

这是修改后的教程代码(与本地文件完美配合):

<code>
import numpy as np
import cv2    
address = '10.0.0.71' # this is my stream ip address
port = 8080 # this is stream port

# should I use socket somehow?
# found this somewhere, no idea what this do
# import socket
# msocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
# msocket.connect((address, port))

cap = cv2.VideoCapture('file.mp4') # how to use VideoCapture with online stream?     

# just showing video to screen
while(cap.isOpened()):
    ret, frame = cap.read()
    cv2.imshow('frame', frame)

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

cap.release()
cv2.destroyAllWindows()

请帮忙。

http://answers.opencv.org/question/24154/how-to-using-opencv-api-get-web-video-stream/

你应该可以 cap = cv2.VideoCapture('yourStreamURIHere')

如果您需要登录流,例如 127.0.0.1 的某个流,用户名:hello,密码:goodbye:

cap = cv2.VideoCapture('http://hello:goodbye@127.0.0.1/?action=stream?otherparamshere)

我有点晚了,但我已经创建了强大的线程VidGear Video Processing python library that now provides NetGear API, which is exclusively designed to transfer video frames synchronously between interconnecting systems over the network in real-time through ZmQ messaging system. For usage example, check my detailed answer here: