GenAI Image Description API

API คำอธิบายรูปภาพ GenAI ของ ML Kit ช่วยให้คุณสร้างคำอธิบายเนื้อหาสั้นๆ สำหรับรูปภาพได้ ซึ่งอาจมีประโยชน์ในกรณีการใช้งานต่อไปนี้

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

ความสามารถหลัก

  • แสดงคำอธิบายสั้นๆ สำหรับรูปภาพอินพุต

ตัวอย่างผลการแข่ง

อินพุต เอาต์พุต
หุ่นยนต์ Android สีเขียวตัวเล็กที่มีดีไซน์คล้ายกระบองเพชรนั่งอยู่บน
             พื้นผิวสีดำ หุ่นยนต์ Android สีเขียวตัวเล็กที่มีดีไซน์คล้ายกระบองเพชรนั่งอยู่บน พื้นผิวสีดำ
สุนัขตัวเล็กสีขาวที่มีจมูกสีดำและลิ้นสีชมพูวิ่ง
                ข้ามทุ่งหญ้าที่มีสะพานเป็นฉากหลัง สุนัขตัวเล็กสีขาวที่มีจมูกสีดำและลิ้นสีชมพูวิ่งข้าม ทุ่งหญ้าที่มีสะพานเป็นฉากหลัง

เริ่มต้นใช้งาน

หากต้องการเริ่มต้นใช้งาน GenAI Image Description API ให้เพิ่มการอ้างอิงนี้ลงในไฟล์บิลด์ของโปรเจ็กต์

implementation("com.google.mlkit:genai-image-description:1.0.0-beta1")

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

Kotlin

// Create an image describer
val options = ImageDescriberOptions.builder(context).build()
val imageDescriber = ImageDescription.getClient(options)

suspend fun prepareAndStartImageDescription(
    bitmap: Bitmap
) {
  // Check feature availability, status will be one of the following:
  // UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
  val featureStatus = imageDescriber.checkFeatureStatus().await()

  if (featureStatus == FeatureStatus.DOWNLOADABLE) {
      // Download feature if necessary.
      // If downloadFeature is not called, the first inference request
      // will also trigger the feature to be downloaded if it's not
      // already downloaded.
      imageDescriber.downloadFeature(object : DownloadCallback {
          override fun onDownloadStarted(bytesToDownload: Long) { }

          override fun onDownloadFailed(e: GenAiException) { }

          override fun onDownloadProgress(totalBytesDownloaded: Long) {}

          override fun onDownloadCompleted() {
              startImageDescriptionRequest(bitmap, imageDescriber)
          }
      })
  } else if (featureStatus == FeatureStatus.DOWNLOADING) {
      // Inference request will automatically run once feature is
      // downloaded.
      // If Gemini Nano is already downloaded on the device, the
      // feature-specific LoRA adapter model will be downloaded
      // very quickly. However, if Gemini Nano is not already
      // downloaded, the download process may take longer.
      startImageDescriptionRequest(bitmap, imageDescriber)
  } else if (featureStatus == FeatureStatus.AVAILABLE) {
      startImageDescriptionRequest(bitmap, imageDescriber)
  }
}

fun startImageDescriptionRequest(
    bitmap: Bitmap,
    imageDescriber: ImageDescriber
) {
    // Create task request
    val imageDescriptionRequest = ImageDescriptionRequest
        .builder(bitmap)
        .build()
}

  // Run inference with a streaming callback
  val imageDescriptionResultStreaming =
      imageDescriber.runInference(imageDescriptionRequest) { outputText ->
          // Append new output text to show in UI
          // This callback is called incrementally as the description
          // is generated
      }

  // You can also get a non-streaming response from the request
  // val imageDescription = imageDescriber.runInference(
  //        imageDescriptionRequest).await().description
}

// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
imageDescriber.close()

Java

// Create an image describer
ImageDescriberOptions options = ImageDescriberOptions.builder(context).build();
ImageDescriber imageDescriber = ImageDescription.getClient(options);

void prepareAndStartImageDescription(
      Bitmap bitmap
) throws ExecutionException, InterruptedException {
  // Check feature availability, status will be one of the following:
  // UNAVAILABLE, DOWNLOADABLE, DOWNLOADING, AVAILABLE
  try {
      int featureStatus = imageDescriber.checkFeatureStatus().get();
      if (featureStatus == FeatureStatus.DOWNLOADABLE) {
          // Download feature if necessary.
          // If downloadFeature is not called, the first inference request
          // will also trigger the feature to be downloaded if it's not
          // already downloaded.
          imageDescriber.downloadFeature(new DownloadCallback() {
              @Override
              public void onDownloadCompleted() {
                  startImageDescriptionRequest(bitmap, imageDescriber);
              }

              @Override
              public void onDownloadFailed(GenAIException e) {}

              @Override
              public void onDownloadProgress(long totalBytesDownloaded) {}

              @Override
              public void onDownloadStarted(long bytesDownloaded) {}
          });
      } else if (featureStatus == FeatureStatus.DOWNLOADING) {
          // Inference request will automatically run once feature is
          // downloaded.
          // If Gemini Nano is already downloaded on the device, the
          // feature-specific LoRA adapter model will be downloaded
          // very quickly. However, if Gemini Nano is not already
          // downloaded, the download process may take longer.
          startImageDescriptionRequest(bitmap, imageDescriber);
      } else if (featureStatus == FeatureStatus.AVAILABLE) {
          startImageDescriptionRequest(bitmap, imageDescriber);
      }
  } catch (ExecutionException | InterruptedException e) {
      e.printStackTrace();
  }
}

void startImageDescriptionRequest(
     Bitmap bitmap,
     ImageDescriber imageDescriber
) {
  // Create task request
  ImageDescriptionRequest imageDescriptionRequest =
          ImageDescriptionRequest.builder(bitmap).build();

  // Start image description request with streaming response
  imageDescriber.runInference(imageDescriptionRequest, newText -> {
      // Append new output text to show in UI
      // This callback is called incrementally as the description
      // is generated
  });

  // You can also get a non-streaming response from the request
  // String imageDescription = imageDescriber.runInference(
  //        imageDescriptionRequest).get().getDescription();
}

// Be sure to release the resource when no longer needed
// For example, on viewModel.onCleared() or activity.onDestroy()
imageDescriber.close();

ฟีเจอร์ที่รองรับและข้อจำกัด

API คำอธิบายรูปภาพ GenAI รองรับภาษาอังกฤษ และจะเพิ่มการรองรับภาษาอื่นๆ ในอนาคต API จะแสดงคำอธิบายสั้นๆ ของรูปภาพ 1 รายการ

ความพร้อมใช้งานของการกำหนดค่าฟีเจอร์ที่เฉพาะเจาะจง (ระบุโดย ImageDescriberOptions) อาจแตกต่างกันไปตามการกำหนดค่าของอุปกรณ์ที่เฉพาะเจาะจง และโมเดลที่ดาวน์โหลดลงในอุปกรณ์

วิธีที่เชื่อถือได้มากที่สุดสำหรับนักพัฒนาแอปในการตรวจสอบว่าฟีเจอร์ API ที่ต้องการ รองรับในอุปกรณ์ที่มี ImageDescriberOptions ที่ขอคือการเรียกใช้เมธอด checkFeatureStatus() วิธีนี้จะแสดงสถานะที่แน่นอน ของความพร้อมใช้งานฟีเจอร์ในอุปกรณ์ขณะรันไทม์

ปัญหาที่พบบ่อยเกี่ยวกับการตั้งค่า

API ของ GenAI ใน ML Kit จะใช้แอป Android AICore เพื่อเข้าถึง Gemini Nano เมื่อตั้งค่าอุปกรณ์ใหม่ (รวมถึงการรีเซ็ต) หรือรีเซ็ตแอป AICore ใหม่ (เช่น ล้างข้อมูล ถอนการติดตั้งแล้วติดตั้งอีกครั้ง) แอป AICore อาจมีเวลาไม่พอที่จะทำการเริ่มต้นให้เสร็จสมบูรณ์ (รวมถึงการดาวน์โหลดการกำหนดค่าล่าสุดจากเซิร์ฟเวอร์) ด้วยเหตุนี้ ML Kit GenAI API จึงอาจทำงานไม่เป็นไปตามที่คาดไว้ ข้อความแสดงข้อผิดพลาดในการตั้งค่าที่พบบ่อยซึ่งคุณอาจเห็นและวิธีจัดการมีดังนี้

ตัวอย่างข้อความแสดงข้อผิดพลาด วิธีจัดการ
AICore ล้มเหลวโดยมีข้อผิดพลาดประเภท 4-CONNECTION_ERROR และรหัสข้อผิดพลาด 601-BINDING_FAILURE: บริการ AICore เชื่อมโยงไม่สำเร็จ ปัญหานี้อาจเกิดขึ้นเมื่อคุณติดตั้งแอปโดยใช้ ML Kit GenAI API ทันทีหลังจากตั้งค่าอุปกรณ์ หรือเมื่อถอนการติดตั้ง AICore หลังจากติดตั้งแอปแล้ว การอัปเดตแอป AICore แล้วติดตั้งแอปของคุณอีกครั้งน่าจะช่วยแก้ปัญหานี้ได้
AICore ล้มเหลวโดยมีข้อผิดพลาดประเภท 3-PREPARATION_ERROR และรหัสข้อผิดพลาด 606-FEATURE_NOT_FOUND: ฟีเจอร์ ... ไม่พร้อมใช้งาน ปัญหานี้อาจเกิดขึ้นเมื่อ AICore ดาวน์โหลดการกำหนดค่าล่าสุดไม่เสร็จ เมื่ออุปกรณ์เชื่อมต่อกับอินเทอร์เน็ต โดยปกติแล้วระบบจะใช้เวลา 2-3 นาทีถึง 2-3 ชั่วโมงในการอัปเดต การรีสตาร์ทอุปกรณ์จะช่วยให้อัปเดตได้เร็วขึ้น

โปรดทราบว่าหากปลดล็อก Bootloader ของอุปกรณ์ คุณจะเห็นข้อผิดพลาดนี้ด้วย เนื่องจาก API นี้ไม่รองรับอุปกรณ์ที่มี Bootloader ที่ปลดล็อก
AICore ล้มเหลวโดยมีข้อผิดพลาดประเภท 1-DOWNLOAD_ERROR และรหัสข้อผิดพลาด 0-UNKNOWN: ฟีเจอร์ ... ล้มเหลวโดยมีสถานะความล้มเหลว 0 และข้อผิดพลาด esz: UNAVAILABLE: แก้ไขโฮสต์ ... ไม่ได้ เชื่อมต่อเครือข่ายไว้ รอสักครู่ แล้วลองอีกครั้ง