RecognizeCelebrities 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

RecognizeCelebrities 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 RecognizeCelebrities

如需詳細資訊,請參閱在映像中辨識名人

.NET
AWS SDK for .NET
注意

還有更多 on 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."); } }
CLI
AWS CLI

辨識影像中的名人

下列recognize-celebrities命令可識別存放在 Amazon S3 儲存貯體中指定映像中的名人。:

aws rekognition recognize-celebrities \ --image "S3Object={Bucket=MyImageS3Bucket,Name=moviestars.jpg}"

輸出:

{ "UnrecognizedFaces": [ { "BoundingBox": { "Width": 0.14416666328907013, "Top": 0.07777778059244156, "Left": 0.625, "Height": 0.2746031880378723 }, "Confidence": 99.9990234375, "Pose": { "Yaw": 10.80408763885498, "Roll": -12.761146545410156, "Pitch": 10.96889877319336 }, "Quality": { "Sharpness": 94.1185531616211, "Brightness": 79.18367004394531 }, "Landmarks": [ { "Y": 0.18220913410186768, "X": 0.6702951788902283, "Type": "eyeLeft" }, { "Y": 0.16337193548679352, "X": 0.7188183665275574, "Type": "eyeRight" }, { "Y": 0.20739148557186127, "X": 0.7055801749229431, "Type": "nose" }, { "Y": 0.2889308035373688, "X": 0.687512218952179, "Type": "mouthLeft" }, { "Y": 0.2706988751888275, "X": 0.7250053286552429, "Type": "mouthRight" } ] } ], "CelebrityFaces": [ { "MatchConfidence": 100.0, "Face": { "BoundingBox": { "Width": 0.14000000059604645, "Top": 0.1190476194024086, "Left": 0.82833331823349, "Height": 0.2666666805744171 }, "Confidence": 99.99359130859375, "Pose": { "Yaw": -10.509642601013184, "Roll": -14.51749324798584, "Pitch": 13.799399375915527 }, "Quality": { "Sharpness": 78.74752044677734, "Brightness": 42.201324462890625 }, "Landmarks": [ { "Y": 0.2290833294391632, "X": 0.8709492087364197, "Type": "eyeLeft" }, { "Y": 0.20639978349208832, "X": 0.9153988361358643, "Type": "eyeRight" }, { "Y": 0.25417643785476685, "X": 0.8907724022865295, "Type": "nose" }, { "Y": 0.32729196548461914, "X": 0.8876466155052185, "Type": "mouthLeft" }, { "Y": 0.3115464746952057, "X": 0.9238573312759399, "Type": "mouthRight" } ] }, "Name": "Celeb A", "Urls": [ "www.imdb.com/name/aaaaaaaaa" ], "Id": "1111111" }, { "MatchConfidence": 97.0, "Face": { "BoundingBox": { "Width": 0.13333334028720856, "Top": 0.24920634925365448, "Left": 0.4449999928474426, "Height": 0.2539682686328888 }, "Confidence": 99.99979400634766, "Pose": { "Yaw": 6.557040691375732, "Roll": -7.316643714904785, "Pitch": 9.272967338562012 }, "Quality": { "Sharpness": 83.23492431640625, "Brightness": 78.83267974853516 }, "Landmarks": [ { "Y": 0.3625510632991791, "X": 0.48898839950561523, "Type": "eyeLeft" }, { "Y": 0.35366007685661316, "X": 0.5313721299171448, "Type": "eyeRight" }, { "Y": 0.3894785940647125, "X": 0.5173314809799194, "Type": "nose" }, { "Y": 0.44889405369758606, "X": 0.5020005702972412, "Type": "mouthLeft" }, { "Y": 0.4408611059188843, "X": 0.5351271629333496, "Type": "mouthRight" } ] }, "Name": "Celeb B", "Urls": [ "www.imdb.com/name/bbbbbbbbb" ], "Id": "2222222" }, { "MatchConfidence": 100.0, "Face": { "BoundingBox": { "Width": 0.12416666746139526, "Top": 0.2968254089355469, "Left": 0.2150000035762787, "Height": 0.23650793731212616 }, "Confidence": 99.99958801269531, "Pose": { "Yaw": 7.801797866821289, "Roll": -8.326810836791992, "Pitch": 7.844768047332764 }, "Quality": { "Sharpness": 86.93206024169922, "Brightness": 79.81291198730469 }, "Landmarks": [ { "Y": 0.4027804136276245, "X": 0.2575301229953766, "Type": "eyeLeft" }, { "Y": 0.3934555947780609, "X": 0.2956969439983368, "Type": "eyeRight" }, { "Y": 0.4309830069541931, "X": 0.2837020754814148, "Type": "nose" }, { "Y": 0.48186683654785156, "X": 0.26812544465065, "Type": "mouthLeft" }, { "Y": 0.47338807582855225, "X": 0.29905644059181213, "Type": "mouthRight" } ] }, "Name": "Celeb C", "Urls": [ "www.imdb.com/name/ccccccccc" ], "Id": "3333333" }, { "MatchConfidence": 97.0, "Face": { "BoundingBox": { "Width": 0.11916666477918625, "Top": 0.3698412775993347, "Left": 0.008333333767950535, "Height": 0.22698412835597992 }, "Confidence": 99.99999237060547, "Pose": { "Yaw": 16.38478660583496, "Roll": -1.0260354280471802, "Pitch": 5.975185394287109 }, "Quality": { "Sharpness": 83.23492431640625, "Brightness": 61.408443450927734 }, "Landmarks": [ { "Y": 0.4632347822189331, "X": 0.049406956881284714, "Type": "eyeLeft" }, { "Y": 0.46388113498687744, "X": 0.08722897619009018, "Type": "eyeRight" }, { "Y": 0.5020678639411926, "X": 0.0758260041475296, "Type": "nose" }, { "Y": 0.544157862663269, "X": 0.054029736667871475, "Type": "mouthLeft" }, { "Y": 0.5463630557060242, "X": 0.08464983850717545, "Type": "mouthRight" } ] }, "Name": "Celeb D", "Urls": [ "www.imdb.com/name/ddddddddd" ], "Id": "4444444" } ] }

