を使用した Amazon Rekognition の例 AWS SDK for .NET - AWS SDK for .NET

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

を使用した Amazon Rekognition の例 AWS SDK for .NET

次のコード例は、Amazon Rekognition AWS SDK for .NET で を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。 Amazon Rekognition

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

「シナリオ」は、1 つのサービス内から、または他の AWS サービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。

各例には、完全なソースコードへのリンクが含まれています。このリンクには、コンテキスト内でコードをセットアップして実行する方法の手順が記載されています。

アクション

次の例は、CompareFaces を使用する方法を説明しています。

詳細については、「イメージ内の顔を比較する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.IO; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to compare faces in two images. /// </summary> public class CompareFaces { public static async Task Main() { float similarityThreshold = 70F; string sourceImage = "source.jpg"; string targetImage = "target.jpg"; var rekognitionClient = new AmazonRekognitionClient(); Amazon.Rekognition.Model.Image imageSource = new Amazon.Rekognition.Model.Image(); try { using FileStream fs = new FileStream(sourceImage, FileMode.Open, FileAccess.Read); byte[] data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); imageSource.Bytes = new MemoryStream(data); } catch (Exception) { Console.WriteLine($"Failed to load source image: {sourceImage}"); return; } Amazon.Rekognition.Model.Image imageTarget = new Amazon.Rekognition.Model.Image(); try { using FileStream fs = new FileStream(targetImage, FileMode.Open, FileAccess.Read); byte[] data = new byte[fs.Length]; data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); imageTarget.Bytes = new MemoryStream(data); } catch (Exception ex) { Console.WriteLine($"Failed to load target image: {targetImage}"); Console.WriteLine(ex.Message); return; } var compareFacesRequest = new CompareFacesRequest { SourceImage = imageSource, TargetImage = imageTarget, SimilarityThreshold = similarityThreshold, }; // Call operation var compareFacesResponse = await rekognitionClient.CompareFacesAsync(compareFacesRequest); // Display results compareFacesResponse.FaceMatches.ForEach(match => { ComparedFace face = match.Face; BoundingBox position = face.BoundingBox; Console.WriteLine($"Face at {position.Left} {position.Top} matches with {match.Similarity}% confidence."); }); Console.WriteLine($"Found {compareFacesResponse.UnmatchedFaces.Count} face(s) that did not match."); } }
  • API 詳細については、「 リファレンスCompareFaces」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、CreateCollection を使用する方法を示しています。

詳細については、「コレクションを作成する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses Amazon Rekognition to create a collection to which you can add /// faces using the IndexFaces operation. /// </summary> public class CreateCollection { public static async Task Main() { var rekognitionClient = new AmazonRekognitionClient(); string collectionId = "MyCollection"; Console.WriteLine("Creating collection: " + collectionId); var createCollectionRequest = new CreateCollectionRequest { CollectionId = collectionId, }; CreateCollectionResponse createCollectionResponse = await rekognitionClient.CreateCollectionAsync(createCollectionRequest); Console.WriteLine($"CollectionArn : {createCollectionResponse.CollectionArn}"); Console.WriteLine($"Status code : {createCollectionResponse.StatusCode}"); } }
  • API 詳細については、「 リファレンスCreateCollection」の「」を参照してください。 AWS SDK for .NET API

次の例は、DeleteCollection を使用する方法を説明しています。

詳細については、「コレクションを削除する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to delete an existing collection. /// </summary> public class DeleteCollection { public static async Task Main() { var rekognitionClient = new AmazonRekognitionClient(); string collectionId = "MyCollection"; Console.WriteLine("Deleting collection: " + collectionId); var deleteCollectionRequest = new DeleteCollectionRequest() { CollectionId = collectionId, }; var deleteCollectionResponse = await rekognitionClient.DeleteCollectionAsync(deleteCollectionRequest); Console.WriteLine($"{collectionId}: {deleteCollectionResponse.StatusCode}"); } }
  • API 詳細については、「 リファレンスDeleteCollection」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、DeleteFaces を使用する方法を示しています。

