درک محیط کاربر در Android SDK (Kotlin/Java)

یاد بگیرید که چگونه از Scene Semantics API در برنامه های خود استفاده کنید.

Scene Semantics API به توسعه دهندگان این امکان را می دهد که صحنه اطراف کاربر را با ارائه اطلاعات معنایی زمان واقعی مبتنی بر مدل ML درک کنند. با توجه به تصویری از یک صحنه در فضای باز، API یک برچسب برای هر پیکسل در مجموعه ای از کلاس های معنایی مفید، مانند آسمان، ساختمان، درخت، جاده، پیاده رو، وسیله نقلیه، شخص و موارد دیگر برمی گرداند. علاوه بر برچسب‌های پیکسل، Scene Semantics API همچنین مقادیر اطمینان را برای هر برچسب پیکسل و روشی آسان برای پرس و جو در مورد شیوع یک برچسب معین در یک صحنه در فضای باز ارائه می‌دهد.

از چپ به راست، نمونه‌هایی از یک تصویر ورودی، تصویر معنایی برچسب‌های پیکسل و تصویر اطمینان مربوطه:

نمونه ای از تصویر ورودی، تصویر معنایی و تصویر اطمینان معنایی.

پیش نیازها

قبل از ادامه، مطمئن شوید که مفاهیم اساسی AR و نحوه پیکربندی یک جلسه ARCore را درک کرده اید.

Scene Semantics را فعال کنید

در جلسه ARCore جدید ، بررسی کنید که آیا دستگاه کاربر از Scene Semantics API پشتیبانی می‌کند یا خیر. همه دستگاه‌های سازگار با ARCore از Scene Semantics API به دلیل محدودیت‌های قدرت پردازش پشتیبانی نمی‌کنند.

برای ذخیره منابع، Scene Semantics به طور پیش فرض در ARCore غیرفعال است. حالت معنایی را فعال کنید تا برنامه شما از Scene Semantics API استفاده کند.

جاوا

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);

کاتلین

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)

تصویر معنایی را بدست آورید

هنگامی که Semantics صحنه فعال شد، تصویر معنایی را می توان بازیابی کرد. تصویر معنایی یک تصویر ImageFormat.Y8 است که در آن هر پیکسل مربوط به یک برچسب معنایی تعریف شده توسط SemanticLabel است.

برای بدست آوردن تصویر معنایی Frame.acquireSemanticImage() استفاده کنید:

جاوا

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

کاتلین

// 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() استفاده کنید:

جاوا

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

کاتلین

// 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() برای بدست آوردن کسری برای یک برچسب معین استفاده کنید:

جاوا

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

کاتلین

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