簡體   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