Detecting faces in an Image - The Issue
For reference to the specific exercise being done, please see the following. For reference to the detect-people.py
file being executed, please see:
from dotenv import load_dotenv
import os
from array import array
from PIL import Image, ImageDraw
import sys
import time
from matplotlib import pyplot as plt
import numpy as np
# import namespaces
import azure.ai.vision as sdk
def main():
global cv_client
try:
# Get Configuration Settings
load_dotenv()
ai_endpoint = os.getenv('AI_SERVICE_ENDPOINT')
ai_key = os.getenv('AI_SERVICE_KEY')
# Get image
image_file = 'images/people.jpg'
if len(sys.argv) > 1:
image_file = sys.argv[1]
# Authenticate Azure AI Vision client
cv_client = sdk.VisionServiceOptions(ai_endpoint, ai_key)
# Analyze image
AnalyzeImage(image_file, cv_client)
except Exception as ex:
print(ex)
def AnalyzeImage(image_file, cv_client):
print('\nAnalyzing', image_file)
# Specify features to be retrieved (PEOPLE)
analysis_options = sdk.ImageAnalysisOptions()
features = analysis_options.features = (
sdk.ImageAnalysisFeature.PEOPLE
)
# Get image analysis
image = sdk.VisionSource(image_file)
image_analyzer = sdk.ImageAnalyzer(cv_client, image, analysis_options)
result = image_analyzer.analyze()
if result.reason == sdk.ImageAnalysisResultReason.ANALYZED:
# Get people in the image
if result.people is not None:
print("\nPeople in image:")
# Prepare image for drawing
image = Image.open(image_file)
fig = plt.figure(figsize=(image.width/100, image.height/100))
plt.axis('off')
draw = ImageDraw.Draw(image)
color = 'cyan'
for detected_people in result.people:
# Draw object bounding box if confidence > 50%
if detected_people.confidence > 0.5:
# Draw object bounding box
r = detected_people.bounding_box
bounding_box = ((r.x, r.y), (r.x + r.w, r.y + r.h))
draw.rectangle(bounding_box, outline=color, width=3)
# Return the confidence of the person detected
print(" {} (confidence: {:.2f}%)".format(detected_people.bounding_box, detected_people.confidence * 100))
# Save annotated image
plt.imshow(image)
plt.tight_layout(pad=0)
outputfile = 'detected_people.jpg'
fig.savefig(outputfile)
print(' Results saved in', outputfile)
else:
error_details = sdk.ImageAnalysisErrorDetails.from_result(result)
print(" Analysis failed.")
print(" Error reason: {}".format(error_details.reason))
print(" Error code: {}".format(error_details.error_code))
print(" Error message: {}".format(error_details.message))
if __name__ == "__main__":
main()
When I execute the Python command:
python detect-people.py
or in my case:
python3 detect-people.py
I receive the following error:
module 'azure.ai.vision' has no attribute 'VisionServiceOptions'
Related to issues from the code input for Authentication of the SDK at
# import namespaces
import azure.ai.vision as sdk
Attempting to Diagnose the Issue
I execute detect-people.py
in my zsh shell from the folder that I cloned the repository. I've gone into my file path where Python is located to locate the frame work and all I can find are the Python modules related to Azure AI 102 Modules for Analyze Images Exercise 1, which are azure.ai.vision.imageanalysis modules. I opened that configuration file and compared it to how the detect-people.py
file configures authentication for Azure and I literally cannot find an azure.ai.vision module configuration file - my vision folder goes straight to a subsequent imageanalysis folder. Therefore, where my Python interpreter attempts to authenticate the SDK for access to the "VisionServiceOptions" features, it cannot. The authentication of the SDK is supposed to occur here:
# Authenticate Azure AI Vision client
cv_client = sdk.VisionServiceOptions(ai_endpoint, ai_key)
I have compared the configuration file from azure.ai.vision.imageanalysis to see if I can make something analogous to that to gain access to the 'VisionServiceOptions' features, but then I look into my actual folders and see no code for 'VisionServiceOptions', so even if I rewrote the script to pull from 'imageanalysis', I wouldn't have any 'VisionServiceOptions' features to do the face detection. I've also searched to see if the Face Detection features may be named something different so I pull something different from azure.ai.vision/sdk
Examining Correlation with an Open and Related Issue
In a previous issue that was raised, I'm unsure if the issue with installing 'azure-ai-vision' is the root of the issue. When attempting to execute pip install azure-ai-vision==0.15.1b1
, I receive the following:
ERROR: Could not find a version that satisfies the requirement azure-ai-vision==0.15.1b1 (from versions: none)
ERROR: No matching distribution found for azure-ai-vision==0.15.1b1
In another raised issue that is still open (#10), I agreed, at the time, with the original poster that the solution may be to use the command:
pip install azure-ai-vision-imageanalysis
However, I believe only being able to execute this command successfully may be why I'm experiencing the problem that I'm having at the moment - I never installed the VisionServiceOptions. Please help, I have a deadline to complete my training in this within a week!