GenAI Image Description API

ML Kit के GenAI Image Description API की मदद से, इमेज के बारे में कम शब्दों में जानकारी जनरेट की जा सकती है. इन मामलों में यह मददगार हो सकता है:

  • इमेज के टाइटल जनरेट करना
  • वैकल्पिक टेक्स्ट (ऑल्ट टेक्स्ट) जनरेट करना, ताकि दृष्टिबाधित उपयोगकर्ताओं को इमेज के कॉन्टेंट को बेहतर तरीके से समझने में मदद मिल सके
  • जनरेट किए गए ब्यौरे को मेटाडेटा के तौर पर इस्तेमाल करना, ताकि उपयोगकर्ता इमेज खोज सकें या उन्हें व्यवस्थित कर सकें
  • जब उपयोगकर्ता अपनी स्क्रीन पर नज़र न डाल पा रहा हो, तब इमेज के बारे में कम शब्दों में जानकारी देना. जैसे, जब वह गाड़ी चला रहा हो या पॉडकास्ट सुन रहा हो

मुख्य सुविधाएं

  • किसी इनपुट इमेज के बारे में कम शब्दों में जानकारी दिखाना

परिणामों के उदाहरण

इनपुट आउटपुट
काले रंग की सतह पर, कैक्टस जैसे डिज़ाइन वाला छोटा और हरा 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();

इस्तेमाल की जा सकने वाली सुविधाएं और सीमाएं

GenAI Image Description API, अंग्रेज़ी भाषा के साथ काम करता है. आने वाले समय में, इसमें और भाषाओं के लिए भी सहायता जोड़ी जाएगी. एपीआई, इमेज के बारे में कम शब्दों में एक जानकारी दिखाता है.

किसी खास सुविधा के कॉन्फ़िगरेशन (ImageDescriberOptions से तय) की उपलब्धता, डिवाइस के कॉन्फ़िगरेशन और डिवाइस पर डाउनलोड किए गए मॉडल के हिसाब से अलग-अलग हो सकती है.

डेवलपर के लिए, यह पक्का करने का सबसे भरोसेमंद तरीका है कि जिस एपीआई सुविधा का इस्तेमाल करना है वह ImageDescriberOptions के साथ काम करने वाले डिवाइस पर काम करती है या नहीं. इसके लिए, checkFeatureStatus() तरीके को कॉल करें. इस तरीके से, डिवाइस पर सुविधा की उपलब्धता की सटीक स्थिति, रनटाइम के दौरान पता चलती है.

सेटअप से जुड़ी सामान्य समस्याएं

ML Kit GenAI API, Gemini Nano को ऐक्सेस करने के लिए Android AICore ऐप्लिकेशन पर निर्भर करते हैं. जब किसी डिवाइस को हाल ही में सेट अप किया गया हो (इसमें रीसेट करना भी शामिल है) या AICore ऐप्लिकेशन को हाल ही में रीसेट किया गया हो (जैसे, डेटा मिटाना, अनइंस्टॉल करना, और फिर से इंस्टॉल करना), तो हो सकता है कि AICore ऐप्लिकेशन को शुरू करने में ज़रूरत के मुताबिक समय न मिले. इसमें, सर्वर से नए कॉन्फ़िगरेशन डाउनलोड करना भी शामिल है. इस वजह से, हो सकता है कि ML Kit GenAI API उम्मीद के मुताबिक काम न करें. यहां सेटअप से जुड़ी गड़बड़ी के कुछ सामान्य मैसेज और उन्हें ठीक करने का तरीका बताया गया है:

गड़बड़ी के मैसेज का उदाहरण कैसे मैनेज करें
गड़बड़ी का टाइप 4-CONNECTION_ERROR और गड़बड़ी कोड 601-BINDING_FAILURE की वजह से, AICore काम नहीं कर सका: AICore सेवा बाइंड नहीं हो सकी. ऐसा तब हो सकता है, जब डिवाइस सेटअप करने के तुरंत बाद, ML Kit GenAI API का इस्तेमाल करके ऐप्लिकेशन इंस्टॉल किया जाए या आपके ऐप्लिकेशन के इंस्टॉल होने के बाद, AICore को अनइंस्टॉल किया जाए. AICore ऐप्लिकेशन को अपडेट करने और फिर से अपना ऐप्लिकेशन इंस्टॉल करने से, यह समस्या ठीक हो जाएगी.
AICore, गड़बड़ी टाइप 3-PREPARATION_ERROR और गड़बड़ी कोड 606-FEATURE_NOT_FOUND की वजह से काम नहीं कर सका: सुविधा ... उपलब्ध नहीं है. ऐसा तब हो सकता है, जब AICore ने नए कॉन्फ़िगरेशन डाउनलोड करना पूरा न किया हो. नेटवर्क से कनेक्ट रहें और कुछ मिनट से लेकर कुछ घंटे तक इंतज़ार करें.

ध्यान दें कि अगर डिवाइस का बूटलोडर अनलॉक है, तो आपको यह गड़बड़ी भी दिखेगी—यह एपीआई, अनलॉक किए गए बूटलोडर वाले डिवाइसों पर काम नहीं करता.
AICore, गड़बड़ी के टाइप 1-DOWNLOAD_ERROR और गड़बड़ी कोड 0-UNKNOWN की वजह से काम नहीं कर सका: सुविधा ... काम नहीं कर रही है. गड़बड़ी की स्थिति 0 और गड़बड़ी esz: UNAVAILABLE: होस्ट को हल नहीं किया जा सका ... इंटरनेट कनेक्शन चालू रखें, कुछ मिनट इंतज़ार करें, और फिर से कोशिश करें.