且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何直接使用Opencv从Azure Blob存储读取图像,而不将其下载到本地文件?

更新时间:2023-02-14 21:36:30

根据我的经验,您可以尝试使用get_blob_to_bytes方法将blob下载为字节数组并将其秘密转换为opencv图像,作为我的示例代码在下面.

Per my experience, you can try to use get_blob_to_bytes method to download the blob as a byte array and covert it to a opencv image, as my sample code below.

from azure.storage.blob import BlockBlobService

account_name = '<your-storage-account>'
account_key = '<your accout key>'
block_blob_service = BlockBlobService(account_name, account_key)

container_name = 'mycontainer'
blob_name = 'test.jpg'
blob = block_blob_service.get_blob_to_bytes(container_name, blob_name)

import numpy as np
import cv2
# use numpy to construct an array from the bytes
x = np.fromstring(blob.content, dtype='uint8')

# decode the array into an image
img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED)
print img.shape

# show it
cv2.imshow("Image Window", img)
cv2.waitKey(0)

希望有帮助.