На самом деле, более простое решение для загрузки изображений из хранилища BLOB-объектов Azure без предварительной загрузки - создать URL-адрес блоба с токеном sas для передачи на 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
})
Надеюсь, это поможет.