詳細については、「コレクションから顔を削除する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to delete one or more faces from /// a Rekognition collection. /// </summary> public class DeleteFaces { public static async Task Main() { string collectionId = "MyCollection"; var faces = new List<string> { "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }; var rekognitionClient = new AmazonRekognitionClient(); var deleteFacesRequest = new DeleteFacesRequest() { CollectionId = collectionId, FaceIds = faces, }; DeleteFacesResponse deleteFacesResponse = await rekognitionClient.DeleteFacesAsync(deleteFacesRequest); deleteFacesResponse.DeletedFaces.ForEach(face => { Console.WriteLine($"FaceID: {face}"); }); } }
  • API 詳細については、「 リファレンスDeleteFaces」の「」を参照してください。 AWS SDK for .NET API

次の例は、DescribeCollection を使用する方法を説明しています。

詳細については、「コレクションを定義する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to describe the contents of a /// collection. /// </summary> public class DescribeCollection { public static async Task Main() { var rekognitionClient = new AmazonRekognitionClient(); string collectionId = "MyCollection"; Console.WriteLine($"Describing collection: {collectionId}"); var describeCollectionRequest = new DescribeCollectionRequest() { CollectionId = collectionId, }; var describeCollectionResponse = await rekognitionClient.DescribeCollectionAsync(describeCollectionRequest); Console.WriteLine($"Collection ARN: {describeCollectionResponse.CollectionARN}"); Console.WriteLine($"Face count: {describeCollectionResponse.FaceCount}"); Console.WriteLine($"Face model version: {describeCollectionResponse.FaceModelVersion}"); Console.WriteLine($"Created: {describeCollectionResponse.CreationTimestamp}"); } }
  • API 詳細については、「 リファレンスDescribeCollection」の「」を参照してください。 AWS SDK for .NET API

次の例は、DetectFaces を使用する方法を説明しています。

詳細については、「イメージ内の顔を検出する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect faces within an image /// stored in an Amazon Simple Storage Service (Amazon S3) bucket. /// </summary> public class DetectFaces { public static async Task Main() { string photo = "input.jpg"; string bucket = "bucket"; var rekognitionClient = new AmazonRekognitionClient(); var detectFacesRequest = new DetectFacesRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket, }, }, // Attributes can be "ALL" or "DEFAULT". // "DEFAULT": BoundingBox, Confidence, Landmarks, Pose, and Quality. // "ALL": See https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Rekognition/TFaceDetail.html Attributes = new List<string>() { "ALL" }, }; try { DetectFacesResponse detectFacesResponse = await rekognitionClient.DetectFacesAsync(detectFacesRequest); bool hasAll = detectFacesRequest.Attributes.Contains("ALL"); foreach (FaceDetail face in detectFacesResponse.FaceDetails) { Console.WriteLine($"BoundingBox: top={face.BoundingBox.Left} left={face.BoundingBox.Top} width={face.BoundingBox.Width} height={face.BoundingBox.Height}"); Console.WriteLine($"Confidence: {face.Confidence}"); Console.WriteLine($"Landmarks: {face.Landmarks.Count}"); Console.WriteLine($"Pose: pitch={face.Pose.Pitch} roll={face.Pose.Roll} yaw={face.Pose.Yaw}"); Console.WriteLine($"Brightness: {face.Quality.Brightness}\tSharpness: {face.Quality.Sharpness}"); if (hasAll) { Console.WriteLine($"Estimated age is between {face.AgeRange.Low} and {face.AgeRange.High} years old."); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }

イメージ内のすべての顔の境界ボックス情報を表示します。

