顔の検出と分析
人間の顔を検出して分析する機能は、AI のコア機能です。 この演習では、画像内の顔を操作するために使用できる 2 つの Azure AI サービスである Azure AI Vision サービスと Face サービスについて説明します。
重要: このラボは、制限された機能への追加アクセスを何も要求せずに完了できます。
注: 2022 年 6 月 21 日から、個人を特定できる情報を返す Azure AI サービスの機能は、制限付きアクセスが許可されているお客様に限定されます。 さらに、感情的な状態を推測する機能は使用できなくなりました。 Microsoft が行った変更と理由について詳しくは、「顔認識に対する責任ある AI 投資と保護」をご覧ください。
このコースのリポジトリを複製する
まだ行っていない場合は、このコースのコード リポジトリを複製する必要があります。
- Visual Studio Code を起動します。
- パレットを開き (SHIFT+CTRL+P)、Git:Clone コマンドを実行して、
https://github.com/MicrosoftLearning/mslearn-ai-vision
リポジトリをローカル フォルダーに複製します (どのフォルダーでも問題ありません)。 - リポジトリを複製したら、Visual Studio Code でフォルダーを開きます。
-
リポジトリ内の C# コード プロジェクトをサポートするために追加のファイルがインストールされるまで待ちます。
注: ビルドとデバッグに必要なアセットを追加するように求めるプロンプトが表示された場合は、[今はしない] を選択します。
Azure AI サービス リソースをプロビジョニングする
サブスクリプションに Azure AI サービス リソースがまだない場合は、プロビジョニングする必要があります。
- Azure portal (
https://portal.azure.com
) を開き、ご利用の Azure サブスクリプションに関連付けられている Microsoft アカウントを使用してサインインします。 - 上部の検索バーで 「Azure AI サービス」を検索し、[Azure AI サービス] を選択し、次の設定で Azure AI サービス マルチサービス アカウント リソースを作成します。
- [サブスクリプション]:”ご自身の Azure サブスクリプション”
- リソース グループ: リソース グループを選択または作成します (制限付きサブスクリプションを使用している場合は、新しいリソース グループを作成する権限がないことがあります。提供されているものを使ってください)
- [リージョン]: 使用できるリージョンを選択します**
- [名前]: 一意の名前を入力します
- 価格レベル: Standard S0
- 必要なチェック ボックスをオンにして、リソースを作成します。
- デプロイが完了するまで待ち、デプロイの詳細を表示します。
- リソースがデプロイされたら、そこに移動して、その [キーとエンドポイント] ページを表示します。 次の手順では、このページのエンドポイントとキーの 1 つが必要になります。
Azure AI Vision SDK を使用するための準備をする
この演習では、Azure AI Vision SDK を使用して画像内の顔を分析する、部分的に実装されたクライアント アプリケーションを完成させます。
注: C# または Python 用の SDK のいずれかに使用することを選択できます。 以下の手順で、希望する言語に適したアクションを実行します。
- Visual Studio Code の [エクスプローラー] ペインで、04-face フォルダーを参照し、言語の設定に応じて C-Sharp または Python フォルダーを展開します。
-
computer-vision フォルダーを右クリックして、統合ターミナルを開きます。 次に、言語設定に適したコマンドを実行して、Azure AI Vision SDK パッケージをインストールします。
C#
dotnet add package Azure.AI.Vision.ImageAnalysis -v 1.0.0-beta.3
Python
pip install azure-ai-vision-imageanalysis==1.0.0b3
- computer-vision フォルダーの内容を表示し、構成設定用のファイルが含まれていることを確認してください。
- C# : appsettings.json
- Python: .env
-
構成ファイルを開き、構成値を更新して、Azure AI サービス リソースのエンドポイントと認証キーを反映します。 変更を保存します。
-
computer-vision フォルダーに、クライアント アプリケーションの次のコード ファイルが含まれていることを確認してください。
- C# : Program.cs
- Python: detect-people.py
-
コード ファイルを開き、上部の既存の名前空間参照の下で、「Import namespaces」というコメントを見つけます。 次に、このコメントの下に、次の言語固有のコードを追加して、Azure AI Vision SDK を使用するために必要な名前空間をインポートします。
C#
// Import namespaces using Azure.AI.Vision.ImageAnalysis;
Python
# import namespaces from azure.ai.vision.imageanalysis import ImageAnalysisClient from azure.ai.vision.imageanalysis.models import VisualFeatures from azure.core.credentials import AzureKeyCredential
分析する画像の確認
この演習では、Azure AI Vision サービスを使用して、人物の画像を分析します。
- Visual Studio Code で、computer-vision フォルダーとそれに含まれる images フォルダーを展開します。
- people.jpg 画像を選択して表示します。
画像内の顔を検出する
これで、SDK を使用して Computer Vision サービスを呼び出し、画像内の顔を検出する準備が整いました。
-
クライアント アプリケーションのコード ファイル (Program.cs または detect-people.py) の Main 関数で、構成設定を読み込むためのコードが提供されていることを確認してください。 次に、「Authenticate Azure AI Vision client」というコメントを見つけます。 次に、このコメントの下に、次の言語固有のコードを追加して、Azure AI Vision クライアント オブジェクトを作成および認証します。
C#
// Authenticate Azure AI Vision client ImageAnalysisClient cvClient = new ImageAnalysisClient( new Uri(aiSvcEndpoint), new AzureKeyCredential(aiSvcKey));
Python
# Authenticate Azure AI Vision client cv_client = ImageAnalysisClient( endpoint=ai_endpoint, credential=AzureKeyCredential(ai_key) )
-
Main 関数の追加したコードの下で、コードが画像ファイルへのパスを指定し、その画像パスを AnalyzeImage という名前の関数に渡していることを確認してください。 この関数はまだ完全には実装されていません。
-
AnalyzeImage 関数で、「Get result with specify features to be retrieved (PEOPLE)」というコメントの下に、次のコードを追加します。
C#
// Get result with specified features to be retrieved (PEOPLE) ImageAnalysisResult result = client.Analyze( BinaryData.FromStream(stream), VisualFeatures.People);
Python
# Get result with specified features to be retrieved (PEOPLE) result = cv_client.analyze( image_data=image_data, visual_features=[ VisualFeatures.PEOPLE], )
-
AnalyzeImage 関数で、「Draw bounding box around detected people」というコメントの下に、次のコードを追加します。
C#
// Draw bounding box around detected people foreach (DetectedPerson person in result.People.Values) { if (person.Confidence > 0.5) { // Draw object bounding box var r = person.BoundingBox; Rectangle rect = new Rectangle(r.X, r.Y, r.Width, r.Height); graphics.DrawRectangle(pen, rect); } // Return the confidence of the person detected //Console.WriteLine($" Bounding box {person.BoundingBox.ToString()}, Confidence: {person.Confidence:F2}"); }
Python
# Draw bounding box around detected people for detected_people in result.people.list: if(detected_people.confidence > 0.5): # Draw object bounding box r = detected_people.bounding_box bounding_box = ((r.x, r.y), (r.x + r.width, r.y + r.height)) 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))
-
変更を保存し、computer-vision フォルダーの統合ターミナルに戻り、次のコマンドを入力してプログラムを実行します。
C#
dotnet run
Python
python detect-people.py
- 検出された顔の数を示す出力を確認します。
- コード ファイルと同じフォルダーに生成された people.jpg ファイルを表示して、注釈付きの顔を確認します。 この場合、コードは顔の属性を使用してボックスの左上の場所にラベルを付け、境界ボックスの座標を使用して各顔の周りに長方形を描画しています。
サービスによって検出されたすべてのユーザーの信頼度スコアを表示する場合は、コメント Return the confidence of the person detected
の下のコード行のコメントを解除し、コードを再実行できます。
Face SDK を使用する準備をする
Azure AI Vision サービスは (他の多くの画像分析機能とともに) 基本的な顔検出を提供しますが、Face サービスは顔の分析と認識のためのより包括的な機能を提供します。
- Visual Studio Code の [エクスプローラー] ペインで、04-face フォルダーを参照し、言語の設定に応じて C-Sharp または Python フォルダーを展開します。
-
face-api フォルダーを右クリックして、統合ターミナルを開きます。 次に、言語設定に適したコマンドを実行して、Face SDK パッケージをインストールします。
C#
dotnet add package Azure.AI.Vision.Face -v 1.0.0-beta.2
Python
pip install azure-ai-vision-face==1.0.0b2
- face-api フォルダーの内容を表示し、構成設定用のファイルが含まれていることを確認してください。
- C# : appsettings.json
- Python: .env
-
構成ファイルを開き、構成値を更新して、Azure AI サービス リソースのエンドポイントと認証キーを反映します。 変更を保存します。
-
face-api フォルダーに、クライアント アプリケーションの次のコード ファイルが含まれていることを確認してください。
- C# : Program.cs
- Python: analyze-faces.py
-
コード ファイルを開き、上部の既存の名前空間参照の下で、「Import namespaces」というコメントを見つけます。 次に、このコメントの下に、次の言語固有のコードを追加して Vision SDK を使用するために必要な名前空間をインポートします。
C#
// Import namespaces using Azure; using Azure.AI.Vision.Face;
Python
# Import namespaces from azure.ai.vision.face import FaceClient from azure.ai.vision.face.models import FaceDetectionModel, FaceRecognitionModel, FaceAttributeTypeDetection03 from azure.core.credentials import AzureKeyCredential
-
Main 関数で、構成設定をロードするためのコードが提供されていることを確認してください。 次に、コメント “Authenticate Face client” を見つけます。 次に、このコメントの下に、次の言語固有のコードを追加して、FaceClient オブジェクトを作成および認証します。
C#
// Authenticate Face client faceClient = new FaceClient( new Uri(cogSvcEndpoint), new AzureKeyCredential(cogSvcKey));
Python
# Authenticate Face client face_client = FaceClient( endpoint=cog_endpoint, credential=AzureKeyCredential(cog_key) )
- Main 関数で、追加したコードの下に、コード内の関数を呼び出して Face サービスの機能を調べることができるメニューがコードに表示されることを確認してください。 この演習の残りの部分では、これらの関数を実装します。
顔を検出して分析する
顔認証サービスの最も基本的な機能の 1 つは、画像内の顔を検出し、頭部姿勢、ぼやけ、マスクの存在などの属性を決定することです。
- アプリケーションのコード ファイルの Main 関数で、ユーザーがメニュー オプション 1 を選択した場合に実行されるコードを調べます。 このコードは DetectFaces 関数を呼び出し、パスを画像ファイルに渡します。
-
コード ファイルで DetectFaces 関数を見つけて、コメント “Specify facial features to be retrieved” の下に、次のコードを追加します。
C#
// Specify facial features to be retrieved FaceAttributeType[] features = new FaceAttributeType[] { FaceAttributeType.Detection03.HeadPose, FaceAttributeType.Detection03.Blur, FaceAttributeType.Detection03.Mask };
Python
# Specify facial features to be retrieved features = [FaceAttributeTypeDetection03.HEAD_POSE, FaceAttributeTypeDetection03.BLUR, FaceAttributeTypeDetection03.MASK]
- DetectFaces 関数に追加したコードの下で、コメント “Get faces” を見つけて、次のコードを追加します。
C#
// Get faces
using (var imageData = File.OpenRead(imageFile))
{
var response = await faceClient.DetectAsync(
BinaryData.FromStream(imageData),
FaceDetectionModel.Detection03,
FaceRecognitionModel.Recognition04,
returnFaceId: false,
returnFaceAttributes: features);
IReadOnlyList<FaceDetectionResult> detected_faces = response.Value;
if (detected_faces.Count() > 0)
{
Console.WriteLine($"{detected_faces.Count()} faces detected.");
// Prepare image for drawing
Image image = Image.FromFile(imageFile);
Graphics graphics = Graphics.FromImage(image);
Pen pen = new Pen(Color.LightGreen, 3);
Font font = new Font("Arial", 4);
SolidBrush brush = new SolidBrush(Color.White);
int faceCount=0;
// Draw and annotate each face
foreach (var face in detected_faces)
{
faceCount++;
Console.WriteLine($"\nFace number {faceCount}");
// Get face properties
Console.WriteLine($" - Head Pose (Yaw): {face.FaceAttributes.HeadPose.Yaw}");
Console.WriteLine($" - Head Pose (Pitch): {face.FaceAttributes.HeadPose.Pitch}");
Console.WriteLine($" - Head Pose (Roll): {face.FaceAttributes.HeadPose.Roll}");
Console.WriteLine($" - Blur: {face.FaceAttributes.Blur.BlurLevel}");
Console.WriteLine($" - Mask: {face.FaceAttributes.Mask.Type}");
// Draw and annotate face
var r = face.FaceRectangle;
Rectangle rect = new Rectangle(r.Left, r.Top, r.Width, r.Height);
graphics.DrawRectangle(pen, rect);
string annotation = $"Face number {faceCount}";
graphics.DrawString(annotation,font,brush,r.Left, r.Top);
}
// Save annotated image
String output_file = "detected_faces.jpg";
image.Save(output_file);
Console.WriteLine(" Results saved in " + output_file);
}
}
Python
# Get faces
with open(image_file, mode="rb") as image_data:
detected_faces = face_client.detect(
image_content=image_data.read(),
detection_model=FaceDetectionModel.DETECTION03,
recognition_model=FaceRecognitionModel.RECOGNITION04,
return_face_id=False,
return_face_attributes=features,
)
if len(detected_faces) > 0:
print(len(detected_faces), 'faces detected.')
# Prepare image for drawing
fig = plt.figure(figsize=(8, 6))
plt.axis('off')
image = Image.open(image_file)
draw = ImageDraw.Draw(image)
color = 'lightgreen'
face_count = 0
# Draw and annotate each face
for face in detected_faces:
# Get face properties
face_count += 1
print('\nFace number {}'.format(face_count))
print(' - Head Pose (Yaw): {}'.format(face.face_attributes.head_pose.yaw))
print(' - Head Pose (Pitch): {}'.format(face.face_attributes.head_pose.pitch))
print(' - Head Pose (Roll): {}'.format(face.face_attributes.head_pose.roll))
print(' - Blur: {}'.format(face.face_attributes.blur.blur_level))
print(' - Mask: {}'.format(face.face_attributes.mask.type))
# Draw and annotate face
r = face.face_rectangle
bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
draw = ImageDraw.Draw(image)
draw.rectangle(bounding_box, outline=color, width=5)
annotation = 'Face number {}'.format(face_count)
plt.annotate(annotation,(r.left, r.top), backgroundcolor=color)
# Save annotated image
plt.imshow(image)
outputfile = 'detected_faces.jpg'
fig.savefig(outputfile)
print('\nResults saved in', outputfile)
- DetectFaces 関数に追加したコードを調べます。 画像ファイルを分析し、頭部姿勢、ぼやけ、マスクの存在などの属性も含めて、その画像ファイルに含まれるすべての顔を検出します。 各顔に割り当てられた一意の顔識別子を含む、各顔の詳細が表示されます。顔の位置は、境界ボックスを使用して画像に示されます。
-
変更を保存して face-api フォルダーの統合ターミナルに戻り、次のコマンドを入力してプログラムを実行します。
C#
dotnet run
C# 出力に、await 演算子を使用している非同期関数に関する警告が表示される場合があります。これは無視してもかまいません。
Python
python analyze-faces.py
- プロンプトが表示されたら、「1」と入力し、出力を観察します。ここには、検出された各顔の ID と属性が含まれているはずです。
- コード ファイルと同じフォルダーに生成された detected_faces.jpg ファイルを表示して、注釈付きの顔を確認します。
詳細情報
Face サービスにはいくつかの追加機能がありますが、責任ある AI 標準に従うと、制限付きアクセス ポリシーで制限されます。 これらの機能には、顔認識モデルの識別、検証、作成が含まれます。 詳細とアクセスの申請については、「Azure AI サービスの制限付きアクセス」を参照してください。
顔検出に Azure AI Vision サービスを使用する方法の詳細については、「Azure AI Vision のドキュメント」を参照してください。
Face サービスの詳細については、Face のドキュメントを参照してください。