I am using Azure AI computer vision for Search photos with image retrieval service. I have created the service in Central India and East US region with Standard pricing tier. I am using the API key and endpoint for image embedding in jupyter notebook.
Here is my code:
def image_embedding(image, endpoint, key):
"""
Image embedding using Azure Computer Vision 4.0 (Vectorize Image API)
"""
with open(image, 'rb') as img:
data = img.read()
# Image retrieval API
version = "?api-version=2023-02-01-preview&modelVersion=latest"
vectorize_img_url = endpoint + "retrieval:vectorizeImage" + version
headers = {
'Content-type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': key
}
try:
r = requests.post(vectorize_img_url, data=data, headers=headers)
if r.status_code == 200:
image_vector = r.json()['vector']
return image_vector
else:
print(f"An error occurred while processing {image}. Error code: {r.status_code}.")
except Exception as e:
print(f"An error occurred while processing {image}: {e}")
return None
# Load environment variables
load_dotenv()
endpoint ='https://Service_Name.cognitiveservices.azure.com/'
key = 'API_Key'
# image embedding
image = r'C:\Users\HP\Downloads\[Hidden].jpg'
image_emb = image_embedding(image, endpoint, key)
# display the image
plt.imshow(Image.open(image))
plt.axis('off')
plt.show()
The error seems to be associated with API endpoint.
Key points to consider:
- The code is error free and is tested earlier with correct outputs.
- API endpoint and key are copied correctly.
- Image path is accurate.
- All libraries are loaded.