简体   繁体   中英

IP Camera Python 3 Error

I am working on using Python 3 to take an IP web camera's stream and display it on my computer. The following code only works in python 2.7

import cv2
import urllib 
import numpy as np

stream=urllib.urlopen('http://192.168.0.90/mjpg/video.mjpg')
bytes=''
while True:
    bytes+=stream.read(16384)
    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.IMREAD_COLOR)
        cv2.imshow('i',i)
        if cv2.waitKey(1) ==27:
            exit(0)

However when I try it on Python 3 I get the following error

stream=urllib.urlopen(' http://192.168.0.90/mjpg/video.mjpg ') AttributeError: 'module' object has no attribute 'urlopen'

Is there any fix for this? I tried making my own buffer but there isn't much information out there on this stuff

For python3 you need import urllib.request :

import urllib.request

stream = urllib.request.urlopen('http://192.168.0.90/mjpg/video.mjpg')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM