2016-08-02 203 views
0
import cv2 
import urllib 
import numpy as np 

stream=urllib.urlopen('http://192.168.1.5:8080/frame.mjpg') 
bytes='' 
while True: 
    bytes+=stream.read(1024) 
    a = bytes.find('\xff\xd8') 
    b = bytes.find('\xff\xd9') 
    if a!=-1 and b!=-1: 
     jpg = bytes[a:b+2] 
    bytes= bytes[b+2:] 
    i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR) 
    cv2.imshow('i',i) 
    if cv2.waitKey(1) ==27: 
     exit(0) 

該代碼也沒有拋出任何錯誤,我在IPWebcam中設置了無身份驗證。 它投擲的錯誤連接被拒絕如何通過打開的cv從IP攝像頭訪問無線攝像頭?

回答

0

試試這個:

import urllib 
import cv2 
import numpy as np 
import time 

# Replace the URL with your own IPwebcam shot.jpg IP:port 
url='http://192.168.20.108:8080/shot.jpg' 


while True: 
    # Use urllib to get the image from the IP camera 
    imgResp = urllib.urlopen(url) 

    # Numpy to convert into a array 
    imgNp = np.array(bytearray(imgResp.read()),dtype=np.uint8) 

    # Finally decode the array to OpenCV usable format ;) 
    img = cv2.imdecode(imgNp,-1) 


    # put the image on screen 
    cv2.imshow('IPWebcam',img) 

    #To give the processor some less stress 
    #time.sleep(0.1) 

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