2017-08-07 57 views
3

ビデオストリームにアクセスできない。ビデオストリームを取得するのに手伝ってください。私はGoogleのソリューションを探して、スタックのオーバーフローで別の質問を投稿するが、残念ながら何も問題を解決することはできません。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の下にあります。このコードを使用します:

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) 
関連する問題