

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

# 人物の動線の検出
<a name="persons"></a>

**注記**  
*サポート終了のお知らせ:* 2025 年 10 月 31 日をもって、AWS は Amazon Rekognition の人物の動線の検出機能のサポートを終了いたします。2025 年 10 月 31 日のサポート終了後、Rekognition の人物の動線の検出機能はご利用いただけなくなります。詳細については、こちらの[ブログ記事](https://aws.amazon.com/blogs/machine-learning/transitioning-from-amazon-rekognition-people-pathing-exploring-other-alternatives/)をご覧ください。

Amazon Rekognition Video は、ビデオ内の人物を動線を追跡し、以下のような情報を提供できます。
+ 動線の追跡時点におけるビデオフレーム内の人物の位置。
+ 検出時の顔のランドマーク (左目の位置など)。

Amazon Rekognition Video による保存済みビデオ内の人物の動線の検出は非同期オペレーションです。ビデオ内の人の追跡を開始するには、[StartPersonTracking](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StartPersonTracking.html) を呼び出します。Amazon Rekognition Video は、ビデオ分析の完了ステータスを Amazon Simple Notification Service トピックに発行します。ビデオ分析が成功したら、[GetPersonTracking](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_GetPersonTracking.html) を呼び出してビデオ分析の結果を取得します。Amazon Rekognition Video API オペレーションの詳細については、[Amazon Rekognition Video オペレーションを呼び出す](api-video.md) を参照してください。

以下の手順では、Amazon S3 バケットに保存されたビデオを通じた人物の動線の追跡方法を示します。この例では、Amazon Simple Queue Service のキューを使用してビデオ分析リクエストの完了ステータスを取得する [Java または Python を使用した、Amazon S3 バケットに保存されたビデオの分析 (SDK)](video-analyzing-with-sqs.md) のコードを拡張します。

**Amazon S3 バケットに保存されたビデオ内のテキストを検出するには (SDK)**

1. 「[Java または Python を使用した、Amazon S3 バケットに保存されたビデオの分析 (SDK)](video-analyzing-with-sqs.md)」を実行します。

1. ステップ 1 で作成したクラス `VideoDetect` に次のコードを追加します。

------
#### [ Java ]

   ```
          //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
          //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)
   
           //Persons========================================================================
           private static void StartPersonDetection(String bucket, String video) throws Exception{
               
               
               NotificationChannel channel= new NotificationChannel()
                       .withSNSTopicArn(snsTopicArn)
                       .withRoleArn(roleArn);
               
            StartPersonTrackingRequest req = new StartPersonTrackingRequest()
                    .withVideo(new Video()
                            .withS3Object(new S3Object()
                                .withBucket(bucket)
                                .withName(video)))
                    .withNotificationChannel(channel);
                                   
                
               
            StartPersonTrackingResult startPersonDetectionResult = rek.startPersonTracking(req);
            startJobId=startPersonDetectionResult.getJobId();
               
           } 
           
           private static void GetPersonDetectionResults() throws Exception{
               int maxResults=10;
               String paginationToken=null;
               GetPersonTrackingResult personTrackingResult=null;
               
               do{
                   if (personTrackingResult !=null){
                       paginationToken = personTrackingResult.getNextToken();
                   }
                   
                   personTrackingResult = rek.getPersonTracking(new GetPersonTrackingRequest()
                        .withJobId(startJobId)
                        .withNextToken(paginationToken)
                        .withSortBy(PersonTrackingSortBy.TIMESTAMP)
                        .withMaxResults(maxResults));
             
                   VideoMetadata videoMetaData=personTrackingResult.getVideoMetadata();
                       
                   System.out.println("Format: " + videoMetaData.getFormat());
                   System.out.println("Codec: " + videoMetaData.getCodec());
                   System.out.println("Duration: " + videoMetaData.getDurationMillis());
                   System.out.println("FrameRate: " + videoMetaData.getFrameRate());
                       
                       
                   //Show persons, confidence and detection times
                   List<PersonDetection> detectedPersons= personTrackingResult.getPersons();
                
                   for (PersonDetection detectedPerson: detectedPersons) { 
                       
                      long seconds=detectedPerson.getTimestamp()/1000;
                      System.out.print("Sec: " + Long.toString(seconds) + " ");
                      System.out.println("Person Identifier: "  + detectedPerson.getPerson().getIndex());
                         System.out.println();             
                   }
               }  while (personTrackingResult !=null && personTrackingResult.getNextToken() != null);
               
           }
   ```

   関数 `main` で、以下の行を置き換えます。

   ```
           StartLabelDetection(amzn-s3-demo-bucket, video);
   
           if (GetSQSMessageSuccess()==true)
           	GetLabelDetectionResults();
   ```

   を:

   ```
           StartPersonDetection(amzn-s3-demo-bucket, video);
   
           if (GetSQSMessageSuccess()==true)
           	GetPersonDetectionResults();
   ```

------
#### [ Java V2 ]

   このコードは、AWS ドキュメント SDK の例 GitHub リポジトリから引用されたものです。詳しい事例は [[こちら](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/rekognition/src/main/java/com/example/rekognition/VideoPersonDetection.java)] です。

   ```
   import software.amazon.awssdk.regions.Region;
   import software.amazon.awssdk.services.rekognition.RekognitionClient;
   import software.amazon.awssdk.services.rekognition.model.S3Object;
   import software.amazon.awssdk.services.rekognition.model.NotificationChannel;
   import software.amazon.awssdk.services.rekognition.model.StartPersonTrackingRequest;
   import software.amazon.awssdk.services.rekognition.model.Video;
   import software.amazon.awssdk.services.rekognition.model.StartPersonTrackingResponse;
   import software.amazon.awssdk.services.rekognition.model.RekognitionException;
   import software.amazon.awssdk.services.rekognition.model.GetPersonTrackingResponse;
   import software.amazon.awssdk.services.rekognition.model.GetPersonTrackingRequest;
   import software.amazon.awssdk.services.rekognition.model.VideoMetadata;
   import software.amazon.awssdk.services.rekognition.model.PersonDetection;
   import java.util.List;
   
   /**
    * 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 VideoPersonDetection {
       private static String startJobId = "";
   
       public static void main(String[] args) {
   
           final String usage = """
   
                   Usage:    <bucket> <video> <topicArn> <roleArn>
   
                   Where:
                      bucket - The name of the bucket in which the video is located (for example, (for example, myBucket).\s
                      video - The name of video (for example, people.mp4).\s
                      topicArn - The ARN of the Amazon Simple Notification Service (Amazon SNS) topic.\s
                      roleArn - The ARN of the AWS Identity and Access Management (IAM) role to use.\s
                   """;
   
           if (args.length != 4) {
               System.out.println(usage);
               System.exit(1);
           }
   
           String bucket = args[0];
           String video = args[1];
           String topicArn = args[2];
           String roleArn = args[3];
           Region region = Region.US_EAST_1;
           RekognitionClient rekClient = RekognitionClient.builder()
                   .region(region)
                   .build();
   
           NotificationChannel channel = NotificationChannel.builder()
                   .snsTopicArn(topicArn)
                   .roleArn(roleArn)
                   .build();
   
           startPersonLabels(rekClient, channel, bucket, video);
           getPersonDetectionResults(rekClient);
           System.out.println("This example is done!");
           rekClient.close();
       }
   
       public static void startPersonLabels(RekognitionClient rekClient,
               NotificationChannel channel,
               String bucket,
               String video) {
           try {
               S3Object s3Obj = S3Object.builder()
                       .bucket(bucket)
                       .name(video)
                       .build();
   
               Video vidOb = Video.builder()
                       .s3Object(s3Obj)
                       .build();
   
               StartPersonTrackingRequest personTrackingRequest = StartPersonTrackingRequest.builder()
                       .jobTag("DetectingLabels")
                       .video(vidOb)
                       .notificationChannel(channel)
                       .build();
   
               StartPersonTrackingResponse labelDetectionResponse = rekClient.startPersonTracking(personTrackingRequest);
               startJobId = labelDetectionResponse.jobId();
   
           } catch (RekognitionException e) {
               System.out.println(e.getMessage());
               System.exit(1);
           }
       }
   
       public static void getPersonDetectionResults(RekognitionClient rekClient) {
           try {
               String paginationToken = null;
               GetPersonTrackingResponse personTrackingResult = null;
               boolean finished = false;
               String status;
               int yy = 0;
   
               do {
                   if (personTrackingResult != null)
                       paginationToken = personTrackingResult.nextToken();
   
                   GetPersonTrackingRequest recognitionRequest = GetPersonTrackingRequest.builder()
                           .jobId(startJobId)
                           .nextToken(paginationToken)
                           .maxResults(10)
                           .build();
   
                   // Wait until the job succeeds
                   while (!finished) {
   
                       personTrackingResult = rekClient.getPersonTracking(recognitionRequest);
                       status = personTrackingResult.jobStatusAsString();
   
                       if (status.compareTo("SUCCEEDED") == 0)
                           finished = true;
                       else {
                           System.out.println(yy + " status is: " + status);
                           Thread.sleep(1000);
                       }
                       yy++;
                   }
   
                   finished = false;
   
                   // Proceed when the job is done - otherwise VideoMetadata is null.
                   VideoMetadata videoMetaData = personTrackingResult.videoMetadata();
   
                   System.out.println("Format: " + videoMetaData.format());
                   System.out.println("Codec: " + videoMetaData.codec());
                   System.out.println("Duration: " + videoMetaData.durationMillis());
                   System.out.println("FrameRate: " + videoMetaData.frameRate());
                   System.out.println("Job");
   
                   List<PersonDetection> detectedPersons = personTrackingResult.persons();
                   for (PersonDetection detectedPerson : detectedPersons) {
                       long seconds = detectedPerson.timestamp() / 1000;
                       System.out.print("Sec: " + seconds + " ");
                       System.out.println("Person Identifier: " + detectedPerson.person().index());
                       System.out.println();
                   }
   
               } while (personTrackingResult != null && personTrackingResult.nextToken() != null);
   
           } catch (RekognitionException | InterruptedException e) {
               System.out.println(e.getMessage());
               System.exit(1);
           }
       }
   }
   ```

------
#### [ Python ]

   ```
   #Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   #PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)
   
       # ============== People pathing ===============  
       def StartPersonPathing(self):
           response=self.rek.start_person_tracking(Video={'S3Object': {'Bucket': self.bucket, 'Name': self.video}},
               NotificationChannel={'RoleArn': self.roleArn, 'SNSTopicArn': self.snsTopicArn})
   
           self.startJobId=response['JobId']
           print('Start Job Id: ' + self.startJobId)
       
       def GetPersonPathingResults(self):
           maxResults = 10
           paginationToken = ''
           finished = False
   
           while finished == False:
               response = self.rek.get_person_tracking(JobId=self.startJobId,
                                               MaxResults=maxResults,
                                               NextToken=paginationToken)
   
               print('Codec: ' + response['VideoMetadata']['Codec'])
               print('Duration: ' + str(response['VideoMetadata']['DurationMillis']))
               print('Format: ' + response['VideoMetadata']['Format'])
               print('Frame rate: ' + str(response['VideoMetadata']['FrameRate']))
               print()
   
               for personDetection in response['Persons']:
                   print('Index: ' + str(personDetection['Person']['Index']))
                   print('Timestamp: ' + str(personDetection['Timestamp']))
                   print()
   
               if 'NextToken' in response:
                   paginationToken = response['NextToken']
               else:
                   finished = True
   ```

   関数 `main` で、以下の行を置き換えます。

   ```
       analyzer.StartLabelDetection()
       if analyzer.GetSQSMessageSuccess()==True:
           analyzer.GetLabelDetectionResults()
   ```

   を:

   ```
       analyzer.StartPersonPathing()
       if analyzer.GetSQSMessageSuccess()==True:
           analyzer.GetPersonPathingResults()
   ```

------
#### [ CLI ]

   以下の AWS CLI コマンドを実行して、ビデオ内の人の追跡を開始します。

   ```
   aws rekognition start-person-tracking --video "{"S3Object":{"Bucket":"amzn-s3-demo-bucket","Name":"video-name"}}" \ 
   --notification-channel "{"SNSTopicArn":"topic-ARN","RoleArn":"role-ARN"}" \
   --region region-name --profile profile-name
   ```

   以下の値を更新します。
   + `amzn-s3-demo-bucket` と `video-name` を、ステップ 2 で指定した Amazon S3 バケット名とファイル名に変更します。
   + `region-name` を、使用している AWS リージョンに変更します。
   + Rekognition セッションを作成する行の `profile-name` の値を、自分のデベロッパープロファイル名に置き換えます。
   + `topic-ARN` を、[Amazon Rekognition Video の設定](api-video-roles.md) のステップ 3 で作成した Amazon SNS トピックの ARN に変更します。
   + `role-ARN` を、[Amazon Rekognition Video の設定](api-video-roles.md) のステップ 7 で作成した IAM サービスロールの ARN に変更します。

   Windows デバイスで CLI にアクセスする場合は、パーサーエラーの発生に対処するため、一重引用符の代わりに二重引用符を使用し、内側の二重引用符をバックスラッシュ (\$1) でエスケープします。次の例を参照してください。

   ```
   aws rekognition start-person-tracking --video "{\"S3Object\":{\"Bucket\":\"amzn-s3-demo-bucket\",\"Name\":\"video-name\"}}" 
   --notification-channel "{\"SNSTopicArn\":\"topic-ARN\",\"RoleArn\":\"role-ARN\"}" \
   --region region-name --profile profile-name
   ```

   上記のコード例を実行した後、返された `jobID` をコピーして、以下の `GetPersonTracking` コマンドに渡すと、`job-id-number` が以前に受け取った `jobID` に置き換わっている結果が得られます。

   ```
   aws rekognition get-person-tracking --job-id job-id-number                                
   ```

------
**注記**  
[Java または Python を使用した、Amazon S3 バケットに保存されたビデオの分析 (SDK)](video-analyzing-with-sqs.md) 以外のビデオ例をすでに実行している場合、置き換えるコードは異なる可能性があります。

1. コードを実行します。追跡された人物の一意の ID が、人物の動線が追跡された時間 (秒単位) とともに表示されます。

## GetPersonTracking オペレーションレスポンス
<a name="getresultspersons-operation-response"></a>

`GetPersonTracking` は、ビデオ内で検出された人の詳細とその動線が追跡されたタイミングに関する情報を含む [PersonDetection](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_PersonDetection.html) オブジェクトの配列 `Persons` を返します。

`Persons` 入力パラメータを使用して `SortBy` をソートすることができます。ビデオ内で人物の動線が追跡された時間によって要素をソートするには、`TIMESTAMP` を指定します。ビデオ内で追跡された人物によってソートするには、`INDEX` を指定します。人物の各結果セット内で、要素は動線の追跡の正確性の信頼度によって降順にソートされます。デフォルトでは、`Persons` は `TIMESTAMP` でソートされて返されます。`GetPersonDetection` の JSON レスポンスの例を次に示します。結果は、ビデオの開始以降にビデオ内で人物の動線が追跡された時間 (ミリ秒単位) によってソートされます。レスポンスで、以下の点に注意してください。
+ **個人情報** – `PersonDetection` 配列要素には、検出された人物に関する情報が含まれています。たとえば、人物が検出された時間 (`Timestamp`)、検出時のビデオフレームでの人物の位置 (`BoundingBox`)、人物の正しい検出に関する Amazon Rekognition Video の信頼度 (`Confidence`) などです。

  人物の動線が追跡されたタイムスタンプごとに顔の特徴は返されません。さらに、状況によっては、追跡された人物の体が見えない場合があり、その場合は顔の位置のみが返されます。
+ **ページング情報** – 例は 1 ページの人物検出情報を示しています。人物要素を返す数は、`MaxResults` の `GetPersonTracking` 入力パラメータで指定できます。結果が `MaxResults` を超える場合、`GetPersonTracking` は次のページの結果を取得するためのトークン (`NextToken`) を返します。詳細については、「[Amazon Rekognition Video の分析結果を取得する](api-video.md#api-video-get)」を参照してください。
+ **インデックス** –ビデオ全体で人物を識別するための一意の識別子。
+ **ビデオの情報** – レスポンスには、`VideoMetadata` から返される情報のページごとにビデオ形式に関する情報 (`GetPersonDetection`) が含まれます。

```
{
    "JobStatus": "SUCCEEDED",
    "NextToken": "AcDymG0fSSoaI6+BBYpka5wVlqttysSPP8VvWcujMDluj1QpFo/vf+mrMoqBGk8eUEiFlllR6g==",
    "Persons": [
        {
            "Person": {
                "BoundingBox": {
                    "Height": 0.8787037134170532,
                    "Left": 0.00572916679084301,
                    "Top": 0.12129629403352737,
                    "Width": 0.21666666865348816
                },
                "Face": {
                    "BoundingBox": {
                        "Height": 0.20000000298023224,
                        "Left": 0.029999999329447746,
                        "Top": 0.2199999988079071,
                        "Width": 0.11249999701976776
                    },
                    "Confidence": 99.85971069335938,
                    "Landmarks": [
                        {
                            "Type": "eyeLeft",
                            "X": 0.06842322647571564,
                            "Y": 0.3010137975215912
                        },
                        {
                            "Type": "eyeRight",
                            "X": 0.10543643683195114,
                            "Y": 0.29697132110595703
                        },
                        {
                            "Type": "nose",
                            "X": 0.09569807350635529,
                            "Y": 0.33701086044311523
                        },
                        {
                            "Type": "mouthLeft",
                            "X": 0.0732642263174057,
                            "Y": 0.3757539987564087
                        },
                        {
                            "Type": "mouthRight",
                            "X": 0.10589495301246643,
                            "Y": 0.3722417950630188
                        }
                    ],
                    "Pose": {
                        "Pitch": -0.5589138865470886,
                        "Roll": -5.1093974113464355,
                        "Yaw": 18.69594955444336
                    },
                    "Quality": {
                        "Brightness": 43.052337646484375,
                        "Sharpness": 99.68138885498047
                    }
                },
                "Index": 0
            },
            "Timestamp": 0
        },
        {
            "Person": {
                "BoundingBox": {
                    "Height": 0.9074074029922485,
                    "Left": 0.24791666865348816,
                    "Top": 0.09259258955717087,
                    "Width": 0.375
                },
                "Face": {
                    "BoundingBox": {
                        "Height": 0.23000000417232513,
                        "Left": 0.42500001192092896,
                        "Top": 0.16333332657814026,
                        "Width": 0.12937499582767487
                    },
                    "Confidence": 99.97504425048828,
                    "Landmarks": [
                        {
                            "Type": "eyeLeft",
                            "X": 0.46415066719055176,
                            "Y": 0.2572723925113678
                        },
                        {
                            "Type": "eyeRight",
                            "X": 0.5068183541297913,
                            "Y": 0.23705792427062988
                        },
                        {
                            "Type": "nose",
                            "X": 0.49765899777412415,
                            "Y": 0.28383663296699524
                        },
                        {
                            "Type": "mouthLeft",
                            "X": 0.487221896648407,
                            "Y": 0.3452930748462677
                        },
                        {
                            "Type": "mouthRight",
                            "X": 0.5142884850502014,
                            "Y": 0.33167609572410583
                        }
                    ],
                    "Pose": {
                        "Pitch": 15.966927528381348,
                        "Roll": -15.547388076782227,
                        "Yaw": 11.34195613861084
                    },
                    "Quality": {
                        "Brightness": 44.80223083496094,
                        "Sharpness": 99.95819854736328
                    }
                },
                "Index": 1
            },
            "Timestamp": 0
        }.....

    ],
    "VideoMetadata": {
        "Codec": "h264",
        "DurationMillis": 67301,
        "FileExtension": "mp4",
        "Format": "QuickTime / MOV",
        "FrameHeight": 1080,
        "FrameRate": 29.970029830932617,
        "FrameWidth": 1920
    }
}
```