

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

# 使用適用於 Kotlin 的 SDK 的 Amazon Rekognition 範例
<a name="kotlin_rekognition_code_examples"></a>

下列程式碼範例示範如何使用適用於 Kotlin 的 AWS SDK 搭配 Amazon Rekognition 來執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

*案例*是向您展示如何呼叫服務中的多個函數或與其他 AWS 服務組合來完成特定任務的程式碼範例。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)
+ [案例](#scenarios)

## 動作
<a name="actions"></a>

### `CompareFaces`
<a name="rekognition_CompareFaces_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `CompareFaces`。

如需詳細資訊，請參閱[比較映像中的人臉](https://docs.aws.amazon.com/rekognition/latest/dg/faces-comparefaces.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun compareTwoFaces(
    similarityThresholdVal: Float,
    sourceImageVal: String,
    targetImageVal: String,
) {
    val sourceBytes = (File(sourceImageVal).readBytes())
    val targetBytes = (File(targetImageVal).readBytes())

    // Create an Image object for the source image.
    val souImage =
        Image {
            bytes = sourceBytes
        }

    val tarImage =
        Image {
            bytes = targetBytes
        }

    val facesRequest =
        CompareFacesRequest {
            sourceImage = souImage
            targetImage = tarImage
            similarityThreshold = similarityThresholdVal
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->

        val compareFacesResult = rekClient.compareFaces(facesRequest)
        val faceDetails = compareFacesResult.faceMatches

        if (faceDetails != null) {
            for (match: CompareFacesMatch in faceDetails) {
                val face = match.face
                val position = face?.boundingBox
                if (position != null) {
                    println("Face at ${position.left} ${position.top} matches with ${face.confidence} % confidence.")
                }
            }
        }

        val uncompared = compareFacesResult.unmatchedFaces
        if (uncompared != null) {
            println("There was ${uncompared.size} face(s) that did not match")
        }

        println("Source image rotation: ${compareFacesResult.sourceImageOrientationCorrection}")
        println("target image rotation: ${compareFacesResult.targetImageOrientationCorrection}")
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [CompareFaces](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `CreateCollection`
<a name="rekognition_CreateCollection_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `CreateCollection`。

如需更多資訊，請參閱[建立集合](https://docs.aws.amazon.com/rekognition/latest/dg/create-collection-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun createMyCollection(collectionIdVal: String) {
    val request =
        CreateCollectionRequest {
            collectionId = collectionIdVal
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.createCollection(request)
        println("Collection ARN is ${response.collectionArn}")
        println("Status code is ${response.statusCode}")
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [CreateCollection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DeleteCollection`
<a name="rekognition_DeleteCollection_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DeleteCollection`。

如需更多資訊，請參閱[刪除集合](https://docs.aws.amazon.com/rekognition/latest/dg/delete-collection-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun deleteMyCollection(collectionIdVal: String) {
    val request =
        DeleteCollectionRequest {
            collectionId = collectionIdVal
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.deleteCollection(request)
        println("The collectionId status is ${response.statusCode}")
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [DeleteCollection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DeleteFaces`
<a name="rekognition_DeleteFaces_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DeleteFaces`。

如需詳細資訊，請參閱[從集合中刪除人臉](https://docs.aws.amazon.com/rekognition/latest/dg/delete-faces-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun deleteFacesCollection(
    collectionIdVal: String?,
    faceIdVal: String,
) {
    val deleteFacesRequest =
        DeleteFacesRequest {
            collectionId = collectionIdVal
            faceIds = listOf(faceIdVal)
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        rekClient.deleteFaces(deleteFacesRequest)
        println("$faceIdVal was deleted from the collection")
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [DeleteFaces](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DescribeCollection`
<a name="rekognition_DescribeCollection_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DescribeCollection`。

如需詳細資訊，請參閱[描述集合](https://docs.aws.amazon.com/rekognition/latest/dg/describe-collection-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun describeColl(collectionName: String) {
    val request =
        DescribeCollectionRequest {
            collectionId = collectionName
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.describeCollection(request)
        println("The collection Arn is ${response.collectionArn}")
        println("The collection contains this many faces ${response.faceCount}")
    }
}
```
+  如需 API 詳細資訊，請參閱《適用於 *AWS Kotlin 的 SDK API 參考*》中的 [DescribeCollection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DetectFaces`
<a name="rekognition_DetectFaces_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DetectFaces`。

如需詳細資訊，請參閱[偵測影像中的人臉](https://docs.aws.amazon.com/rekognition/latest/dg/faces-detect-images.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun detectFacesinImage(sourceImage: String?) {
    val souImage =
        Image {
            bytes = (File(sourceImage).readBytes())
        }

    val request =
        DetectFacesRequest {
            attributes = listOf(Attribute.All)
            image = souImage
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.detectFaces(request)
        response.faceDetails?.forEach { face ->
            val ageRange = face.ageRange
            println("The detected face is estimated to be between ${ageRange?.low} and ${ageRange?.high} years old.")
            println("There is a smile ${face.smile?.value}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [DetectFaces](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DetectLabels`
<a name="rekognition_DetectLabels_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DetectLabels`。

如需詳細資訊，請參閱[偵測映像中的標籤](https://docs.aws.amazon.com/rekognition/latest/dg/labels-detect-labels-image.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun detectImageLabels(sourceImage: String) {
    val souImage =
        Image {
            bytes = (File(sourceImage).readBytes())
        }
    val request =
        DetectLabelsRequest {
            image = souImage
            maxLabels = 10
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.detectLabels(request)
        response.labels?.forEach { label ->
            println("${label.name} : ${label.confidence}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [DetectLabels](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DetectModerationLabels`
<a name="rekognition_DetectModerationLabels_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DetectModerationLabels`。

如需詳細資訊，請參閱[偵測不適合的映像](https://docs.aws.amazon.com/rekognition/latest/dg/procedure-moderate-images.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun detectModLabels(sourceImage: String) {
    val myImage =
        Image {
            this.bytes = (File(sourceImage).readBytes())
        }

    val request =
        DetectModerationLabelsRequest {
            image = myImage
            minConfidence = 60f
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.detectModerationLabels(request)
        response.moderationLabels?.forEach { label ->
            println("Label: ${label.name} - Confidence: ${label.confidence} % Parent: ${label.parentName}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [DetectModerationLabels](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `DetectText`
<a name="rekognition_DetectText_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `DetectText`。

如需更多資訊，請參閱[偵測映像中的文字](https://docs.aws.amazon.com/rekognition/latest/dg/text-detecting-text-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun detectTextLabels(sourceImage: String?) {
    val souImage =
        Image {
            bytes = (File(sourceImage).readBytes())
        }

    val request =
        DetectTextRequest {
            image = souImage
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.detectText(request)
        response.textDetections?.forEach { text ->
            println("Detected: ${text.detectedText}")
            println("Confidence: ${text.confidence}")
            println("Id: ${text.id}")
            println("Parent Id:  ${text.parentId}")
            println("Type: ${text.type}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [DetectText](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `IndexFaces`
<a name="rekognition_IndexFaces_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `IndexFaces`。

如需詳細資訊，請參閱[將人臉新增至集合](https://docs.aws.amazon.com/rekognition/latest/dg/add-faces-to-collection-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun addToCollection(
    collectionIdVal: String?,
    sourceImage: String,
) {
    val souImage =
        Image {
            bytes = (File(sourceImage).readBytes())
        }

    val request =
        IndexFacesRequest {
            collectionId = collectionIdVal
            image = souImage
            maxFaces = 1
            qualityFilter = QualityFilter.Auto
            detectionAttributes = listOf(Attribute.Default)
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val facesResponse = rekClient.indexFaces(request)

        // Display the results.
        println("Results for the image")
        println("\n Faces indexed:")
        facesResponse.faceRecords?.forEach { faceRecord ->
            println("Face ID: ${faceRecord.face?.faceId}")
            println("Location: ${faceRecord.faceDetail?.boundingBox}")
        }

        println("Faces not indexed:")
        facesResponse.unindexedFaces?.forEach { unindexedFace ->
            println("Location: ${unindexedFace.faceDetail?.boundingBox}")
            println("Reasons:")

            unindexedFace.reasons?.forEach { reason ->
                println("Reason:  $reason")
            }
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [IndexFaces](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `ListCollections`
<a name="rekognition_ListCollections_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `ListCollections`。

如需詳細資訊，請參閱[列出的集合](https://docs.aws.amazon.com/rekognition/latest/dg/list-collection-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun listAllCollections() {
    val request =
        ListCollectionsRequest {
            maxResults = 10
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.listCollections(request)
        response.collectionIds?.forEach { resultId ->
            println(resultId)
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [ListCollections](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `ListFaces`
<a name="rekognition_ListFaces_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `ListFaces`。

如需更多資訊，請參閱[集合中列出的人臉](https://docs.aws.amazon.com/rekognition/latest/dg/list-faces-in-collection-procedure.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun listFacesCollection(collectionIdVal: String?) {
    val request =
        ListFacesRequest {
            collectionId = collectionIdVal
            maxResults = 10
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val response = rekClient.listFaces(request)
        response.faces?.forEach { face ->
            println("Confidence level there is a face: ${face.confidence}")
            println("The face Id value is ${face.faceId}")
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [ListFaces](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

### `RecognizeCelebrities`
<a name="rekognition_RecognizeCelebrities_kotlin_topic"></a>

以下程式碼範例顯示如何使用 `RecognizeCelebrities`。

如需詳細資訊，請參閱[在映像中辨識名人](https://docs.aws.amazon.com/rekognition/latest/dg/celebrities-procedure-image.html)。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。

```
suspend fun recognizeAllCelebrities(sourceImage: String?) {
    val souImage =
        Image {
            bytes = (File(sourceImage).readBytes())
        }

    val request =
        RecognizeCelebritiesRequest {
            image = souImage
        }

    RekognitionClient.fromEnvironment { 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.")
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的 [RecognizeCelebrities](https://sdk.amazonaws.com/kotlin/api/latest/index.html)。

## 案例
<a name="scenarios"></a>

### 建立無伺服器應用程式來管理相片
<a name="cross_PAM_kotlin_topic"></a>

下列程式碼範例示範如何建立無伺服器應用程式，讓使用者以標籤管理相片。

**SDK for Kotlin**  
 顯示如何開發照片資產管理應用程式，以便使用 Amazon Rekognition 偵測圖片中的標籤，並將其儲存以供日後擷取。  
如需完整的原始碼和如何設定及執行的指示，請參閱 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/usecases/creating_pam) 上的完整範例。  
如要深入探索此範例的來源，請參閱 [AWS  社群](https://community.aws/posts/cloud-journeys/01-serverless-image-recognition-app)上的文章。  

**此範例中使用的服務**
+ API Gateway
+ DynamoDB
+ Lambda
+ Amazon Rekognition
+ Amazon S3
+ Amazon SNS

### 偵測映像中的資訊
<a name="rekognition_VideoDetection_kotlin_topic"></a>

以下程式碼範例顯示做法：
+ 啟動 Amazon Rekognition 任務，以偵測影片中的人物、物件和文字等元素。
+ 檢查工作狀態，直到工作完成。
+ 輸出每個工作偵測到的元素清單。

**適用於 Kotlin 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/services/rekognition#code-examples)中設定和執行。
偵測存放於 Amazon S3 儲存貯體中的人臉。  

```
suspend fun startFaceDetection(
    channelVal: NotificationChannel?,
    bucketVal: String,
    videoVal: String,
) {
    val s3Obj =
        S3Object {
            bucket = bucketVal
            name = videoVal
        }
    val vidOb =
        Video {
            s3Object = s3Obj
        }

    val request =
        StartFaceDetectionRequest {
            jobTag = "Faces"
            faceAttributes = FaceAttributes.All
            notificationChannel = channelVal
            video = vidOb
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val startLabelDetectionResult = rekClient.startFaceDetection(request)
        startJobId = startLabelDetectionResult.jobId.toString()
    }
}

suspend fun getFaceResults() {
    var finished = false
    var status: String
    var yy = 0
    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        var response: GetFaceDetectionResponse? = null

        val recognitionRequest =
            GetFaceDetectionRequest {
                jobId = startJobId
                maxResults = 10
            }

        // Wait until the job succeeds.
        while (!finished) {
            response = rekClient.getFaceDetection(recognitionRequest)
            status = response.jobStatus.toString()
            if (status.compareTo("Succeeded") == 0) {
                finished = true
            } else {
                println("$yy status is: $status")
                delay(1000)
            }
            yy++
        }

        // Proceed when the job is done - otherwise VideoMetadata is null.
        val videoMetaData = response?.videoMetadata
        println("Format: ${videoMetaData?.format}")
        println("Codec: ${videoMetaData?.codec}")
        println("Duration: ${videoMetaData?.durationMillis}")
        println("FrameRate: ${videoMetaData?.frameRate}")

        // Show face information.
        response?.faces?.forEach { face ->
            println("Age: ${face.face?.ageRange}")
            println("Face: ${face.face?.beard}")
            println("Eye glasses: ${face?.face?.eyeglasses}")
            println("Mustache: ${face.face?.mustache}")
            println("Smile: ${face.face?.smile}")
        }
    }
}
```
偵測 Amazon S3 儲存貯體中存放影片中的不當或冒犯性內容。  

```
suspend fun startModerationDetection(
    channel: NotificationChannel?,
    bucketVal: String?,
    videoVal: String?,
) {
    val s3Obj =
        S3Object {
            bucket = bucketVal
            name = videoVal
        }
    val vidOb =
        Video {
            s3Object = s3Obj
        }
    val request =
        StartContentModerationRequest {
            jobTag = "Moderation"
            notificationChannel = channel
            video = vidOb
        }

    RekognitionClient.fromEnvironment { region = "us-east-1" }.use { rekClient ->
        val startModDetectionResult = rekClient.startContentModeration(request)
        startJobId = startModDetectionResult.jobId.toString()
    }
}

suspend fun getModResults() {
    var finished = false
    var status: String
    var yy = 0
    RekognitionClient { region = "us-east-1" }.use { rekClient ->
        var modDetectionResponse: GetContentModerationResponse? = null

        val modRequest =
            GetContentModerationRequest {
                jobId = startJobId
                maxResults = 10
            }

        // Wait until the job succeeds.
        while (!finished) {
            modDetectionResponse = rekClient.getContentModeration(modRequest)
            status = modDetectionResponse.jobStatus.toString()
            if (status.compareTo("Succeeded") == 0) {
                finished = true
            } else {
                println("$yy status is: $status")
                delay(1000)
            }
            yy++
        }

        // Proceed when the job is done - otherwise VideoMetadata is null.
        val videoMetaData = modDetectionResponse?.videoMetadata
        println("Format: ${videoMetaData?.format}")
        println("Codec: ${videoMetaData?.codec}")
        println("Duration: ${videoMetaData?.durationMillis}")
        println("FrameRate: ${videoMetaData?.frameRate}")

        modDetectionResponse?.moderationLabels?.forEach { mod ->
            val seconds: Long = mod.timestamp / 1000
            print("Mod label: $seconds ")
            println(mod.moderationLabel)
        }
    }
}
```
+ 如需 API 詳細資訊，請參閱《AWS 適用於 Kotlin 的 SDK API 參考》**中的下列主題。
  + [GetCelebrityRecognition](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [GetContentModeration](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [GetLabelDetection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [GetPersonTracking](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [GetSegmentDetection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [GetTextDetection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [StartCelebrityRecognition](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [StartContentModeration](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [StartLabelDetection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [StartPersonTracking](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [StartSegmentDetection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)
  + [StartTextDetection](https://sdk.amazonaws.com/kotlin/api/latest/index.html)

### 偵測映像中的物件
<a name="cross_RekognitionPhotoAnalyzer_kotlin_topic"></a>

下列程式碼範例示範如何建置應用程式，該應用程式可使用 Amazon Rekognition 依影像中的類別偵測物件。

**適用於 Kotlin 的 SDK **  
 展示如何使用 Amazon Rekognition Kotlin API 建立應用程式，該應用程式使用 Amazon Rekognition 對位於 Amazon Simple Storage Service (Amazon S3) 儲存貯體中的映像按類別識別物件。此應用程式可使用 Amazon Simple Email Service (Amazon SES) 向管理員傳送包含結果的電子郵件通知。  
 如需完整的原始碼和如何設定及執行的指示，請參閱 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/kotlin/usecases/creating_photo_analyzer_app) 上的完整範例。  

**此範例中使用的服務**
+ Amazon Rekognition
+ Amazon S3
+ Amazon SES