2017-08-07 145 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)