Tensorfllow js 利用ssdlite mobilenetv2(含quantization)进行鞋检测

// Notice there is no 'import' statement. 'mobilenet' and 'tf' is
// available on the index-page because of the script tag above.

// Load the model.
(async () => {

  tf.setBackend('webgl');
  const model = await tf.loadGraphModel('quantized - lite/model.json');
  console.log('model loaded: ', model)
  const dummy =tf.zeros([1, 300,300,3])
  const dummyPrediction = await model.executeAsync({ image_tensor: dummy })
  dummy.dispose();
  for(let i = 0; i < dummyPrediction.length; i++) {
    dummyPrediction[i].dispose();
  }

  const video = document.getElementById("video");
  const canvas = document.getElementById("canvas");
  const status = document.getElementById("status");
  const context = canvas.getContext("2d");
  canvas.width = document.body.clientWidth; //document.width is obsolete
  canvas.height = document.body.clientHeight; //document.height is obsolete
  video.width = document.body.clientWidth; //document.width is obsolete
  video.height = document.body.clientHeight; //document.height is obsolete
  const stream = await navigator.mediaDevices.getUserMedia({
    audio: false,
    video: {
      facingMode: "environment"
    }
  });

  video.srcObject = stream;
  video.onloadeddata = function() {
    detect();
  }

  
  async function detect() {
    
    const tfImg = await tf.browser.fromPixels(video)
    const smallImg = tf.image.resizeBilinear(tfImg, [300, 300]) // 600, 450
    const tf4d = smallImg.expandDims()
    // const resized = tf.cast(smallImg, tf.float16)
    // const tf4d = tf.tensor4d(Array.from(resized.dataSync()), [1, 300, 300, 3]) // 600, 450
    const predictions = await model.executeAsync({ image_tensor: tf4d }, ['detection_boxes', 'num_detections', 'detection_classes', 'detection_scores'])
    tf4d.dispose();
    smallImg.dispose();
    tfImg.dispose();

    
    const predictionBoxes = predictions[0].dataSync();
    const predictionScores = predictions[3].dataSync();
    // console.log(predictionScores>0.3)
    // console.log("Test")
    context.clearRect(0, 0, canvas.width, canvas.height);   

    // const result = await model.predict(video);

    context.beginPath();
    for (let i = 0; i < 5; i++) {
        const score = predictionScores[i] * 100

        // console.log(score)
        
      if (score > 30) {
            const minY = predictionBoxes[i * 4] * video.height
            const minX = predictionBoxes[i * 4 + 1] * video.width
            const maxY = predictionBoxes[i * 4 + 2] * video.height
            const maxX = predictionBoxes[i * 4 + 3] * video.width
            
            // console.log('model loaded: ', minX, minY, maxX - minX, maxY - minY)
            
            context.rect(minX, minY, maxX - minX, maxY - minY);
            // context.rect(100, 100, 100,100);

            context.lineWidth = canvas.width / 100;
            context.strokeStyle = "cyan";
            context.fillStyle = "cyan";
            context.font = "" + canvas.height / 25 + "px Arial";
            context.stroke();
            context.fillText(
            "Shoe - " +  score.toFixed(1),
            minX,
            minY > 10 ? minY - 5 : 10
            );
      }
    }
    predictions[0].dispose();
    predictions[1].dispose();
    predictions[2].dispose();
    predictions[3].dispose();

    requestAnimationFrame(detect);
  }
})();

U2FsdGVkX1/852uf8vDqFz3Ce7HPAF9QpCAUvvtYPL1ZcNtmMXwQlkybTAiBf3QH
oG00RDJ2Xz8k7/KuAOhoVg==

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值