Python 3 IP网络摄像头字节E

2024-09-30 04:38:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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中运行,有什么想法吗?在


Tags: importreadstreamifbytesrequestnpfind

热门问题