姿态识别

姿态识别以开源的方式提供,将类库和文件,拷贝到工程中,如下图所示,类库下载地址:https://pan.baidu.com/s/1IQ_GPdBR8ALtQhJPDYgCrw,提取码:losl。

图:“姿态识别“添加类库示意
图:“姿态识别“添加代码文件示意

在Camera2BasicFragment.kt文件的classifyFrame()方法中,通过classifier!!.classifyFrame()获取识别的姿态,并根据姿态进行应用,如地图操作。下方是根据识别的姿态进行地图缩放和平移操作的示例代码。

private fun classifyFrame() {
  if (classifier == null || activity == null || cameraDevice == null) {
    return;
  }
  val bitmap = textureView!!.getBitmap(classifier!!.imageSizeX, classifier!!.imageSizeY)
  val textToShow = classifier!!.classifyFrame(bitmap)
  bitmap.recycle()
  drawView!!.setDrawPoint(classifier!!.mPrintPointArray!!, 0.5f)
  if(currentTime == 0L || lastTime == 0L)
  {
    currentTime = System.currentTimeMillis()
    lastTime = currentTime
    return
  }
  if (operMode == 0)
  {
    currentTime = System.currentTimeMillis();
    if (currentTime-lastTime>1000)
    {
      var minusTmp:Float = classifier!!.mPrintPointArray!![0][7]-classifier!!.mPrintPointArray!![0][4]
      if (minusTmp!=0.0f) {
        if (minusTmp > 50) {
          m_mapControl.map.zoom(1.1)
          m_mapControl.map.refresh()
        }
        if (minusTmp < 20) {
          m_mapControl.map.zoom(0.9)
          m_mapControl.map.refresh()
        }
      }
      lastTime = currentTime;
    }
  }
  if(operMode == 1)
  {
    var leftX = classifier!!.mPrintPointArray!![0][4]
    var leftY = classifier!!.mPrintPointArray!![1][4]
    if (lastLeftPointX2D == null) {
      lastLeftPointX2D = PointF(leftX, leftY);
    } else {
      //左手控制左移和上移
      val absLeftDifx: Float = Math.abs(leftX - lastLeftPointX2D!!.x)
      val absLeftDify: Float = Math.abs(leftY - lastLeftPointX2D!!.y)
      var ptLeftCenter = m_mapControl?.map?.center
      var ptNewLeftCenter: Point2D = Point2D(ptLeftCenter)
      if (absLeftDifx>10||absLeftDify>10)
      {
        lastLeftPointX2D = PointF(leftX, leftY);
        return
      }
      if (absLeftDifx > threahold) {
        if (leftX > lastLeftPointX2D!!.x) {
          ptNewLeftCenter.setX(ptLeftCenter!!.x - absLeftDifx * 5000)
        } else {
          ptNewLeftCenter.setX(ptLeftCenter!!.x + absLeftDifx * 5000)
        }
      }
      if (absLeftDify > threahold) {
        if (leftY > lastLeftPointX2D!!.y) {
          ptNewLeftCenter.setY(ptLeftCenter!!.y + absLeftDify * 5000)
        } else {
          ptNewLeftCenter.setY(ptLeftCenter!!.y - absLeftDify * 5000)
        }
      }
      if (absLeftDifx > threahold || absLeftDify > threahold) {
        m_mapControl?.map?.setCenter(ptNewLeftCenter)
        m_mapControl?.map?.refresh()
        lastLeftPointX2D = PointF(leftX, leftY);
      }
    }
  }
}

在DrawView.kt文件中,定义了点与身体部位的对应关系,可以根据需要设置点的颜色。

private val mColorArray = intArrayOf(
  resources.getColor(R.color.color_top, null),
  resources.getColor(R.color.color_neck, null),
  resources.getColor(R.color.color_l_shoulder, null),
  resources.getColor(R.color.color_l_elbow, null),
  resources.getColor(R.color.color_l_wrist, null),
  resources.getColor(R.color.color_r_shoulder, null),
  resources.getColor(R.color.color_r_elbow, null),
  resources.getColor(R.color.color_r_wrist, null),
  resources.getColor(R.color.color_l_hip, null),
  resources.getColor(R.color.color_l_knee, null),
  resources.getColor(R.color.color_l_ankle, null),
  resources.getColor(R.color.color_r_hip, null),
  resources.getColor(R.color.color_r_knee, null),
  resources.getColor(R.color.color_r_ankle, null),
  resources.getColor(R.color.color_background, null)
)
图:姿态识别

以上示例代码下载地址:https://pan.baidu.com/s/1IQ_GPdBR8ALtQhJPDYgCrw,提取码:losl。