Kotlin AWS Rekognition转换

时间:2018-07-26 21:01:34

标签: java android amazon-web-services kotlin amazon-rekognition

我目前正在移植Swift for iOS内置的应用程序。迅速通过以下方式进行重新识别:

在将包导入到您的podfile中之后,首先初始化客户端:

rekognitionClient = AWSRekognition.default()

然后创建一个“ faceRequest”以调用该服务,并查看集合中的面孔是否与您发送的图像匹配:

guard let FaceRequest = AWSRekognitionSearchFacesByImageRequest() else
       {
           puts("Unable to initialize AWSRekognitionSearchfacerequest.")
           return
       }
       FaceRequest.collectionId = "MY_COLLECTION_NAME"
       FaceRequest.faceMatchThreshold = 75
       FaceRequest.maxFaces = 2
       let FacesourceImage = capturedImage
       let Faceimage = AWSRekognitionImage()
       Faceimage!.bytes = UIImageJPEGRepresentation(FacesourceImage!, 0.7)
       FaceRequest.image = Faceimage
       rekognitionClient.searchFaces(byImage:FaceRequest) { (response:AWSRekognitionSearchFacesByImageResponse?, error:Error?) in
           if error == nil
           {
               //print(response!)

               for faceMatch in (response?.faceMatches)! {
                     //do something
               }
           }
        }

我希望将其转换为Kotlin,并且在语法和发出请求方面遇到问题。我有位图格式的图像,可以发送到服务了。

这是我一直在尝试的一个示例:

fun doRekognitionRequest(bitmap: Bitmap){

    //this says AmazonRekognitionClient has been deprecated
    val rekognitionClient = AmazonRekognitionClient()

    //unresolved reference
    val facesImageRequest = facesByImageRequest()
}

这是我的进口货

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.SurfaceTexture
import android.graphics.drawable.BitmapDrawable
import android.hardware.camera2.*
import android.net.Uri
import android.os.*
import android.util.Log
import android.view.*
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import kotlinx.android.synthetic.main.camera_layout.*
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions
import java.io.*
import java.io.File
import java.util.*
import android.provider.MediaStore
import androidx.core.content.FileProvider
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClient
import com.amazonaws.services.rekognition.model.FaceMatch;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.S3Object;
import com.amazonaws.services.rekognition.model.SearchFacesByImageRequest;
import com.amazonaws.services.rekognition.model.SearchFacesByImageResult;

我正在尝试基于这个人所做的事情:https://github.com/awslabs/serverless-photo-recognition/blob/master/src/main/kotlin/com/budilov/rekognition/RekognitionService.kt

除了尝试将Java示例代码从识别文档转换为Kotlin之外,没有任何运气。

https://docs.aws.amazon.com/rekognition/latest/dg/search-face-with-image-procedure.html

   AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient();

  ObjectMapper objectMapper = new ObjectMapper();

   // Get an image object from S3 bucket.
  Image image=new Image()
          .withS3Object(new S3Object()
                  .withBucket(bucket)
                  .withName(photo));

  // Search collection for faces similar to the largest face in the image.
  SearchFacesByImageRequest searchFacesByImageRequest = new SearchFacesByImageRequest()
          .withCollectionId(collectionId)
          .withImage(image)
          .withFaceMatchThreshold(70F)
          .withMaxFaces(2);

   SearchFacesByImageResult searchFacesByImageResult = 
           rekognitionClient.searchFacesByImage(searchFacesByImageRequest);

   System.out.println("Faces matching largest face in image from" + photo);
  List < FaceMatch > faceImageMatches = searchFacesByImageResult.getFaceMatches();
  for (FaceMatch face: faceImageMatches) {
      System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
              .writeValueAsString(face));
     System.out.println();
  }

Kotlin和Android对我来说都是非常新的东西,我来自C#和Swift背景,因此非常感谢您的帮助。 干杯!

修改

设法使searchfacesbyImageRequest构造函数被编译器看到。现在只能将位图转换为图像。

    val facesImageRequest = SearchFacesByImageRequest()
    facesImageRequest.collectionId = "MY_COLLECTION_NAME"
    facesImageRequest.maxFaces = 2
    facesImageRequest.faceMatchThreshold = 75.0F
    facesImageRequest.image = Image(bitmap)

1 个答案:

答案 0 :(得分:1)

这是从com.amazonaws.services.rekognition.model.Image构造android.graphics.Bitmap的一种方法

val bitmap : Bitmap? = // Source of your picture
val byteBuffer = ByteBuffer.allocate(bitmap.byteCount)
bitmap?.copyPixelsToBuffer(byteBuffer)
val image = Image().withBytes(byteBuffer)