2017-08-07 108 views
3

无法访问视频流。任何人都可以帮助我获得视频流。我已经在谷歌搜索解决方案,并在堆栈溢出后发布另一个问题,但不幸的是没有任何东西不能解决问题。使用OpenCV访问IP摄像机

import cv2 
cap = cv2.VideoCapture() 
cap.open('http://192.168.4.133:80/videostream.cgi?user=admin&pwd=admin') 
while(cap.isOpened()): 
    ret, frame = cap.read() 
    cv2.imshow('frame', frame) 
    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break 
cap.release() 
cv2.destroyAllWindows() 

回答

1

您可以使用urllib从视频流中读取帧。

import cv2 
import urllib 
import numpy as np 

stream = urllib.urlopen('http://192.168.100.128:5000/video_feed') 
bytes = '' 
while True: 
    bytes += stream.read(1024) 
    a = bytes.find(b'\xff\xd8') 
    b = bytes.find(b'\xff\xd9') 
    if a != -1 and b != -1: 
     jpg = bytes[a:b+2] 
     bytes = bytes[b+2:] 
     img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR) 
     cv2.imshow('Video', img) 
     if cv2.waitKey(1) == 27: 
      exit(0) 

如果您想从您的电脑的网络摄像头流式传输视频,请查看此内容。 https://github.com/shehzi-khan/video-streaming

3

谢谢。可能是,现在urlopen不在utllib下。这是根据urllib.request.urlopen.I使用此代码:

import cv2 
from urllib.request import urlopen 
import numpy as np 

stream = urlopen('http://192.168.4.133:80/video_feed') 
bytes = '' 
while True: 
    bytes += stream.read(1024) 
    a = bytes.find(b'\xff\xd8') 
    b = bytes.find(b'\xff\xd9') 
    if a != -1 and b != -1: 
     jpg = bytes[a:b+2] 
     bytes = bytes[b+2:] 
     img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR) 
     cv2.imshow('Video', img) 
     if cv2.waitKey(1) == 27: 
      exit(0)