我正在使用python3来获取一个ipweb摄像头的流,并将其显示在我的计算机上。以下代码仅在Python2.7中工作
import cv2
import urllib.request
import numpy as np
stream=urllib.request.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)
但是,当我在python3上尝试时,我得到了以下错误
bytes+=stream.read(16384)
TypeError: Can't convert 'bytes' object to str implicitly
这在2.7版本中运行得很好,但是我找不到一个方法让它在3中运行,有什么想法吗?在
在python3中str不是单字节
把它改成
字节也是内置的。。。您可能不应该将其用作变量名
相关问题 更多 >
编程相关推荐