فهم بيئة المستخدم على حزمة تطوير البرامج (SDK) لنظام التشغيل Android (Kotlin/Java)

تعرّف على كيفية استخدام Scene Semantics API في تطبيقاتك الخاصة.

تتيح Scene Semantics API للمطوّرين فهم المشهد المحيط بالمستخدم من خلال توفير معلومات دلالية في الوقت الفعلي مستندة إلى نموذج تعلُّم الآلة. عند توفّر صورة لمشهد خارجي، تعرض واجهة برمجة التطبيقات تصنيفًا لكل وحدة بكسل على مستوى مجموعة من الفئات الدلالية المفيدة، مثل السماء والمبنى والشجرة والطريق والرصيف والمركبة والشخص والمزيد. بالإضافة إلى تصنيفات وحدات البكسل، توفر واجهة برمجة التطبيقات Scene Semantics API أيضًا قيم ثقة لكل تصنيف بكسل وطريقة سهلة الاستخدام للاستعلام عن انتشار تسمية معينة في مشهد خارجي.

من اليسار إلى اليمين، أمثلة لصورة إدخال، والصورة الدلالية لتصنيفات البكسل، وصورة الثقة المقابلة لها:

مثال على صورة الإدخال والصورة الدلالية وصورة الثقة الدلالية

المتطلبات الأساسية

قبل المتابعة، تأكد من فهم مفاهيم الواقع المعزّز الأساسية وكيفية ضبط جلسة ARCore.

تفعيل دلالات المشهد

في جلسة ARCore جديدة، تحقَّق مما إذا كان جهاز المستخدم متوافقًا مع Scene Semantics API. لا تتوافق بعض الأجهزة المتوافقة مع ARCore مع Scene Semantics API بسبب قيود معالجة البيانات.

لحفظ الموارد، يتم إيقاف Scene Semantics تلقائيًا على ARCore. يمكنك تفعيل الوضع الدلالي لكي يستخدم تطبيقك واجهة برمجة تطبيقات Scene Semantics.

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)

الحصول على الصورة الدلالية

بعد تمكين "Sene Semantics"، يمكن استرداد الصورة الدلالية. الصورة الدلالية هي صورة 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.
}

ينبغي أن تكون الصور الدلالية للإخراج متاحة بعد حوالي 1 إلى 3 لقطات من بداية الجلسة، حسب الجهاز.

الحصول على صورة الثقة

بالإضافة إلى الصورة الدلالية التي توفّر تصنيفًا لكل بكسل، توفّر واجهة برمجة التطبيقات أيضًا صورة ثقة لقيم ثقة وحدات البكسل المقابلة. صورة الثقة هي صورة 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.
}

يجب أن تتوفّر صور ثقة في الإخراج بعد إطار واحد إلى 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.
}