ทำความเข้าใจสภาพแวดล้อมของผู้ใช้ใน 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 เนื่องจากข้อจำกัดด้านพลังงานในการประมวลผล

เพื่อประหยัดทรัพยากร Scene Semantics จะปิดใช้โดยค่าเริ่มต้นใน ARCore เปิดใช้โหมดการสื่อความหมายเพื่อให้แอปของคุณใช้ Scene 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.
}