ทำความเข้าใจสภาพแวดล้อมของผู้ใช้ใน 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.
}