ทำความเข้าใจสภาพแวดล้อมของผู้ใช้ใน Android SDK (Kotlin/Java)

ดูวิธีใช้ Scene Semantics API ในแอปของคุณเอง

Scene Semantics API ช่วยให้นักพัฒนาซอฟต์แวร์เข้าใจฉากรอบตัวของผู้ใช้ได้ โดยให้ข้อมูลเชิงความหมายแบบเรียลไทม์ที่อิงตามโมเดล ML เมื่อได้รับรูปภาพฉากกลางแจ้ง API จะแสดงผลป้ายกำกับสำหรับแต่ละพิกเซลในชุดคลาสเชิงอรรถศาสตร์ที่มีประโยชน์ เช่น ท้องฟ้า อาคาร ต้นไม้ ถนน ทางเท้า ยานพาหนะ บุคคล และอื่นๆ นอกจากป้ายกำกับพิกเซลแล้ว Scene Semantics API ยังมีค่าความเชื่อมั่นสำหรับป้ายกำกับพิกเซลแต่ละรายการ และวิธีค้นหาความถี่ของป้ายกำกับหนึ่งๆ ในฉากกลางแจ้งที่ใช้งานง่าย

จากซ้ายไปขวาคือตัวอย่างรูปภาพอินพุต รูปภาพเชิงอรรถของป้ายกำกับพิกเซล และรูปภาพความเชื่อมั่นที่เกี่ยวข้อง

ตัวอย่างรูปภาพอินพุต รูปภาพเชิงความหมาย และรูปภาพความเชื่อมั่นเชิงความหมาย

ข้อกำหนดเบื้องต้น

โปรดทำความเข้าใจแนวคิดพื้นฐานของ AR และวิธีกำหนดค่าเซสชัน ARCore ก่อนดำเนินการต่อ

เปิดใช้ความหมายของฉาก

ในเซสชัน ARCore ใหม่ ให้ตรวจสอบว่าอุปกรณ์ของผู้ใช้รองรับ Scene Semantics API หรือไม่ อุปกรณ์ที่เข้ากันได้กับ ARCore บางรุ่นไม่รองรับ Scene Semantics API เนื่องจากข้อจำกัดด้านกำลังการประมวลผล

ระบบจะปิดใช้ความหมายของฉากใน ARCore โดยค่าเริ่มต้นเพื่อประหยัดทรัพยากร เปิดใช้โหมดเชิงความหมายเพื่อให้แอปใช้ Scene Semantics API

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

รับรูปภาพที่สื่อความหมาย

เมื่อเปิดใช้ความหมายของฉากแล้ว คุณจะเรียกข้อมูลรูปภาพเชิงความหมายได้ รูปภาพเชิงความหมายคือรูปภาพ ImageFormat.Y8 โดยแต่ละพิกเซลจะสอดคล้องกับป้ายกำกับเชิงความหมายที่ SemanticLabel กำหนด

ใช้ Frame.acquireSemanticImage() เพื่อรับรูปภาพที่สื่อความหมาย

JavaKotlin
// 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() เพื่อรับรูปภาพความเชื่อมั่นเชิงความหมาย

JavaKotlin
// 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 เฟรมนับจากเริ่มเซสชัน ทั้งนี้ขึ้นอยู่กับอุปกรณ์

ค้นหาเศษส่วนของพิกเซลสําหรับป้ายกํากับที่สื่อความหมาย

นอกจากนี้ คุณยังค้นหาเศษส่วนของพิกเซลในเฟรมปัจจุบันซึ่งอยู่ในคลาสหนึ่งๆ เช่น ท้องฟ้า ได้ด้วย การค้นหานี้มีประสิทธิภาพมากกว่าการแสดงผลรูปภาพเชิงอรรถศาสตร์และทำการค้นหาแบบพิกเซลสำหรับป้ายกำกับที่เฉพาะเจาะจง เศษส่วนที่แสดงผลคือค่า float ในช่วง [0.0, 1.0]

ใช้ Frame.getSemanticLabelFraction() เพื่อรับเศษส่วนของป้ายกำกับหนึ่งๆ

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