

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 비디오에서 레이블 감지
<a name="labels-detecting-labels-video"></a>

Amazon Rekognition Video는 비디오에서 레이블(객체 및 개념)과 레이블이 감지된 시간을 감지할 수 있습니다. SDK 코드에 대한 예는 [Java 또는 Python으로 Amazon S3 버킷에 저장된 비디오 분석(SDK)](video-analyzing-with-sqs.md) 단원을 참조하십시오. AWS CLI 예제는 단원을 참조하십시오[를 사용하여 비디오 분석 AWS Command Line Interface](video-cli-commands.md).

Amazon Rekognition Video의 레이블 감지는 비동기 작업입니다. 비디오에서 레이블 감지를 시작하려면 [StartLabelDetection을](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StartlabelDetection.html) 직접 호출하세요.

Amazon Rekognition Video는 비디오 분석의 완료 상태를 Amazon Simple Notification Service 주제에 게시합니다. 비디오 분석이 성공적으로 완료되면 [GetLabelDetection](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_GetLabelDetection.html)을 직접 호출하여 감지된 레이블을 가져옵니다. 비디오 분석 API 작업을 호출하는 방법에 대한 자세한 내용은 [Amazon Rekognition Video 작업 직접 호출](api-video.md) 단원을 참조하십시오.

## StartLabelDetection 요청
<a name="getlabeldetection-operation-request"></a>

다음 예제는 `StartLabelDetection` 작업의 요청입니다. Amazon S3 버킷에 저장된 비디오를 사용하여 `StartLabelDetection` 작업을 제공합니다. 이 예제 요청 JSON에서는 Amazon S3 버킷 및 비디오 이름이, `MinConfidence`, `Features`, `Settings`, `NotificationChannel`와 함께 지정됩니다.

`MinConfidence`는 Amazon Rekognition Video가 감지된 레이블 또는 인스턴스 경계 상자(감지된 경우)를 응답에 반환하기 위해 가져야 할 최소 정확성 신뢰도입니다.

`Features`를 사용하면 GENERAL\_LABELS가 응답의 일부로 반환되도록 지정할 수 있습니다.

`Settings`를 사용하면 GENERAL\_LABELS에 대해 반환된 항목을 필터링할 수 있습니다. 레이블의 경우 포함 및 제외 필터를 사용할 수 있습니다. 특정 레이블, 개별 레이블 또는 레이블 카테고리별로 필터링할 수도 있습니다.
+ `LabelInclusionFilters` - 응답에 포함할 레이블을 지정하는 데 사용됩니다.
+ `LabelExclusionFilters` - 응답에서 제외할 레이블을 지정하는 데 사용됩니다.
+ `LabelCategoryInclusionFilters` - 응답에 포함할 레이블 카테고리를 지정하는 데 사용됩니다.
+ `LabelCategoryExclusionFilters` - 응답에서 제외할 레이블 카테고리를 지정하는 데 사용됩니다.

필요에 따라 포함 필터와 제외 필터를 조합하여 일부 레이블 또는 카테고리를 제외하는 동시에 다른 레이블이나 카테고리를 포함할 수도 있습니다.

`NotificationChannel`은 Amazon Rekognition Video에서 레이블 감지 작업의 완료 상태를 게시하려는 Amazon SNS 주제의 ARN입니다. `AmazonRekognitionServiceRole` 권한 정책을 사용하는 경우 Amazon SNS 주제의 이름은 반드시 Rekognition으로 시작해야 합니다.

다음은 필터를 포함한 JSON 형식의 샘플 `StartLabelDetection` 요청입니다.

```
{
    "ClientRequestToken": "5a6e690e-c750-460a-9d59-c992e0ec8638",
    "JobTag": "5a6e690e-c750-460a-9d59-c992e0ec8638",
    "Video": {
        "S3Object": {
            "Bucket": "bucket",
            "Name": "video.mp4" 
         } 
     }, 
     "Features": ["GENERAL_LABELS"],
     "MinConfidence": 75,
     "Settings": {
         "GeneralLabels": {
             "LabelInclusionFilters": ["Cat", "Dog"],
             "LabelExclusionFilters": ["Tiger"],
             "LabelCategoryInclusionFilters": ["Animals and Pets"],
             "LabelCategoryExclusionFilters": ["Popular Landmark"] 
         }
     },
     "NotificationChannel": {
         "RoleArn": "arn:aws:iam::012345678910:role/SNSAccessRole",
         "SNSTopicArn": "arn:aws:sns:us-east-1:012345678910:notification-topic",
     }
}
```