如需詳細資訊,請參閱 Amazon Rekognition 開發人員指南中的識別映像中的名人

Java
Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.core.SdkBytes; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; import software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesRequest; import software.amazon.awssdk.services.rekognition.model.RecognizeCelebritiesResponse; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import software.amazon.awssdk.services.rekognition.model.Image; import software.amazon.awssdk.services.rekognition.model.Celebrity; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class RecognizeCelebrities { public static void main(String[] args) { final String usage = """ Usage: <sourceImage> Where: sourceImage - The path to the image (for example, C:\\AWS\\pic1.png).\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String sourceImage = args[0]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); System.out.println("Locating celebrities in " + sourceImage); recognizeAllCelebrities(rekClient, sourceImage); rekClient.close(); } public static void recognizeAllCelebrities(RekognitionClient rekClient, String sourceImage) { try { InputStream sourceStream = new FileInputStream(sourceImage); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); Image souImage = Image.builder() .bytes(sourceBytes) .build(); RecognizeCelebritiesRequest request = RecognizeCelebritiesRequest.builder() .image(souImage) .build(); RecognizeCelebritiesResponse result = rekClient.recognizeCelebrities(request); List<Celebrity> celebs = result.celebrityFaces(); System.out.println(celebs.size() + " celebrity(s) were recognized.\n"); for (Celebrity celebrity : celebs) { System.out.println("Celebrity recognized: " + celebrity.name()); System.out.println("Celebrity ID: " + celebrity.id()); System.out.println("Further information (if available):"); for (String url : celebrity.urls()) { System.out.println(url); } System.out.println(); } System.out.println(result.unrecognizedFaces().size() + " face(s) were unrecognized."); } catch (RekognitionException | FileNotFoundException e) { System.out.println(e.getMessage()); System.exit(1); } } }
Kotlin
SDK for Kotlin
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

suspend fun recognizeAllCelebrities(sourceImage: String?) { val souImage = Image { bytes = (File(sourceImage).readBytes()) } val request = RecognizeCelebritiesRequest { image = souImage } RekognitionClient { region = "us-east-1" }.use { rekClient -> val response = rekClient.recognizeCelebrities(request) response.celebrityFaces?.forEach { celebrity -> println("Celebrity recognized: ${celebrity.name}") println("Celebrity ID:${celebrity.id}") println("Further information (if available):") celebrity.urls?.forEach { url -> println(url) } } println("${response.unrecognizedFaces?.size} face(s) were unrecognized.") } }
Python
SDK for Python (Boto3)
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def recognize_celebrities(self): """ Detects celebrities in the image. :return: A tuple. The first element is the list of celebrities found in the image. The second element is the list of faces that were detected but did not match any known celebrities. """ try: response = self.rekognition_client.recognize_celebrities(Image=self.image) celebrities = [ RekognitionCelebrity(celeb) for celeb in response["CelebrityFaces"] ] other_faces = [ RekognitionFace(face) for face in response["UnrecognizedFaces"] ] logger.info( "Found %s celebrities and %s other faces in %s.", len(celebrities), len(other_faces), self.image_name, ) except ClientError: logger.exception("Couldn't detect celebrities in %s.", self.image_name) raise else: return celebrities, other_faces
  • 如需 API 詳細資訊,請參閱 RecognizeCelebrities AWS SDK for Python (Boto3) Word 參考中的 API