จดจำจุดสังเกตได้อย่างปลอดภัยด้วย Cloud Vision โดยใช้การตรวจสอบสิทธิ์และฟังก์ชันของ Firebase บน Android

หากต้องการเรียกใช้ Google Cloud API จากแอป คุณต้องสร้าง REST API ระดับกลาง ที่จัดการการให้สิทธิ์และปกป้องค่าลับ เช่น คีย์ API จากนั้นคุณต้อง เขียนโค้ดในแอปบนอุปกรณ์เคลื่อนที่เพื่อตรวจสอบสิทธิ์และสื่อสารกับบริการตัวกลางนี้

วิธีหนึ่งในการสร้าง REST API นี้คือการใช้ การตรวจสอบสิทธิ์ Firebase และ ฟังก์ชัน ซึ่งจะช่วยให้คุณมีเกตเวย์แบบ Serverless ที่มีการจัดการไปยัง Cloud APIs ซึ่งจัดการการตรวจสอบสิทธิ์และเรียกใช้จากแอปบนอุปกรณ์เคลื่อนที่ได้ด้วย SDK ที่สร้างไว้ล่วงหน้า

คู่มือนี้แสดงวิธีใช้เทคนิคนี้เพื่อเรียกใช้ Cloud Vision API จากแอป วิธีนี้จะช่วยให้ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์ทั้งหมดเข้าถึงบริการที่เรียกเก็บเงินของ Cloud Vision ผ่านโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของคุณได้ ดังนั้น โปรดพิจารณาว่ากลไกการตรวจสอบสิทธิ์นี้เพียงพอสำหรับ Use Case ของคุณหรือไม่ก่อนดำเนินการต่อ

ก่อนเริ่มต้น

กำหนดค่าโปรเจ็กต์

  1. เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้เพิ่ม
  2. หากยังไม่ได้เปิดใช้ API บนระบบคลาวด์สำหรับโปรเจ็กต์ ให้ทำดังนี้ ตอนนี้

    1. เปิดFirebase ML หน้า API ในคอนโซล Firebase
    2. หากยังไม่ได้อัปเกรดโปรเจ็กต์เป็นแพ็กเกจราคา Blaze แบบจ่ายเมื่อใช้ ให้คลิกอัปเกรดเพื่อดำเนินการ (ระบบจะแจ้งให้คุณอัปเกรดก็ต่อเมื่อโปรเจ็กต์ไม่ได้ใช้แพ็กเกจราคา Blaze)

      เฉพาะโปรเจ็กต์ในแพ็กเกจราคา Blaze เท่านั้นที่ใช้ API บนระบบคลาวด์ได้

    3. หากยังไม่ได้เปิดใช้ API บนระบบคลาวด์ ให้คลิก เปิดใช้ API บนระบบคลาวด์
  3. กำหนดค่าคีย์ API ของ Firebase ที่มีอยู่เพื่อไม่อนุญาตให้เข้าถึง Cloud Vision API ดังนี้
    1. เปิดหน้าข้อมูลเข้าสู่ระบบของ Cloud Console
    2. สำหรับคีย์ API แต่ละรายการในรายการ ให้เปิดมุมมองการแก้ไข แล้วเพิ่ม API ที่พร้อมใช้งานทั้งหมดยกเว้น Cloud Vision API ลงในรายการในส่วนข้อจำกัดของคีย์

ทําให้ฟังก์ชันที่เรียกใช้ได้ใช้งานได้

จากนั้น ให้ติดตั้งใช้งาน Cloud Function ที่คุณจะใช้เพื่อเชื่อมต่อแอปกับ Cloud Vision API functions-samplesที่เก็บมีตัวอย่าง ที่คุณใช้ได้

โดยค่าเริ่มต้น การเข้าถึง Cloud Vision API ผ่านฟังก์ชันนี้จะอนุญาตให้ เฉพาะผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์ของแอปเข้าถึง Cloud Vision API ได้ คุณ แก้ไขฟังก์ชันสำหรับข้อกำหนดต่างๆ ได้

วิธีทำให้ฟังก์ชันใช้งานได้

  1. โคลนหรือดาวน์โหลดที่เก็บฟังก์ชันตัวอย่าง แล้วเปลี่ยนเป็นไดเรกทอรี Node-1st-gen/vision-annotate-image
    git clone https://github.com/firebase/functions-samples
    cd Node-1st-gen/vision-annotate-image
    
  2. ติดตั้งการอ้างอิงด้วยคำสั่งต่อไปนี้
    cd functions
    npm install
    cd ..
  3. หากยังไม่มี Firebase CLI ให้ติดตั้ง
  4. เริ่มต้นโปรเจ็กต์ Firebase ในไดเรกทอรี vision-annotate-image เมื่อได้รับข้อความแจ้ง ให้เลือกโปรเจ็กต์ในรายการ
    firebase init
  5. ทำให้ฟังก์ชันใช้งานได้
    firebase deploy --only functions:annotateImage

เพิ่ม Firebase Auth ไปยังแอป