using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to display the details of the /// bounding boxes around the faces detected in an image. /// </summary> public class ImageOrientationBoundingBox { public static async Task Main() { string photo = @"D:\Development\AWS-Examples\Rekognition\target.jpg"; // "photo.jpg"; var rekognitionClient = new AmazonRekognitionClient(); var image = new Amazon.Rekognition.Model.Image(); try { using var fs = new FileStream(photo, FileMode.Open, FileAccess.Read); byte[] data = null; data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); image.Bytes = new MemoryStream(data); } catch (Exception) { Console.WriteLine("Failed to load file " + photo); return; } int height; int width; // Used to extract original photo width/height using (var imageBitmap = new Bitmap(photo)) { height = imageBitmap.Height; width = imageBitmap.Width; } Console.WriteLine("Image Information:"); Console.WriteLine(photo); Console.WriteLine("Image Height: " + height); Console.WriteLine("Image Width: " + width); try { var detectFacesRequest = new DetectFacesRequest() { Image = image, Attributes = new List<string>() { "ALL" }, }; DetectFacesResponse detectFacesResponse = await rekognitionClient.DetectFacesAsync(detectFacesRequest); detectFacesResponse.FaceDetails.ForEach(face => { Console.WriteLine("Face:"); ShowBoundingBoxPositions( height, width, face.BoundingBox, detectFacesResponse.OrientationCorrection); Console.WriteLine($"BoundingBox: top={face.BoundingBox.Left} left={face.BoundingBox.Top} width={face.BoundingBox.Width} height={face.BoundingBox.Height}"); Console.WriteLine($"The detected face is estimated to be between {face.AgeRange.Low} and {face.AgeRange.High} years old.\n"); }); } catch (Exception ex) { Console.WriteLine(ex.Message); } } /// <summary> /// Display the bounding box information for an image. /// </summary> /// <param name="imageHeight">The height of the image.</param> /// <param name="imageWidth">The width of the image.</param> /// <param name="box">The bounding box for a face found within the image.</param> /// <param name="rotation">The rotation of the face's bounding box.</param> public static void ShowBoundingBoxPositions(int imageHeight, int imageWidth, BoundingBox box, string rotation) { float left; float top; if (rotation == null) { Console.WriteLine("No estimated orientation. Check Exif data."); return; } // Calculate face position based on image orientation. switch (rotation) { case "ROTATE_0": left = imageWidth * box.Left; top = imageHeight * box.Top; break; case "ROTATE_90": left = imageHeight * (1 - (box.Top + box.Height)); top = imageWidth * box.Left; break; case "ROTATE_180": left = imageWidth - (imageWidth * (box.Left + box.Width)); top = imageHeight * (1 - (box.Top + box.Height)); break; case "ROTATE_270": left = imageHeight * box.Top; top = imageWidth * (1 - box.Left - box.Width); break; default: Console.WriteLine("No estimated orientation information. Check Exif data."); return; } // Display face location information. Console.WriteLine($"Left: {left}"); Console.WriteLine($"Top: {top}"); Console.WriteLine($"Face Width: {imageWidth * box.Width}"); Console.WriteLine($"Face Height: {imageHeight * box.Height}"); } }
  • API 詳細については、「 リファレンスDetectFaces」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、DetectLabels を使用する方法を示しています。

詳細については、「イメージ内のラベルを検出する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect labels within an image /// stored in an Amazon Simple Storage Service (Amazon S3) bucket. /// </summary> public class DetectLabels { public static async Task Main() { string photo = "del_river_02092020_01.jpg"; // "input.jpg"; string bucket = "igsmiths3photos"; // "bucket"; var rekognitionClient = new AmazonRekognitionClient(); var detectlabelsRequest = new DetectLabelsRequest { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket, }, }, MaxLabels = 10, MinConfidence = 75F, }; try { DetectLabelsResponse detectLabelsResponse = await rekognitionClient.DetectLabelsAsync(detectlabelsRequest); Console.WriteLine("Detected labels for " + photo); foreach (Label label in detectLabelsResponse.Labels) { Console.WriteLine($"Name: {label.Name} Confidence: {label.Confidence}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }

コンピュータに保存されているイメージファイル内のラベルを検出します。

using System; using System.IO; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect labels within an image /// stored locally. /// </summary> public class DetectLabelsLocalFile { public static async Task Main() { string photo = "input.jpg"; var image = new Amazon.Rekognition.Model.Image(); try { using var fs = new FileStream(photo, FileMode.Open, FileAccess.Read); byte[] data = null; data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); image.Bytes = new MemoryStream(data); } catch (Exception) { Console.WriteLine("Failed to load file " + photo); return; } var rekognitionClient = new AmazonRekognitionClient(); var detectlabelsRequest = new DetectLabelsRequest { Image = image, MaxLabels = 10, MinConfidence = 77F, }; try { DetectLabelsResponse detectLabelsResponse = await rekognitionClient.DetectLabelsAsync(detectlabelsRequest); Console.WriteLine($"Detected labels for {photo}"); foreach (Label label in detectLabelsResponse.Labels) { Console.WriteLine($"{label.Name}: {label.Confidence}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
  • API 詳細については、「 リファレンスDetectLabels」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、DetectModerationLabels を使用する方法を示しています。

