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

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

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

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

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

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

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

सीन सिमैंटिक चालू करें

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

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

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.
}

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

सिमैंटिक लेबल के लिए पिक्सल के अंश की क्वेरी करें

आपके पास मौजूदा फ़्रेम में, आकाश जैसी किसी खास क्लास से जुड़े पिक्सल के हिस्से के बारे में क्वेरी करने का विकल्प भी है. सिमैंटिक इमेज दिखाने और किसी खास लेबल के लिए पिक्सल के हिसाब से खोज करने की तुलना में, यह क्वेरी ज़्यादा असरदार है. लौटाया गया अंश, [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.
}