You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助摄像头(LiDAR Scanner)实现人体距离与方向检测?

人体距离与方向检测实现方案

方案1:ARBodyTracking + ARWorldTracking(LiDAR增强)

ARBodyTrackingConfiguration负责识别人体3D骨架,结合ARWorldTracking的空间定位能力(LiDAR提供高精度深度),可直接从骨架节点的世界坐标计算距离与方向。

  • 实现步骤:
    1. 初始化配置时,同时启用场景重建(若设备支持LiDAR):
      let configuration = ARBodyTrackingConfiguration()
      if ARWorldTrackingConfiguration.supportsSceneReconstruction(.meshWithClassification) {
          configuration.sceneReconstruction = .meshWithClassification
      }
      session.run(configuration)
      
    2. 在ARSessionDelegate的更新回调中,提取ARBodyAnchor的骨架坐标:
      func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
          for anchor in anchors {
              guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
              // 获取人体根节点(胸部位置)的世界坐标
              let bodyWorldPos = bodyAnchor.transform.columns.3
              // 获取相机的世界坐标
              guard let cameraPos = session.currentFrame?.camera.transform.columns.3 else { return }
              
              // 计算相机与人体的直线距离(单位:米)
              let distance = simd_distance(
                  simd_float3(bodyWorldPos.x, bodyWorldPos.y, bodyWorldPos.z),
                  simd_float3(cameraPos.x, cameraPos.y, cameraPos.z)
              )
              print("人体距离:\(String(format: "%.2f", distance))米")
              
              // 计算方向向量(从相机指向人体)
              let direction = simd_normalize(
                  simd_float3(bodyWorldPos.x - cameraPos.x,
                              bodyWorldPos.y - cameraPos.y,
                              bodyWorldPos.z - cameraPos.z)
              )
              print("人体方向:x:\(direction.x) y:\(direction.y) z:\(direction.z)")
          }
      }
      

方案2:深度图像 + 人体检测(兼容无BodyTracking设备)

如果设备不支持ARBodyTracking,可先用Core ML模型(如YOLOv8、MobileNetV2)检测图像中的人体区域,再结合LiDAR/单目深度数据计算距离。

  • 实现步骤:
    1. 开启ARSession的深度数据采集:
      let configuration = ARWorldTrackingConfiguration()
      configuration.providesDepthData = true
      session.run(configuration)
      
    2. 检测人体区域后,提取对应深度数据的平均值:
      func session(_ session: ARSession, didUpdate frame: ARFrame) {
          guard let depthData = frame.depthData else { return }
          // 假设已通过Core ML得到人体的屏幕bounding box
          let humanScreenRect = CGRect(x: 80, y: 120, width: 240, height: 480)
          
          // 转换屏幕坐标到深度图像坐标系
          let depthImage = depthData.depthMap
          let depthResolution = CGSize(width: depthImage.width, height: depthImage.height)
          let imageResolution = frame.camera.imageResolution
          let depthRect = CGRect(
              x: humanScreenRect.origin.x * depthResolution.width / imageResolution.width,
              y: humanScreenRect.origin.y * depthResolution.height / imageResolution.height,
              width: humanScreenRect.width * depthResolution.width / imageResolution.width,
              height: humanScreenRect.height * depthResolution.height / imageResolution.height
          )
          
          // 计算区域内平均深度(距离)
          var totalDepth: Float = 0
          var validPixelCount = 0
          for y in Int(depthRect.minY)...Int(depthRect.maxY) {
              for x in Int(depthRect.minX)...Int(depthRect.maxX) {
                  guard x < depthImage.width, y < depthImage.height else { continue }
                  let depthValue = depthImage[y, x].depth
                  totalDepth += depthValue
                  validPixelCount += 1
              }
          }
          
          if validPixelCount > 0 {
              let averageDistance = totalDepth / Float(validPixelCount)
              print("人体平均距离:\(String(format: "%.2f", averageDistance))米")
          }
      }
      

核心注意事项

  • 设备兼容性:ARBodyTracking需要A12+芯片;LiDAR深度仅支持iPhone 12 Pro及以上、iPad Pro 2020及以上;单目深度(sceneDepth)支持iOS 15+的A12+设备。
  • 精度优化:优先使用LiDAR深度数据,精度远高于单目;选择人体胸部/根节点计算距离,比四肢位置更稳定。
  • 坐标系说明:ARKit采用右手坐标系,Z轴指向相机前方,计算时需确保向量维度统一(忽略齐次坐标的w分量)。

内容的提问来源于stack exchange,提问作者Hemendra Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 18:44:54