## GetLabelDetection 작업 응답
<a name="getlabeldetection-operation-response"></a>

`GetLabelDetection`은 비디오에서 감지된 레이블에 대한 정보가 포함된 배열(`Labels`)을 반환합니다. 배열을 시간별로 정렬하거나 `SortBy` 파라미터 지정할 때 감지된 레이블별로 정렬할 수 있습니다. `AggregateBy` 파라미터를 사용하여 응답 항목을 집계하는 방법을 선택할 수도 있습니다.

다음은 `GetLabelDetection`의 JSON 응답 예입니다. 응답에서 다음에 유의하십시오.
+ **Sort order** - 시간별로 정렬된 반환된 레이블의 배열. 레이블별로 정렬하려면, `GetLabelDetection`에서 `SortBy` 입력 파라미터에 `NAME`를 지정하십시오. 레이블이 비디오에 여러 번 나오면 ([LabelDetection](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_LabelDetection.html))요소의 여러 인스턴스가 있습니다. 기본 정렬 순서는 `TIMESTAMP`이고 보조 정렬 순서는 `NAME`입니다.
+ **레이블 정보** - `LabelDetection` 배열 요소에는 ([Label](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_Label.html))객체가 포함되어 있는데 이는 감지된의 레이블 이름과 Amazon Rekognition이 그에 대해 가지는 정확성 신뢰도를 포함합니다. 또한 `Label` 객체에는 레이블의 계층적 분류와 일반 레이블의 경우에는 경계 상자 정보가 포함됩니다. `Timestamp`는 비디오 시작부터 레이블이 감지될 때까지 지난 밀리초 단위의 시간입니다.

  레이블과 관련된 모든 카테고리 또는 별칭에 관한 정보도 반환됩니다. 비디오 `SEGMENTS`별로 집계된 결과의 경우 세그먼트의 시작 시간, 종료 시간, 지속 시간을 각각 정의하는 `StartTimestampMillis`, `EndTimestampMillis`, `DurationMillis` 구조가 반환됩니다.