ฟังก์ชันที่เรียกใช้ได้ซึ่งติดตั้งใช้งานข้างต้นจะปฏิเสธคำขอจากผู้ใช้แอปที่ไม่ได้รับการตรวจสอบสิทธิ์ หากยังไม่ได้ดำเนินการ คุณจะต้องเพิ่ม Firebase Auth ลงในแอป

เพิ่มทรัพยากร Dependency ที่จำเป็นลงในแอป

  • เพิ่มทรัพยากร Dependency สำหรับ Cloud Functions for Firebase (ไคลเอ็นต์) และไลบรารี gson Android ไปยังไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติคือ <project>/<app-module>/build.gradle.kts หรือ <project>/<app-module>/build.gradle)
    implementation("com.google.firebase:firebase-functions:22.1.0")
    implementation("com.google.code.gson:gson:2.8.6")
  • 1. เตรียมรูปภาพอินพุต

    หากต้องการเรียกใช้ Cloud Vision รูปภาพต้องอยู่ในรูปแบบสตริงที่เข้ารหัส Base64 หากต้องการประมวลผลรูปภาพจาก URI ของไฟล์ที่บันทึกไว้ ให้ทำดังนี้
    1. รับรูปภาพเป็นออบเจ็กต์ Bitmap

      Kotlin

      var bitmap: Bitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri)

      Java

      Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
    2. คุณอาจลดขนาดรูปภาพเพื่อประหยัดแบนด์วิดท์ได้ ดู ขนาดรูปภาพที่ Cloud Vision แนะนำ

      Kotlin

      private fun scaleBitmapDown(bitmap: Bitmap, maxDimension: Int): Bitmap {
          val originalWidth = bitmap.width
          val originalHeight = bitmap.height
          var resizedWidth = maxDimension
          var resizedHeight = maxDimension
          if (originalHeight > originalWidth) {
              resizedHeight = maxDimension
              resizedWidth =
                  (resizedHeight * originalWidth.toFloat() / originalHeight.toFloat()).toInt()
          } else if (originalWidth > originalHeight) {
              resizedWidth = maxDimension
              resizedHeight =
                  (resizedWidth * originalHeight.toFloat() / originalWidth.toFloat()).toInt()
          } else if (originalHeight == originalWidth) {
              resizedHeight = maxDimension
              resizedWidth = maxDimension
          }
          return Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false)
      }

      Java

      private Bitmap scaleBitmapDown(Bitmap bitmap, int maxDimension) {
          int originalWidth = bitmap.getWidth();
          int originalHeight = bitmap.getHeight();
          int resizedWidth = maxDimension;
          int resizedHeight = maxDimension;
      
          if (originalHeight > originalWidth) {
              resizedHeight = maxDimension;
              resizedWidth = (int) (resizedHeight * (float) originalWidth / (float) originalHeight);
          } else if (originalWidth > originalHeight) {
              resizedWidth = maxDimension;
              resizedHeight = (int) (resizedWidth * (float) originalHeight / (float) originalWidth);
          } else if (originalHeight == originalWidth) {
              resizedHeight = maxDimension;
              resizedWidth = maxDimension;
          }
          return Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false);
      }

      Kotlin

      // Scale down bitmap size
      bitmap = scaleBitmapDown(bitmap, 640)

      Java

      // Scale down bitmap size
      bitmap = scaleBitmapDown(bitmap, 640);
    3. แปลงออบเจ็กต์บิตแมปเป็นสตริงที่เข้ารหัส Base64

      Kotlin

      // Convert bitmap to base64 encoded string
      val byteArrayOutputStream = ByteArrayOutputStream()
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
      val imageBytes: ByteArray = byteArrayOutputStream.toByteArray()
      val base64encoded = Base64.encodeToString(imageBytes, Base64.NO_WRAP)

      Java

      // Convert bitmap to base64 encoded string
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
      byte[] imageBytes = byteArrayOutputStream.toByteArray();
      String base64encoded = Base64.encodeToString(imageBytes, Base64.NO_WRAP);
    4. รูปภาพที่แสดงโดยออบเจ็กต์ Bitmap ต้องตั้งตรงโดยไม่ต้องหมุนเพิ่มเติม

    2. เรียกใช้ฟังก์ชันที่เรียกใช้ได้เพื่อจดจำสถานที่สำคัญ

    หากต้องการจดจำสถานที่สำคัญในรูปภาพ ให้เรียกใช้ฟังก์ชันที่เรียกใช้ได้โดยส่งคำขอ JSON ของ Cloud Vision

    1. ก่อนอื่น ให้เริ่มต้นอินสแตนซ์ของ Cloud Functions ดังนี้

      Kotlin

      private lateinit var functions: FirebaseFunctions
      // ...
      functions = Firebase.functions
      

      Java

      private FirebaseFunctions mFunctions;
      // ...
      mFunctions = FirebaseFunctions.getInstance();
      
    2. กำหนดวิธีการเรียกใช้ฟังก์ชัน

      Kotlin

      private fun annotateImage(requestJson: String): Task<JsonElement> {
          return functions
              .getHttpsCallable("annotateImage")
              .call(requestJson)
              .continueWith { task ->
                  // This continuation runs on either success or failure, but if the task
                  // has failed then result will throw an Exception which will be
                  // propagated down.
                  val result = task.result?.data
                  JsonParser.parseString(Gson().toJson(result))
              }
      }
      

      Java

      private Task<JsonElement> annotateImage(String requestJson) {
          return mFunctions
                  .getHttpsCallable("annotateImage")
                  .call(requestJson)
                  .continueWith(new Continuation<HttpsCallableResult, JsonElement>() {
                      @Override
                      public JsonElement then(@NonNull Task<HttpsCallableResult> task) {
                          // This continuation runs on either success or failure, but if the task
                          // has failed then getResult() will throw an Exception which will be
                          // propagated down.
                          return JsonParser.parseString(new Gson().toJson(task.getResult().getData()));
                      }
                  });
      }
      
    3. สร้างคำขอ JSON ด้วยประเภท LANDMARK_DETECTION

      Kotlin

      // Create json request to cloud vision
      val request = JsonObject()
      // Add image to request
      val image = JsonObject()
      image.add("content", JsonPrimitive(base64encoded))
      request.add("image", image)
      // Add features to the request
      val feature = JsonObject()
      feature.add("maxResults", JsonPrimitive(5))
      feature.add("type", JsonPrimitive("LANDMARK_DETECTION"))
      val features = JsonArray()
      features.add(feature)
      request.add("features", features)
      

      Java

      // Create json request to cloud vision
      JsonObject request = new JsonObject();
      // Add image to request
      JsonObject image = new JsonObject();
      image.add("content", new JsonPrimitive(base64encoded));
      request.add("image", image);
      //Add features to the request
      JsonObject feature = new JsonObject();
      feature.add("maxResults", new JsonPrimitive(5));
      feature.add("type", new JsonPrimitive("LANDMARK_DETECTION"));
      JsonArray features = new JsonArray();
      features.add(feature);
      request.add("features", features);
      
    4. สุดท้าย ให้เรียกใช้ฟังก์ชันโดยใช้คำสั่งต่อไปนี้

      Kotlin

      annotateImage(request.toString())
          .addOnCompleteListener { task ->
              if (!task.isSuccessful) {
                  // Task failed with an exception
                  // ...
              } else {
                  // Task completed successfully
                  // ...
              }
          }
      

      Java

      annotateImage(request.toString())
              .addOnCompleteListener(new OnCompleteListener<JsonElement>() {
                  @Override
                  public void onComplete(@NonNull Task<JsonElement> task) {
                      if (!task.isSuccessful()) {
                          // Task failed with an exception
                          // ...
                      } else {
                          // Task completed successfully
                          // ...
                      }
                  }
              });
      

    3. ดูข้อมูลเกี่ยวกับจุดสังเกตที่ระบบจดจำได้

    หากการดำเนินการจดจำสถานที่สำคัญสำเร็จ ระบบจะแสดงผลการตอบกลับ JSON ของ BatchAnnotateImagesResponse ในผลลัพธ์ของงาน ออบเจ็กต์แต่ละรายการในอาร์เรย์ landmarkAnnotations จะแสดงสถานที่สำคัญที่ระบบจดจำได้ในรูปภาพ สำหรับสถานที่สำคัญแต่ละแห่ง คุณจะได้รับพิกัดขอบเขตในรูปภาพอินพุต ชื่อของสถานที่สำคัญ ละติจูดและลองจิจูด รหัสเอนทิตีของกราฟความรู้ (หากมี) และ คะแนนความเชื่อมั่นของการจับคู่ เช่น

    Kotlin

    for (label in task.result!!.asJsonArray[0].asJsonObject["landmarkAnnotations"].asJsonArray) {
        val labelObj = label.asJsonObject
        val landmarkName = labelObj["description"]
        val entityId = labelObj["mid"]
        val score = labelObj["score"]
        val bounds = labelObj["boundingPoly"]
        // Multiple locations are possible, e.g., the location of the depicted
        // landmark and the location the picture was taken.
        for (loc in labelObj["locations"].asJsonArray) {
            val latitude = loc.asJsonObject["latLng"].asJsonObject["latitude"]
            val longitude = loc.asJsonObject["latLng"].asJsonObject["longitude"]
        }
    }
    

    Java

    for (JsonElement label : task.getResult().getAsJsonArray().get(0).getAsJsonObject().get("landmarkAnnotations").getAsJsonArray()) {
        JsonObject labelObj = label.getAsJsonObject();
        String landmarkName = labelObj.get("description").getAsString();
        String entityId = labelObj.get("mid").getAsString();
        float score = labelObj.get("score").getAsFloat();
        JsonObject bounds = labelObj.get("boundingPoly").getAsJsonObject();
        // Multiple locations are possible, e.g., the location of the depicted
        // landmark and the location the picture was taken.
        for (JsonElement loc : labelObj.get("locations").getAsJsonArray()) {
            JsonObject latLng = loc.getAsJsonObject().get("latLng").getAsJsonObject();
            double latitude = latLng.get("latitude").getAsDouble();
            double longitude = latLng.get("longitude").getAsDouble();
        }
    }