我正在一个项目中,在该项目中,我使用建立的keras模型对存储在Azure Blob存储中的图像进行分类,并将结果导出为.csv文件。通过使用get_blob_to_path并将一些图像下载到我的笔记本电脑上,我能够做到这一点。但是,由于图片太多,因此我希望不通过get_blob_to_bytes或get_blob_to_stream下载图片。
答案 0 :(得分:0)
实际上,一种无需先下载就从Azure Blob存储加载图像的更简单的解决方案是使用sas令牌生成blob URL,以传递给imageio.imread
。
这是我的代码从您的那里更改了。
from azure.storage.blob import BlockBlobService
from azure.storage.blob import ContainerPermissions
from datetime import datetime, timedelta
import imageio
import numpy as np
from skimage import transform
import pandas as pd
account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<your container name>'
# generate the container-level sas token
block_blob_service = BlockBlobService(account_name=account_name, account_key=account_key)
token = block_blob_service.generate_container_shared_access_signature(container_name, permission=ContainerPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1),)
# generate the list of blob urls with sas token
blob_names = service.list_blob_names(container_name)
df = pd.read_csv("~/Desktop/list.csv")
blob_urls_with_token = (f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}?{token}" for blob_name in blob_names if blob_name in df.values)
#function to prepare the image for keras model
def load(img_sas_url):
image = imageio.imread(img_sas_url) # directly read image from the blob url with sas token
image = np.array(image).astype('float32')/255
image = transform.resize(image, (224, 224, 3))
image = np.expand_dims(image, axis=0)
return image
#predicting the images and append it to a datafram
predictions = []
images=[]
name = []
probs =[]
for img_sas_url in blob_urls_with_token:
image = load(img_sas_url)
prediction = model.predict_classes(image)
prob = model.predict(image).max()
predictions.append(prediction)
probs.append(prob)
images.append(file)
name.append(root.split('\\')[4])
output = pd.DataFrame(
{'ImageID':name,
'ImageName':images,
'Predictions':predictions,
'Probabilities':probs
})
希望有帮助。