【发布时间】:2021-07-29 20:44:49
【问题描述】:
我正在尝试使用 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
【问题讨论】:
-
当您尝试阅读
np.fromstring的文档和示例时发生了什么?他们在示例中使用的数据是否看起来像 PNG 中的原始字节?不,它看起来像包含人类可读的数字表示的文本。相反,您有原始字节,所以...查看相关文档,看看您是否无法弄清楚应该使用什么。
标签: python numpy opencv numpy-ndarray aiohttp