TensorFlow.js: 小程序加载posenet,score一直是0.03,是什么原因呢?

大家有看过这个视频吗?


效果是这样的:

我跟着做的,score 总是为0.03,请问是什么原因呢?

源码:

    const tf = require('@tensorflow/tfjs-core')
    const posenet = require('@tensorflow-models/posenet')
    const regeneratorRuntime = require('regenerator-runtime')
    //index.js

    Page({

    async onReady() {
    const camera = wx.createCameraContext(this)
    this.canvas = wx.createCanvasContext('pose', this)
    this.loadPosenet()
    let count = 0
    const listener = camera.onCameraFrame((frame) => {
      count++
      if (count === 4) {
        if (this.net) {
          this.drawPose(frame)
        }
        count = 0
      }
    })
    listener.start()
  },

   async loadPosenet() {
    this.net = await posenet.load({
      architecture: 'MobileNetV1',
      outputStride: 16,
      inputResolution:{ width: 640, height: 480 },
      multiplier: 0.5,
      modelUrl:'https://storage.googleapis.com/tfjs-models/savedmodel/posenet/mobilenet/quant2/075/model-stride16.json'

    })
    console.log(this.net)
  },

  async detectPose(frame, net) {
    const imgData = {data: new Uint8Array(frame.data), width: frame.width, height: frame.height}
    const imgSlice = tf.tidy(() => {
      const imgTensor = tf.browser.fromPixels(imgData, 4)
      return imgTensor.slice([0, 0, 0], [-1, -1, 3])
    })

    const pose = await net.estimateSinglePose(imgSlice, {flipHorizontal: false})
    imgSlice.dispose()

    return pose

  },

  async drawPose(frame) {
    const pose = await this.detectPose(frame, this.net)

    if (pose == null || this.canvas == null) return
    console.log(pose)

    if (pose.score >= 0.1) {
      // Draw circles

      for (i in pose.keypoints) {
        const point = pose.keypoints[i]

        if (point.score >= 0.5) {
          const {y, x} = point.position
          this.drawCircle(this.canvas, x, y)

        }

      }

      // Draw lines

      const adjacentKeyPoints = posenet.getAdjacentKeyPoints(pose.keypoints, 0.3)
      for (i in adjacentKeyPoints) {
        const points = adjacentKeyPoints[i]
        this.drawLine(this.canvas, points[0].position, points[1].position)
      }

      this.canvas.draw()
    }
  },

  drawCircle(canvas, x, y) {
    canvas.beginPath()
    canvas.arc(x * 0.72, y * 0.72, 3, 0, 2 * Math.PI)
    canvas.fillStyle = 'aqua'
    canvas.fill()

  },

  drawLine(canvas, pos0, pos1) {
    canvas.beginPath()
    canvas.moveTo(pos0.x * 0.72, pos0.y * 0.72)
    canvas.lineTo(pos1.x * 0.72, pos1.y * 0.72)
    canvas.lineWidth = 2
    canvas.strokeStyle = 'aqua'
    canvas.stroke()
  }
})

帮你改了一下代码格式,再 at 一下动图里出现的人 @huan 来帮你看看问题?

谢谢,这个案例真是太香了,太吸引人了, 提高了人机交互的能力

下面是 tf.wiki TFJS 章节的 Repo,里面有较全面的 TFJS 相关代码和介绍,你可以先系统的多看一些:

和你正在做的 postnet ,类似的例子有一个 mobilenet 物体识别,你可以在这里直接使用,源码在这里

你的 posenet 代码,建议可以基于上面的代码修改一下在电脑 chrome 上先跑起来调试,因为这样比较方便查找问题。

1 Like

嗯,好的,谢谢,我先看看这些

赞,谢谢 Huan 的回复和解答!