Android SDK (Kotlin/Java) पर उपयोगकर्ता के एनवायरमेंट को समझना

अपने ऐप्लिकेशन में Scene Semantics API का इस्तेमाल करने का तरीका जानें.

सीन सेमेटिक्स एपीआई की मदद से, डेवलपर उपयोगकर्ता के आस-पास के सीन को समझ सकते हैं. इसके लिए, यह एपीआई मशीन लर्निंग मॉडल के आधार पर, रीयल-टाइम में सेमेटिक जानकारी उपलब्ध कराता है. किसी आउटडोर सीन की इमेज देने पर, एपीआई हर पिक्सल के लिए एक लेबल दिखाता है. यह लेबल, काम की सेमैटिक क्लास के सेट में होता है. जैसे, आसमान, इमारत, पेड़, सड़क, फ़ुटपाथ, वाहन, व्यक्ति वगैरह. पिक्सल लेबल के अलावा, सीन सेमेटिक्स एपीआई हर पिक्सल लेबल के लिए कॉन्फ़िडेंस वैल्यू भी उपलब्ध कराता है. साथ ही, किसी आउटडोर सीन में किसी लेबल की मौजूदगी के बारे में आसानी से क्वेरी करने का तरीका भी उपलब्ध कराता है.

बाईं से दाईं ओर, इनपुट इमेज, पिक्सल लेबल की सेमैनटिक इमेज, और उससे जुड़ी कॉन्फ़िडेंस इमेज के उदाहरण:

इनपुट इमेज, सिमेंटिक इमेज, और सिमेंटिक कॉन्फ़िडेंस इमेज का उदाहरण.

ज़रूरी शर्तें

आगे बढ़ने से पहले, पक्का करें कि आपने एआर के बुनियादी कॉन्सेप्ट और ARCore सेशन को कॉन्फ़िगर करने का तरीका समझ लिया हो.

सीन सेमेंटेक्स की सुविधा चालू करना

नए ARCore सेशन में, देखें कि उपयोगकर्ता के डिवाइस पर Scene Semantics API काम करता है या नहीं. प्रोसेसिंग पावर की सीमाओं की वजह से, ARCore के साथ काम करने वाले सभी डिवाइसों पर सीन सेमेटिक्स एपीआई काम नहीं करता.

संसाधनों को सेव करने के लिए, ARCore पर सीन सिमैंटिक्स की सुविधा डिफ़ॉल्ट रूप से बंद रहती है. अपने ऐप्लिकेशन में Scene Semantics API का इस्तेमाल करने के लिए, सेमेटिक मोड चालू करें.

Java

Config config = session.getConfig();

// Check whether the user's device supports the Scene Semantics API.
boolean isSceneSemanticsSupported =
    session.isSemanticModeSupported(Config.SemanticMode.ENABLED);
if (isSceneSemanticsSupported) {
  config.setSemanticMode(Config.SemanticMode.ENABLED);
}
session.configure(config);

Kotlin

val config = session.config

// Check whether the user's device supports the Scene Semantics API.
val isSceneSemanticsSupported = session.isSemanticModeSupported(Config.SemanticMode.ENABLED)
if (isSceneSemanticsSupported) {
  config.semanticMode = Config.SemanticMode.ENABLED
}
session.configure(config)

सिमेंटिक इमेज पाना

सीन सेमेटिक्स की सुविधा चालू होने के बाद, सिमैंटिक इमेज को वापस पाया जा सकता है. सिमेंटिक इमेज एक ImageFormat.Y8 इमेज होती है. इसमें हर पिक्सल, SemanticLabel से तय किए गए सिमेंटिक लेबल से जुड़ा होता है.

सिमेंटिक इमेज पाने के लिए, Frame.acquireSemanticImage() का इस्तेमाल करें:

Java

// Retrieve the semantic image for the current frame, if available.
try (Image semanticImage = frame.acquireSemanticImage()) {
  // Use the semantic image here.
} catch (NotYetAvailableException e) {
  // No semantic image retrieved for this frame.
  // The output image may be missing for the first couple frames before the model has had a
  // chance to run yet.
}

Kotlin

// Retrieve the semantic image for the current frame, if available.
try {
  frame.acquireSemanticImage().use { semanticImage ->
    // Use the semantic image here.
  }
} catch (e: NotYetAvailableException) {
  // No semantic image retrieved for this frame.
}

सेशन शुरू होने के एक से तीन फ़्रेम के बाद, सेमैनटिक इमेज दिखनी चाहिए. हालांकि, यह डिवाइस पर निर्भर करता है.

कॉन्फ़िडेंस इमेज पाना

एपीआई, हर पिक्सल के लिए लेबल देने वाली सेमेंटिक इमेज के साथ-साथ, पिक्सल की कॉन्फ़िडेंस वैल्यू की कॉन्फ़िडेंस इमेज भी उपलब्ध कराता है. कॉन्फ़िडेंस इमेज एक ImageFormat.Y8 इमेज होती है. इसमें हर पिक्सल, [0, 255] की रेंज में मौजूद किसी वैल्यू से जुड़ा होता है. यह वैल्यू, हर पिक्सल के लिए सेमैनटिक लेबल से जुड़ी संभावना से जुड़ी होती है.

सिमेंटिक कॉन्फ़िडेंस इमेज पाने के लिए, Frame.acquireSemanticConfidenceImage() का इस्तेमाल करें:

Java

// Retrieve the semantic confidence image for the current frame, if available.
try (Image semanticImage = frame.acquireSemanticConfidenceImage()) {
  // Use the semantic confidence image here.
} catch (NotYetAvailableException e) {
  // No semantic confidence image retrieved for this frame.
  // The output image may be missing for the first couple frames before the model has had a
  // chance to run yet.
}

Kotlin

// Retrieve the semantic confidence image for the current frame, if available.
try {
  frame.acquireSemanticConfidenceImage().use { semanticConfidenceImage ->
    // Use the semantic confidence image here.
  }
} catch (e: NotYetAvailableException) {
  // No semantic confidence image retrieved for this frame.
}

डिवाइस के हिसाब से, सेशन शुरू होने के लगभग 1 से 3 फ़्रेम के बाद, कॉन्फ़िडेंस इमेज दिखनी चाहिए.

किसी सिमेंटिक लेबल के लिए पिक्सल के हिस्से की क्वेरी करना

मौजूदा फ़्रेम में, किसी खास क्लास (जैसे, आसमान) से जुड़े पिक्सल के हिस्से के बारे में भी क्वेरी की जा सकती है. यह क्वेरी, किसी खास लेबल के लिए पिक्सल के हिसाब से खोज करने और सेमैनटिक इमेज दिखाने से ज़्यादा असरदार है. फ़ंक्शन से मिली भिन्न, [0.0, 1.0] की रेंज में एक फ़्लोट वैल्यू होती है.

किसी लेबल के लिए फ़्रैक्शन पाने के लिए, Frame.getSemanticLabelFraction() का इस्तेमाल करें:

Java

// Retrieve the fraction of pixels for the semantic label sky in the current frame.
try {
  float outFraction = frame.getSemanticLabelFraction(SemanticLabel.SKY);
  // Use the semantic label fraction here.
} catch (NotYetAvailableException e) {
  // No fraction of semantic labels was retrieved for this frame.
}

Kotlin

// Retrieve the fraction of pixels for the semantic label sky in the current frame.
try {
  val fraction = frame.getSemanticLabelFraction(SemanticLabel.SKY)
  // Use the semantic label fraction here.
} catch (e: NotYetAvailableException) {
  // No fraction of semantic labels was retrieved for this frame.
}