繁体   English   中英

“utf-8”编解码器无法解码位置 0 中的字节 0x89:起始字节无效

[英]'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

我正在尝试使用 OpenCV2 在图像上绘制边界框。 我正在使用aiohttp.ClientSession()向图像发出请求,我正在使用cv2.imdecode读取图像。

我的代码最终类似于:

async with aiohttp.ClientSession() as session:
    async with session.get(attachment.proxy_url, headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55'
        }) as resp:

        image = await resp.content.read()
        nparr = np.fromstring(image, dtype=np.uint8)
        cvimg = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        
        # *hopefully* get to this point without erroring

通常当它到达 imdecode 部分时,它会出现'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

opencv 试图加载的图像是https://media.discordapp.net/attachments/831327584364920862/870406065257336852/photo-1571577275698-54f36820ee9b.png

试试这个,看看它是否有效:

import base64
import json
import cv2
import numpy as np

async with aiohttp.ClientSession() as session:
    async with session.get(attachment.proxy_url, headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.55'
        }) as resp:

        image = await resp.content.read()
        jpg_original = base64.b64decode(image)
        jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
        img = cv2.imdecode(jpg_as_np, flags=1)

我也搞定了。 事实证明,这是引发此错误的代码的不同部分,并且与我发送的代码段无关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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