5

Referring to this similar question How to parse mjpeg http stream from ip camera? I was able to read the stream from IP camera, by using requests:

stream = requests.get('http://<user>:<pass>@<addr>:<port>/videostream.cgi', stream=True)

bytez = ''
while True:
    bytez += stream.raw.read(16384)
    ...

and it works beautifully, but would like to get there by using cv2.VideoCapture() instead requests.

I tried variations in a manner of:

cap = cv2.VideoCapture()
cap.open('http://<user>:<pass>@<addr>:<port>/videostream.cgi?.mjpg')

while(True):
    ret, frame = cap.read()
    ...

but wasn't able to get anything, but Exception about empty frame.

How to read IP camera stream with cv2.VideoCapture()?

Community
  • 1
  • 1
theta
  • 24,593
  • 37
  • 119
  • 159

3 Answers3

3

Pass the location of the camera in your cap = cv2.VideoCapture() line:

cap = cv2.VideoCapture('http://<user>:<pass>@<addr>:<port>/videostream.cgi?.mjpg')
bugmenot123
  • 1,069
  • 1
  • 18
  • 33
2

Add C:\OpenCV\3rdparty\ffmpeg\ to the Windows PATH environment variable or copy opencv_ffmpeg.dll from that directory to C:\Python27. This has been answered in this question OpenCV 2.4 VideoCapture not working on Windows

Community
  • 1
  • 1
TonyParker
  • 2,024
  • 5
  • 18
  • 27
0

I haven't yet tried accessing an IP camera from VideoCapture, but on your method cap = cv2.VideoCapture() the video capture is expecting a number representing the camera usually 0.

By leaving it empty, it doesn't access any camera thus the Exception about an empty frame (even though later on you declare cap.open(), openCV already tried to open a camera and determined that is empty)

hjavaher
  • 2,589
  • 3
  • 30
  • 52
  • 1
    OpenCV: out device of bound (0-0): 1 ... unless I pass 0 which is the laptop camera. ifconfig shows the camera exists on en7. – user391339 Jan 09 '18 at 08:44