הבנת סביבת המשתמש ב-Android SDK (Kotlin/Java)

רוצים לדעת איך משתמשים ב-Scene Semantics API באפליקציות שלכם?

ה-Scemantics API מאפשר למפתחים להבין את הסצנה שמסביב למשתמש באמצעות מידע סמנטי בזמן אמת שמבוסס על מודל למידת מכונה. בהינתן תמונה של סצנה בחוץ, ה-API מחזיר תווית לכל פיקסל בקבוצה של סיווגים סמנטיים שימושיים, כגון שמיים, בניין, עץ, כביש, מדרכה, כלי רכב, אדם ועוד. בנוסף לתוויות פיקסל, ממשק ה-API 'סמנטיקה של סצינה' מציע גם ערכי מהימנות לכל תווית של פיקסל ודרך קלה לשימוש לשליחת שאילתות לגבי השכיחות של תווית נתונה בסצנה חיצונית.

משמאל לימין דוגמאות של תמונת קלט, התמונה הסמנטית של תוויות הפיקסלים ותמונת הסמך המתאימה:

דוגמה לתמונת קלט, תמונה סמנטית ותמונת מהימנות סמנטית.

דרישות מוקדמות

לפני שממשיכים, חשוב לוודא שאתם מבינים את המושגים הבסיסיים של AR ואת האופן שבו מגדירים סשן של ARCore.

הפעלת סמנטיקה של סצנות

בסשן ARCore חדש, צריך לבדוק אם מכשיר של משתמש תומך ב-Scemantics API. לא כל המכשירים שתואמים ל-ARCore תומכים ב-Scemantics API בגלל אילוצי כוח עיבוד.

כדי לחסוך במשאבים, התכונה 'סמנטיקה' מושבתת כברירת מחדל ב-ARCore. צריך להפעיל מצב סמנטי כדי שהאפליקציה תשתמש ב-Scement 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.
}

תמונות סמנטיות של פלט צריכות להיות זמינות לאחר 1-3 פריימים מתחילת הסשן, בהתאם למכשיר.

משיגים את תמונת הביטחון

בנוסף לתמונה הסמנטית, שמספקת תווית לכל פיקסל, ה-API מספק גם תמונת מהימנות של ערכי המהימנות התואמים של הפיקסלים. תמונת המהימנות היא תמונה של 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.
}