【问题标题】:Python 3 IP Webcamera byte ErrorPython 3 IP 网络摄像头字节错误
【发布时间】:2015-12-29 18:58:01
【问题描述】:

我正在使用 Python 3 获取 IP 网络摄像头的流并将其显示在我的计算机上。以下代码仅适用于 python 2.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)

但是,当我在 Python 3 上尝试时,出现以下错误

字节+=stream.read(16384)

TypeError: 无法将 'bytes' 对象隐式转换为 str

这在 2.7 中完美运行,但我找不到让它在 3 中运行的方法,有什么想法吗?

【问题讨论】:

    标签: python python-2.7 opencv python-3.x ip-camera


    【解决方案1】:

    在 python3 中 str 不是单个字节

    改成

    bytes=b''
    

    bytes 也是一个内置的......你可能不应该将它用作变量名

    【讨论】:

    • 我试过你说的,但现在它在第 9 行抛出这个错误a = bytes.find('\xff\xd8') TypeError: 'str' does not support the buffer interface
    • a = bytes.find(b'\xff\xd8') 在任何使用字节的地方都必须在其前面加上 b
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2017-02-18
    相关资源
    最近更新 更多