詳細については、「不適切なイメージを検出する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect unsafe content in a /// JPEG or PNG format image. /// </summary> public class DetectModerationLabels { public static async Task Main(string[] args) { string photo = "input.jpg"; string bucket = "bucket"; var rekognitionClient = new AmazonRekognitionClient(); var detectModerationLabelsRequest = new DetectModerationLabelsRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket, }, }, MinConfidence = 60F, }; try { var detectModerationLabelsResponse = await rekognitionClient.DetectModerationLabelsAsync(detectModerationLabelsRequest); Console.WriteLine("Detected labels for " + photo); foreach (ModerationLabel label in detectModerationLabelsResponse.ModerationLabels) { Console.WriteLine($"Label: {label.Name}"); Console.WriteLine($"Confidence: {label.Confidence}"); Console.WriteLine($"Parent: {label.ParentName}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
  • API 詳細については、「 リファレンスDetectModerationLabels」の「」を参照してください。 AWS SDK for .NET API

次の例は、DetectText を使用する方法を説明しています。

詳細については、「イメージ内のテキストを検出する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect text in an image. The /// example was created using the AWS SDK for .NET version 3.7 and .NET /// Core 5.0. /// </summary> public class DetectText { public static async Task Main() { string photo = "Dad_photographer.jpg"; // "input.jpg"; string bucket = "igsmiths3photos"; // "bucket"; var rekognitionClient = new AmazonRekognitionClient(); var detectTextRequest = new DetectTextRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket, }, }, }; try { DetectTextResponse detectTextResponse = await rekognitionClient.DetectTextAsync(detectTextRequest); Console.WriteLine($"Detected lines and words for {photo}"); detectTextResponse.TextDetections.ForEach(text => { Console.WriteLine($"Detected: {text.DetectedText}"); Console.WriteLine($"Confidence: {text.Confidence}"); Console.WriteLine($"Id : {text.Id}"); Console.WriteLine($"Parent Id: {text.ParentId}"); Console.WriteLine($"Type: {text.Type}"); }); } catch (Exception e) { Console.WriteLine(e.Message); } } }
  • API 詳細については、「 リファレンスDetectText」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、GetCelebrityInfo を使用する方法を示しています。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Shows how to use Amazon Rekognition to retrieve information about the /// celebrity identified by the supplied celebrity Id. /// </summary> public class CelebrityInfo { public static async Task Main() { string celebId = "nnnnnnnn"; var rekognitionClient = new AmazonRekognitionClient(); var celebrityInfoRequest = new GetCelebrityInfoRequest { Id = celebId, }; Console.WriteLine($"Getting information for celebrity: {celebId}"); var celebrityInfoResponse = await rekognitionClient.GetCelebrityInfoAsync(celebrityInfoRequest); // Display celebrity information. Console.WriteLine($"celebrity name: {celebrityInfoResponse.Name}"); Console.WriteLine("Further information (if available):"); celebrityInfoResponse.Urls.ForEach(url => { Console.WriteLine(url); }); } }
  • API 詳細については、「 リファレンスGetCelebrityInfo」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、IndexFaces を使用する方法を示しています。

