簡體   English   中英

使用Azure Blob存儲中的圖像作為輸入

[英]Using Images in Azure Blob Storage as input

我正在一個項目中,在該項目中,我使用構建的keras模型對存儲在Azure Blob存儲中的圖像進行分類並將結果導出為.csv文件。 通過使用get_blob_to_path並將一些圖像下載到我的筆記本電腦上,我能夠做到這一點。 但是,由於圖片太多,因此我希望不通過get_blob_to_bytes或get_blob_to_stream下載圖片。

實際上,一種無需先下載就從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
})

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM