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 ऐप्लिकेशन को रीसेट किया गया हो. जैसे, डेटा मिटाना, अनइंस्टॉल करना, और फिर से इंस्टॉल करना. इस वजह से, 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 ने नए कॉन्फ़िगरेशन डाउनलोड न किए हों. डिवाइस के इंटरनेट से कनेक्ट होने पर, अपडेट होने में कुछ मिनट से लेकर कुछ घंटे तक लग सकते हैं. डिवाइस को रीस्टार्ट करने से, अपडेट की प्रोसेस तेज़ हो सकती है.

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