詳細については、「コレクションに顔を追加する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect faces in an image /// that has been uploaded to an Amazon Simple Storage Service (Amazon S3) /// bucket and then adds the information to a collection. /// </summary> public class AddFaces { public static async Task Main() { string collectionId = "MyCollection2"; string bucket = "doc-example-bucket"; string photo = "input.jpg"; var rekognitionClient = new AmazonRekognitionClient(); var image = new Image { S3Object = new S3Object { Bucket = bucket, Name = photo, }, }; var indexFacesRequest = new IndexFacesRequest { Image = image, CollectionId = collectionId, ExternalImageId = photo, DetectionAttributes = new List<string>() { "ALL" }, }; IndexFacesResponse indexFacesResponse = await rekognitionClient.IndexFacesAsync(indexFacesRequest); Console.WriteLine($"{photo} added"); foreach (FaceRecord faceRecord in indexFacesResponse.FaceRecords) { Console.WriteLine($"Face detected: Faceid is {faceRecord.Face.FaceId}"); } } }
  • API 詳細については、「 リファレンスIndexFaces」の「」を参照してください。 AWS SDK for .NET API

次の例は、ListCollections を使用する方法を説明しています。

コレクションの詳細については、「コレクションを一覧表示する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses Amazon Rekognition to list the collection IDs in the /// current account. /// </summary> public class ListCollections { public static async Task Main() { var rekognitionClient = new AmazonRekognitionClient(); Console.WriteLine("Listing collections"); int limit = 10; var listCollectionsRequest = new ListCollectionsRequest { MaxResults = limit, }; var listCollectionsResponse = new ListCollectionsResponse(); do { if (listCollectionsResponse is not null) { listCollectionsRequest.NextToken = listCollectionsResponse.NextToken; } listCollectionsResponse = await rekognitionClient.ListCollectionsAsync(listCollectionsRequest); listCollectionsResponse.CollectionIds.ForEach(id => { Console.WriteLine(id); }); } while (listCollectionsResponse.NextToken is not null); } }
  • API 詳細については、「 リファレンスListCollections」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、ListFaces を使用する方法を示しています。

詳細については、「コレクションに顔を保存する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to retrieve the list of faces /// stored in a collection. /// </summary> public class ListFaces { public static async Task Main() { string collectionId = "MyCollection2"; var rekognitionClient = new AmazonRekognitionClient(); var listFacesResponse = new ListFacesResponse(); Console.WriteLine($"Faces in collection {collectionId}"); var listFacesRequest = new ListFacesRequest { CollectionId = collectionId, MaxResults = 1, }; do { listFacesResponse = await rekognitionClient.ListFacesAsync(listFacesRequest); listFacesResponse.Faces.ForEach(face => { Console.WriteLine(face.FaceId); }); listFacesRequest.NextToken = listFacesResponse.NextToken; } while (!string.IsNullOrEmpty(listFacesResponse.NextToken)); } }
  • API 詳細については、「 リファレンスListFaces」の「」を参照してください。 AWS SDK for .NET API

次の例は、RecognizeCelebrities を使用する方法を説明しています。

詳細については、「イメージ内で有名人を認識する」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.IO; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Shows how to use Amazon Rekognition to identify celebrities in a photo. /// </summary> public class CelebritiesInImage { public static async Task Main(string[] args) { string photo = "moviestars.jpg"; var rekognitionClient = new AmazonRekognitionClient(); var recognizeCelebritiesRequest = new RecognizeCelebritiesRequest(); var img = new Amazon.Rekognition.Model.Image(); byte[] data = null; try { using var fs = new FileStream(photo, FileMode.Open, FileAccess.Read); data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); } catch (Exception) { Console.WriteLine($"Failed to load file {photo}"); return; } img.Bytes = new MemoryStream(data); recognizeCelebritiesRequest.Image = img; Console.WriteLine($"Looking for celebrities in image {photo}\n"); var recognizeCelebritiesResponse = await rekognitionClient.RecognizeCelebritiesAsync(recognizeCelebritiesRequest); Console.WriteLine($"{recognizeCelebritiesResponse.CelebrityFaces.Count} celebrity(s) were recognized.\n"); recognizeCelebritiesResponse.CelebrityFaces.ForEach(celeb => { Console.WriteLine($"Celebrity recognized: {celeb.Name}"); Console.WriteLine($"Celebrity ID: {celeb.Id}"); BoundingBox boundingBox = celeb.Face.BoundingBox; Console.WriteLine($"position: {boundingBox.Left} {boundingBox.Top}"); Console.WriteLine("Further information (if available):"); celeb.Urls.ForEach(url => { Console.WriteLine(url); }); }); Console.WriteLine($"{recognizeCelebritiesResponse.UnrecognizedFaces.Count} face(s) were unrecognized."); } }
  • API 詳細については、「 リファレンスRecognizeCelebrities」の「」を参照してください。 AWS SDK for .NET API

