简体   繁体   English

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

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

I'm trying to draw a bounding box on an image using OpenCV2.我正在尝试使用 OpenCV2 在图像上绘制边界框。 I'm using aiohttp.ClientSession() to make a request to an image and I'm using cv2.imdecode to read the image.我正在使用aiohttp.ClientSession()向图像发出请求,我正在使用cv2.imdecode读取图像。

My code ends up something like:我的代码最终类似于:

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

Usually when it gets to the imdecode part, it errors with 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte .通常当它到达 imdecode 部分时,它会出现'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

The image that opencv is trying to load is https://media.discordapp.net/attachments/831327584364920862/870406065257336852/photo-1571577275698-54f36820ee9b.png opencv 试图加载的图像是https://media.discordapp.net/attachments/831327584364920862/870406065257336852/photo-1571577275698-54f36820ee9b.png

Try this and see if it works:试试这个,看看它是否有效:

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)

I got it too work.我也搞定了。 Turns out it was a different section of code that threw this error, and was unrelated to the snippets I sent.事实证明,这是引发此错误的代码的不同部分,并且与我发送的代码段无关。

暂无
暂无

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

相关问题 (django) 'utf-8' 编解码器无法解码位置 0 的字节 0x89:无效的起始字节 - (django) 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte 在 Django 中上传图像:“utf-8”编解码器无法解码位置 246 中的字节 0x89:起始字节无效 - Uploading Image in Django: 'utf-8' codec can't decode byte 0x89 in position 246: invalid start byte UnicodeDecodeError:“utf-8”编解码器无法解码 position 中的字节 0x89 0:起始字节无效。 如何修复 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte. How to fix it “utf-8”编解码器无法解码字节 0x89 - 'utf-8' codec can't decode byte 0x89 UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 3131 中的字节 0x80:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 3131: invalid start byte “utf-8”编解码器无法解码位置 11 中的字节 0x92:起始字节无效 - 'utf-8' codec can't decode byte 0x92 in position 11: invalid start byte Python UnicodeDecodeError:“ utf-8”编解码器无法解码位置2的字节0x8c:无效的起始字节 - Python UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8c in position 2: invalid start byte UnicodeDecodeError:'utf-8'编解码器无法解码位置3的字节0x97:无效的起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x97 in position 3: invalid start byte “utf-8”编解码器无法解码 position 18 中的字节 0x92:无效的起始字节 - 'utf-8' codec can't decode byte 0x92 in position 18: invalid start byte `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte` - `UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM