GenAI Image Description API ของ ML Kit ช่วยให้คุณสร้างคำอธิบายเนื้อหาสั้นๆ สำหรับรูปภาพได้ ซึ่งจะมีประโยชน์ใน Use Case ต่อไปนี้
- การสร้างชื่อรูปภาพ
- การสร้างข้อความแสดงแทน (ข้อความ alt) เพื่อช่วยผู้ใช้ที่มีความบกพร่องทางสายตาเข้าใจเนื้อหาของรูปภาพได้ดียิ่งขึ้น
- การใช้รายละเอียดที่สร้างขึ้นเป็นข้อมูลเมตาเพื่อช่วยให้ผู้ใช้ค้นหาหรือจัดระเบียบรูปภาพ
- การใช้คำอธิบายสั้นๆ ของรูปภาพเมื่อผู้ใช้ไม่สามารถมองไปที่หน้าจอ เช่น ขณะขับรถหรือฟังพอดแคสต์
ความสามารถหลัก
- แสดงคำอธิบายสั้นๆ สำหรับรูปภาพอินพุต
ตัวอย่างผลการแข่ง
อินพุต | เอาต์พุต |
![]() |
หุ่นยนต์ 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();
ฟีเจอร์ที่รองรับและข้อจํากัด
GenAI Image Description API รองรับภาษาอังกฤษ และจะรองรับภาษาอื่นๆ เพิ่มเติมในอนาคต API จะแสดงคำอธิบายสั้นๆ 1 รายการของรูปภาพ
ความพร้อมใช้งานของการกําหนดค่าฟีเจอร์ที่เฉพาะเจาะจง (ระบุโดย ImageDescriberOptions
) อาจแตกต่างกันไปตามการกําหนดค่าของอุปกรณ์หนึ่งๆ และรุ่นที่ดาวน์โหลดลงในอุปกรณ์
วิธีที่เชื่อถือได้มากที่สุดสำหรับนักพัฒนาแอปในการยืนยันว่าฟีเจอร์ API ที่ต้องการได้รับการรองรับในอุปกรณ์ที่มี ImageDescriberOptions
ที่ขอคือเรียกใช้เมธอด checkFeatureStatus()
วิธีนี้จะแสดงสถานะความพร้อมใช้งานของฟีเจอร์ในอุปกรณ์อย่างแน่ชัดขณะรันไทม์
ปัญหาการตั้งค่าที่พบได้ทั่วไป
ไม่รองรับ API นี้ML Kit GenAI API ต้องใช้แอป 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: ไม่สามารถแก้ไขโฮสต์ ... | เชื่อมต่อเครือข่ายไว้ รอ 2-3 นาที แล้วลองอีกครั้ง |