次のコード例は、SearchFaces を使用する方法を示しています。

詳細については、顔 (フェイス ID) を検索する を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to find faces in an image that /// match the face Id provided in the method request. /// </summary> public class SearchFacesMatchingId { public static async Task Main() { string collectionId = "MyCollection"; string faceId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; var rekognitionClient = new AmazonRekognitionClient(); // Search collection for faces matching the face id. var searchFacesRequest = new SearchFacesRequest { CollectionId = collectionId, FaceId = faceId, FaceMatchThreshold = 70F, MaxFaces = 2, }; SearchFacesResponse searchFacesResponse = await rekognitionClient.SearchFacesAsync(searchFacesRequest); Console.WriteLine("Face matching faceId " + faceId); Console.WriteLine("Matche(s): "); searchFacesResponse.FaceMatches.ForEach(face => { Console.WriteLine($"FaceId: {face.Face.FaceId} Similarity: {face.Similarity}"); }); } }
  • API 詳細については、「 リファレンスSearchFaces」の「」を参照してください。 AWS SDK for .NET API

次の例は、SearchFacesByImage を使用する方法を説明しています。

詳細については、「顔を検索する (イメージ)」を参照してください。

AWS SDK for .NET
注記

については、「」を参照してください GitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to search for images matching those /// in a collection. /// </summary> public class SearchFacesMatchingImage { public static async Task Main() { string collectionId = "MyCollection"; string bucket = "bucket"; string photo = "input.jpg"; var rekognitionClient = new AmazonRekognitionClient(); // Get an image object from S3 bucket. var image = new Image() { S3Object = new S3Object() { Bucket = bucket, Name = photo, }, }; var searchFacesByImageRequest = new SearchFacesByImageRequest() { CollectionId = collectionId, Image = image, FaceMatchThreshold = 70F, MaxFaces = 2, }; SearchFacesByImageResponse searchFacesByImageResponse = await rekognitionClient.SearchFacesByImageAsync(searchFacesByImageRequest); Console.WriteLine("Faces matching largest face in image from " + photo); searchFacesByImageResponse.FaceMatches.ForEach(face => { Console.WriteLine($"FaceId: {face.Face.FaceId}, Similarity: {face.Similarity}"); }); } }
  • API 詳細については、「 リファレンスSearchFacesByImage」の「」を参照してください。 AWS SDK for .NET API

シナリオ

次のコード例では、ユーザーがラベルを使用して写真を管理できるサーバーレスアプリケーションを作成する方法について示しています。

AWS SDK for .NET

Amazon Rekognition を使用して画像内のラベルを検出し、保存して後で取得できるようにする写真アセット管理アプリケーションの開発方法を示します。

完全なソースコードとセットアップと実行の手順については、「」の詳細な例を参照してください GitHub

この例のソースについて詳しくは、AWS  コミュニティでブログ投稿を参照してください。

この例で使用されているサービス
  • API ゲートウェイ

  • DynamoDB

  • Lambda

  • Amazon Rekognition

  • Amazon S3

  • Amazon SNS

次のコード例は、Amazon Rekognition を使用してイメージ内のオブジェクトをカテゴリ別に検出するアプリを構築する方法を示しています。

AWS SDK for .NET

Amazon Rekognition を使用して、NETAmazon Rekognition を使用して Amazon Simple Storage Service (Amazon S3) バケットにあるイメージ内のオブジェクトをカテゴリ別に識別するアプリAPIを作成する方法を示します。アプリは、Amazon Simple Email Service (Amazon ) を使用して、結果を含む E メール通知を管理者に送信しますSES。

完全なソースコードとセットアップと実行の手順については、「」の詳細な例を参照してくださいGitHub

この例で使用されているサービス
  • Amazon Rekognition

  • Amazon S3

  • Amazon SES