Tensorflowjs在我们的Web应用程序浏览器中有效,但在移动Web浏览器中无效

时间:2019-04-27 19:31:57

标签: javascript web mobile camera tensorflow.js

  

Blockquote

我已经构建了一个运行正常的tensforflow.js Web应用程序,并将其部署到Firebase。但是,当我通过手机访问网站时,相机在捕获图像和图像时并没有响应,并且会抛出错误:排除此问题的任何指导都值得赞赏。谢谢

  

Screenshot to error错误:请求的纹理尺寸[5069x5069]更大           比该浏览器/ gpu [4096x4096]上的webgl最大值大。

import * as tf from '@tensorflow/tfjs'
import {
  loadFrozenModel,
  loadGraphModel
} from '@tensorflow/tfjs-converter'
import labels from './labels.json'
import React from 'react';
import carTire from './carTire.jpg'
import bikeTire from './bicycleTire.jpg'
import motorTire from './motorTire.jpg'
import bicycleTire from './bicycleTire.jpg'

import {
  loadLayersModel
} from '@tensorflow/tfjs';


const ASSETS_URL = `${window.location.origin}/assets`
const MODEL_URL = `${ASSETS_URL}/kera_model/model-keras.json`
const WEIGHTS_URL = `${ASSETS_URL}/model/weights_manifest.json`
const IMAGE_SIZE = 224 // Model input size

const loadModel = async() => {
  console.log(MODEL_URL)
  const model = await tf.loadLayersModel(MODEL_URL)
  console.log(model)

  //Warm up GPU
  console.log(model);
  const input = tf.zeros([1, IMAGE_SIZE, IMAGE_SIZE, 3])
  console.log(input);
  return model
}

const predict = async(img, model) => {
  const t0 = performance.now()

  if (img) {
    // const image = tf.browser.fromPixels(img).toFloat();
    const image = tf.fromPixels(img).toFloat();
    console.log(image);
    //image = tf.expandDims();
    // model.add(tf.layers.dense({units: 1, inputShape: [1]}));
    const resized = tf.image.resizeBilinear(image, [IMAGE_SIZE, IMAGE_SIZE])
    const offset = tf.scalar(255 / 2)
    const normalized = resized.sub(offset).div(offset)
    const input = normalized.expandDims()
    //const output = await tf.tidy(() => model.predict({ input })).data() // MobileNet V1
    const output = await tf.tidy(() => model.predict(input)).data() // MobileNet V2
    //const output =await model.predict(input);
    console.log(output);
    const predictions = labels
      .map((label, index) => ({
        label,
        accuracy: output[index]
      }))
      .sort((a, b) => b.accuracy - a.accuracy)
    const time = `${(performance.now() - t0).toFixed(1)} ms`

    return {
      predictions,
      time
    }
  }

}

// }
export const start = async(webImage) => {
  // const input = document.getElementById('image')
  /*test: */
  const input = document.getElementById('webImage')

  console.log(input, "<<<<model image input")

  const model = await loadModel()

  const predictions = await predict(input, model)
  console.log(predictions);

  const temp = []
  if (predictions) {
    predictions.predictions.map(obj => {
      temp.push(obj)
    })

    console.log("predictions,predicionts", predictions.predictions)
    return predictions.predictions;
  }

}

0 个答案:

没有答案