+ **Aggregation** - 결과가 반환될 때 집계되는 방식을 지정합니다. 기본값은 `TIMESTAMPS` 기준 집계입니다. 일정 기간 동안의 결과를 집계하는 `SEGMENTS` 집계 기준을 선택할 수도 있습니다. `SEGMENTS` 기준으로 집계할 경우 경계 상자가 있는 감지된 인스턴스에 대한 정보는 반환되지 않습니다. 세그먼트 내에서 감지된 레이블만 반환됩니다.
+ **페이징 정보** - 이 예제는 레이블 감지 정보의 페이지 하나를 보여줍니다. `GetLabelDetection`의 `MaxResults` 입력 파라미터에서 반환하는 `LabelDetection` 객체의 개수를 지정할 수 있습니다. `MaxResults` 보다 많은 결과가 존재할 경우 `GetLabelDetection`은 결과의 다음 페이지를 가져올 때 사용되는 토큰(`NextToken`)을 반환합니다. 자세한 내용은 [Amazon Rekognition Video 분석 결과 가져오기](api-video.md#api-video-get) 단원을 참조하십시오.
+ **비디오 정보** - 응답에는`GetLabelDetection`에서 반환된 정보의 각 페이지에 있는 비디오 형식(`VideoMetadata`)에 관한 정보가 포함되어 있습니다.

다음은 타임스탬프 기준으로 집계된 JSON 형식의 GetLabelDetection 응답 샘플입니다.

```
{  
    "JobStatus": "SUCCEEDED",
    "LabelModelVersion": "3.0",
    "Labels": [
        {
            "Timestamp": 1000,
            "Label": {
                "Name": "Car",
                "Categories": [
                  {
                    "Name": "Vehicles and Automotive"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Automobile"
                  }
                ],
                "Parents": [
                  {
                    "Name": "Vehicle"
                  }
                ],
                "Confidence": 99.9364013671875, // Classification confidence
                "Instances": [    
                    {        
                        "BoundingBox": {            
                            "Width": 0.26779675483703613,
                            "Height": 0.8562285900115967,
                            "Left": 0.3604024350643158,
                            "Top": 0.09245597571134567         
                        },  
                        "Confidence": 99.9364013671875 // Detection confidence     
                    }    
                ]
            }
        },
        {
            "Timestamp": 1000,
            "Label": {
                "Name": "Cup",
                "Categories": [
                  {
                    "Name": "Kitchen and Dining"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Mug"
                  }
                ],
                "Parents": [],
                "Confidence": 99.9364013671875, // Classification confidence
                "Instances": [    
                    {        
                        "BoundingBox": {            
                            "Width": 0.26779675483703613,
                            "Height": 0.8562285900115967,
                            "Left": 0.3604024350643158,
                            "Top": 0.09245597571134567         
                        },  
                        "Confidence": 99.9364013671875 // Detection confidence     
                    }    
                ]
            }
        },
        {
            "Timestamp": 2000,
            "Label": {
                "Name": "Kangaroo",
                "Categories": [
                  {
                    "Name": "Animals and Pets"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Wallaby"
                  }
                ],
                "Parents": [
                  {
                    "Name": "Mammal"
                  }
                ],
                "Confidence": 99.9364013671875,  
                "Instances": [    
                    {        
                        "BoundingBox": {            
                            "Width": 0.26779675483703613,
                            "Height": 0.8562285900115967,
                            "Left": 0.3604024350643158,
                            "Top": 0.09245597571134567,
                        },  
                        "Confidence": 99.9364013671875    
                    }    
                ]
            }
        },
        {
            "Timestamp": 4000,
            "Label": {
                "Name": "Bicycle",
                "Categories": [
                  {
                    "Name": "Hobbies and Interests"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Bike"
                  }
                ],
                "Parents": [
                  {
                    "Name": "Vehicle"
                  }
                ],
                "Confidence": 99.9364013671875,
                "Instances": [    
                    {        
                        "BoundingBox": {            
                            "Width": 0.26779675483703613,
                            "Height": 0.8562285900115967,
                            "Left": 0.3604024350643158,
                            "Top": 0.09245597571134567         
                        },  
                        "Confidence": 99.9364013671875     
                    }    
                ]
            }
        }
    ],
    "VideoMetadata": {
        "ColorRange": "FULL",
        "DurationMillis": 5000,
        "Format": "MP4",
        "FrameWidth": 1280,
        "FrameHeight": 720,
        "FrameRate": 24
    }
}
```

다음은 세그먼트 기준으로 집계된 JSON 형식의 GetLabelDetection 응답 샘플입니다.

```
{  
    "JobStatus": "SUCCEEDED",
    "LabelModelVersion": "3.0",
    "Labels": [ 
        {
            "StartTimestampMillis": 225,
            "EndTimestampMillis": 3578,
            "DurationMillis": 3353,
            "Label": {
                "Name": "Car",
                "Categories": [
                  {
                    "Name": "Vehicles and Automotive"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Automobile"
                  }
                ],
                "Parents": [
                  {
                    "Name": "Vehicle"
                  }
                ],
                "Confidence": 99.9364013671875 // Maximum confidence score for Segment mode
            }
        },
        {
            "StartTimestampMillis": 7578,
            "EndTimestampMillis": 12371,
            "DurationMillis": 4793,
            "Label": {
                "Name": "Kangaroo",
                "Categories": [
                  {
                    "Name": "Animals and Pets"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Wallaby"
                  }
                ],
                "Parents": [
                  {
                    "Name": "Mammal"
                  }
                ],
                "Confidence": 99.9364013671875
            }
        },
        {
            "StartTimestampMillis": 22225,
            "EndTimestampMillis": 22578,
            "DurationMillis": 2353,
            "Label": {
                "Name": "Bicycle",
                "Categories": [
                  {
                    "Name": "Hobbies and Interests"
                  }
                ],
                "Aliases": [
                  {
                    "Name": "Bike"
                  }
                ],
                "Parents": [
                  {
                    "Name": "Vehicle"
                  }
                ],
                "Confidence": 99.9364013671875
            }
        }
    ],
    "VideoMetadata": {
        "ColorRange": "FULL",
        "DurationMillis": 5000,
        "Format": "MP4",
        "FrameWidth": 1280,
        "FrameHeight": 720,
        "FrameRate": 24
    }
}
```

## GetLabelDetection 응답의 변환
<a name="getlabeldetection-transform-response"></a>

GetLabelDetection API를 사용하여 결과를 가져오는 경우 기본 레이블과 별칭이 모두 동일한 목록에 포함된 이전 API 응답 구조를 모방하는 응답 구조가 필요할 수 있습니다.

이전 섹션에 있는 예제 JSON 응답에는 GetLabelDetection에서 오는 API 응답의 현재 형식이 표시됩니다.

다음 예제는 GetLabelDetection API의 이전 응답을 보여줍니다.

```
{
    "Labels": [
        {
            "Timestamp": 0,
            "Label": {
                "Instances": [],
                "Confidence": 60.51791763305664,
                "Parents": [],
                "Name": "Leaf"
            }
        },
        {
            "Timestamp": 0,
            "Label": {
                "Instances": [],
                "Confidence": 99.53411102294922,
                "Parents": [],
                "Name": "Human"
            }
        },
        {
            "Timestamp": 0,
            "Label": {
                "Instances": [
                    {
                        "BoundingBox": {
                            "Width": 0.11109819263219833,
                            "Top": 0.08098889887332916,
                            "Left": 0.8881205320358276,
                            "Height": 0.9073750972747803
                        },
                        "Confidence": 99.5831298828125
                    },
                    {
                        "BoundingBox": {
                            "Width": 0.1268676072359085,
                            "Top": 0.14018426835536957,
                            "Left": 0.0003282368124928324,
                            "Height": 0.7993982434272766
                        },
                        "Confidence": 99.46029663085938
                    }
                ],
                "Confidence": 99.63411102294922,
                "Parents": [],
                "Name": "Person"
            }
        },
        .
        .   
        .

        {
            "Timestamp": 166,
            "Label": {
                "Instances": [],
                "Confidence": 73.6471176147461,
                "Parents": [
                    {
                        "Name": "Clothing"
                    }
                ],
                "Name": "Sleeve"
            }
        }
        
    ],
    "LabelModelVersion": "2.0",
    "JobStatus": "SUCCEEDED",
    "VideoMetadata": {
        "Format": "QuickTime / MOV",
        "FrameRate": 23.976024627685547,
        "Codec": "h264",
        "DurationMillis": 5005,
        "FrameHeight": 674,
        "FrameWidth": 1280
    }
}
```

필요한 경우 현재 응답을 이전 응답 형식을 따르도록 변환할 수 있습니다. 다음 샘플 코드를 사용하여 최신 API 응답을 이전 API 응답 구조로 변환할 수 있습니다.

```
from copy import deepcopy

VIDEO_LABEL_KEY = "Labels"
LABEL_KEY = "Label"
ALIASES_KEY = "Aliases"
INSTANCE_KEY = "Instances"
NAME_KEY = "Name"

#Latest API response sample for AggregatedBy SEGMENTS
EXAMPLE_SEGMENT_OUTPUT = {
    "Labels": [
        {
            "Timestamp": 0,
            "Label":{
                "Name": "Person",
                "Confidence": 97.530106,
                "Parents": [],
                "Aliases": [
                    {
                        "Name": "Human"
                    },
                ],
                "Categories": [
                    {
                        "Name": "Person Description"
                    }
                ],
            },
            "StartTimestampMillis": 0,
            "EndTimestampMillis": 500666,
            "DurationMillis": 500666
        },
        {
            "Timestamp": 6400,
            "Label": {
                "Name": "Leaf",
                "Confidence": 89.77790069580078,
                "Parents": [
                    {
                        "Name": "Plant"
                    }
                ],
                "Aliases": [],
                "Categories": [
                    {
                        "Name": "Plants and Flowers"
                    }
                ],

            },
            "StartTimestampMillis": 6400,
            "EndTimestampMillis": 8200,
            "DurationMillis": 1800
        },
    ]
}

#Output example after the transformation for AggregatedBy SEGMENTS
EXPECTED_EXPANDED_SEGMENT_OUTPUT = {
    "Labels": [
        {
            "Timestamp": 0,
            "Label":{
                "Name": "Person",
                "Confidence": 97.530106,
                "Parents": [],
                "Aliases": [
                    {
                        "Name": "Human"
                    },
                ],
                "Categories": [
                    {
                        "Name": "Person Description"
                    }
                ],
            },
            "StartTimestampMillis": 0,
            "EndTimestampMillis": 500666,
            "DurationMillis": 500666
        },
        {
            "Timestamp": 6400,
            "Label": {
                "Name": "Leaf",
                "Confidence": 89.77790069580078,
                "Parents": [
                    {
                        "Name": "Plant"
                    }
                ],
                "Aliases": [],
                "Categories": [
                    {
                        "Name": "Plants and Flowers"
                    }
                ],

            },
            "StartTimestampMillis": 6400,
            "EndTimestampMillis": 8200,
            "DurationMillis": 1800
        },
        {
            "Timestamp": 0,
            "Label":{
                "Name": "Human",
                "Confidence": 97.530106,
                "Parents": [],
                "Categories": [
                    {
                        "Name": "Person Description"
                    }
                ],
            },
            "StartTimestampMillis": 0,
            "EndTimestampMillis": 500666,
            "DurationMillis": 500666
        },
    ]
}

#Latest API response sample for AggregatedBy TIMESTAMPS
EXAMPLE_TIMESTAMP_OUTPUT = {
    "Labels": [
        {
            "Timestamp": 0,
            "Label": {
                "Name": "Person",
                "Confidence": 97.530106,
                "Instances": [
                    {
                        "BoundingBox": {
                            "Height": 0.1549897,
                            "Width": 0.07747964,
                            "Top": 0.50858885,
                            "Left": 0.00018205095
                        },
                        "Confidence": 97.530106
                    },
                ],
                "Parents": [],
                "Aliases": [
                    {
                        "Name": "Human"
                    },
                ],
                "Categories": [
                    {
                        "Name": "Person Description"
                    }
                ],
            },
        },
        {
            "Timestamp": 6400,
            "Label": {
                "Name": "Leaf",
                "Confidence": 89.77790069580078,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Plant"
                    }
                ],
                "Aliases": [],
                "Categories": [
                    {
                        "Name": "Plants and Flowers"
                    }
                ],
            },
        },
    ]
}

#Output example after the transformation for AggregatedBy TIMESTAMPS
EXPECTED_EXPANDED_TIMESTAMP_OUTPUT = {
    "Labels": [
        {
            "Timestamp": 0,
            "Label": {
                "Name": "Person",
                "Confidence": 97.530106,
                "Instances": [
                    {
                        "BoundingBox": {
                            "Height": 0.1549897,
                            "Width": 0.07747964,
                            "Top": 0.50858885,
                            "Left": 0.00018205095
                        },
                        "Confidence": 97.530106
                    },
                ],
                "Parents": [],
                "Aliases": [
                    {
                        "Name": "Human"
                    },
                ],
                "Categories": [
                    {
                        "Name": "Person Description"
                    }
                ],
            },
        },
        {
            "Timestamp": 6400,
            "Label": {
                "Name": "Leaf",
                "Confidence": 89.77790069580078,
                "Instances": [],
                "Parents": [
                    {
                        "Name": "Plant"
                    }
                ],
                "Aliases": [],
                "Categories": [
                    {
                        "Name": "Plants and Flowers"
                    }
                ],
            },
        },
        {
            "Timestamp": 0,
            "Label": {
                "Name": "Human",
                "Confidence": 97.530106,
                "Parents": [],
                "Categories": [
                    {
                        "Name": "Person Description"
                    }
                ],
            },
        },
    ]
}

def expand_aliases(inferenceOutputsWithAliases):

    if VIDEO_LABEL_KEY in inferenceOutputsWithAliases:
        expandInferenceOutputs = []
        for segmentLabelDict in inferenceOutputsWithAliases[VIDEO_LABEL_KEY]:
            primaryLabelDict = segmentLabelDict[LABEL_KEY]
            if ALIASES_KEY in primaryLabelDict:
                for alias in primaryLabelDict[ALIASES_KEY]:
                    aliasLabelDict = deepcopy(segmentLabelDict)
                    aliasLabelDict[LABEL_KEY][NAME_KEY] = alias[NAME_KEY]
                    del aliasLabelDict[LABEL_KEY][ALIASES_KEY]
                    if INSTANCE_KEY in aliasLabelDict[LABEL_KEY]:
                        del aliasLabelDict[LABEL_KEY][INSTANCE_KEY]
                    expandInferenceOutputs.append(aliasLabelDict)

        inferenceOutputsWithAliases[VIDEO_LABEL_KEY].extend(expandInferenceOutputs)

    return inferenceOutputsWithAliases


if __name__ == "__main__":

    segmentOutputWithExpandAliases = expand_aliases(EXAMPLE_SEGMENT_OUTPUT)
    assert segmentOutputWithExpandAliases == EXPECTED_EXPANDED_SEGMENT_OUTPUT

    timestampOutputWithExpandAliases = expand_aliases(EXAMPLE_TIMESTAMP_OUTPUT)
    assert timestampOutputWithExpandAliases == EXPECTED_EXPANDED_TIMESTAMP_OUTPUT
```