باستخدام واجهة برمجة التطبيقات "وصف الصور من الذكاء الاصطناعي التوليدي" في ML Kit، يمكنك إنشاء أوصاف قصيرة للصور. يمكن أن يكون هذا مفيدًا في حالات الاستخدام التالية:
- إنشاء عناوين للصور
- إنشاء نص بديل لمساعدة المستخدمين الذين يعانون ضعفًا في البصر على فهم محتوى الصور بشكل أفضل
- استخدام الأوصاف التي تم إنشاؤها كبيانات وصفية لمساعدة المستخدمين في البحث عن الصور أو تنظيمها
- استخدام أوصاف قصيرة للصور عندما لا يتمكّن المستخدم من النظر إلى الشاشة، مثلاً أثناء القيادة أو الاستماع إلى بودكاست
الإمكانات الرئيسية
- عرض وصف موجز لصورة إدخال
أمثلة النتائج
الإدخال | الناتج |
![]() |
روبوت 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 مع اللغة الإنجليزية، وسيتم توفير دعم للغات إضافية في المستقبل. تعرض واجهة برمجة التطبيقات وصفًا موجزًا واحدًا للصورة.
قد يختلف مدى توفّر إعدادات الميزة المحدّدة (المحدّدة بواسطة
ImageDescriberOptions
) حسب إعدادات الجهاز المحدّد والطُرز التي تم تنزيلها على الجهاز.
الطريقة الأكثر موثوقية التي يمكن للمطوّرين اتّباعها لضمان توفّر ميزة واجهة برمجة التطبيقات المطلوبة على جهاز يتضمّن ImageDescriberOptions
المطلوب هي استدعاء الطريقة checkFeatureStatus()
. توفّر هذه الطريقة الحالة النهائية لتوفّر الميزة على الجهاز في وقت التشغيل.
مشاكل الإعداد الشائعة
تعتمد واجهات برمجة التطبيقات المستنِدة إلى الذكاء الاصطناعي التوليدي في ML Kit على تطبيق Android AICore للوصول إلى Gemini Nano. عند إعداد جهاز للتو (بما في ذلك إعادة الضبط)، أو إعادة ضبط تطبيق AICore للتو (مثل محو البيانات، وإلغاء التثبيت ثم إعادة التثبيت)، قد لا يتوفّر لتطبيق AICore وقت كافٍ لإنهاء عملية التهيئة (بما في ذلك تنزيل أحدث الإعدادات من الخادم). نتيجةً لذلك، قد لا تعمل واجهات برمجة التطبيقات المستندة إلى الذكاء الاصطناعي التوليدي في ML Kit على النحو المتوقّع. في ما يلي رسائل الخطأ الشائعة التي قد تظهر لك أثناء عملية الإعداد وكيفية التعامل معها:
مثال على رسالة خطأ | كيفية التعامل معها |
تعذّر تنفيذ AICore بسبب نوع الخطأ 4-CONNECTION_ERROR ورمز الخطأ 601-BINDING_FAILURE: تعذّر ربط خدمة AICore. | قد يحدث ذلك عند تثبيت التطبيق باستخدام واجهات برمجة تطبيقات GenAI في ML Kit بعد إعداد الجهاز مباشرةً أو عند إلغاء تثبيت AICore بعد تثبيت تطبيقك. يجب أن يؤدي تحديث تطبيق AICore ثم إعادة تثبيت تطبيقك إلى حلّ المشكلة. |
تعذّر تنفيذ AICore بسبب الخطأ من النوع 3-PREPARATION_ERROR ورمز الخطأ 606-FEATURE_NOT_FOUND: الميزة ... غير متاحة. |
قد يحدث ذلك عندما لا ينتهي تطبيق AICore من تنزيل أحدث الإعدادات. عندما يكون الجهاز متصلاً بالإنترنت، يستغرق التحديث عادةً بضع دقائق إلى بضع ساعات. يمكن أن تؤدي إعادة تشغيل الجهاز إلى تسريع عملية التحديث.
يُرجى العِلم أنّه في حال تم إلغاء قفل أداة تحميل التشغيل على الجهاز، سيظهر لك هذا الخطأ أيضًا، لأنّ واجهة برمجة التطبيقات هذه لا تتوافق مع الأجهزة التي تم إلغاء قفل أداة تحميل التشغيل عليها. |
تعذّر تنفيذ AICore بسبب نوع الخطأ 1-DOWNLOAD_ERROR ورمز الخطأ 0-UNKNOWN: تعذّر تنفيذ الميزة ... مع حالة التعذّر 0 والخطأ esz: UNAVAILABLE: يتعذّر تحليل المضيف ... | يُرجى الحفاظ على الاتصال بالشبكة والانتظار بضع دقائق ثم إعادة المحاولة. |