TensorflowJS库可在localhost上运行,但是一旦部署到Netlify后就不再可用?

问题描述

我遵循了this教程,将COCOSSD对象检测模型实现到了前端。我使用@ tensorflow / tfjs和npm @ tensorflow-models / coco-ssd安装了节点模块。该Web应用程序仅适用于前端,也就是说,对象检测模型在localhost上运行良好,但是一旦我通过npm run build部署到Netlify,netlify deploy --prod将不再起作用。我已经确认这不是相机问题。

这是我用于模型实现的代码

import * as cocoSsd from '@tensorflow-models/coco-ssd';
import '@tensorflow/tfjs';

class Detector extends Component {
  constructor(props) {
    super(props);

    this.state = {
      count: 0,list: ['person','laptop','scissors','mouse','spoon','keyboard',],isstopped: true,}
  } 
  videoRef = React.createRef();

  componentDidMount() {
    if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
      const webCamPromise = navigator.mediaDevices
        .getUserMedia({
          audio: false,video: {
            facingMode: "user"
          }
        })
        .then(stream => {
          window.stream = stream;
          this.videoRef.current.srcObject = stream;
          return new Promise((resolve,reject) => {
            this.videoRef.current.onloadedMetadata = () => {
              resolve();
            };
          });
        });
      const modelPromise = cocoSsd.load();
      Promise.all([modelPromise,webCamPromise])
        .then(values => {
          this.detectFrame(this.videoRef.current,values[0]);
        })
        .catch(error => {
          console.error(error);
        });
    }
  }

  detectFrame = (video,model) => {
    model.detect(video).then(predictions => {
      this.checkPredictions(predictions);
      requestAnimationFrame(() => {
        this.detectFrame(video,model);
      });
    });
  };

  checkPredictions = predictions => {
    predictions.forEach(prediction => {
      if(prediction.class === this.state.list[0]) {
        const tempL = this.state.list;
        const tempC = this.state.count + 1;
        tempL.shift();
        this.setState({list: tempL,count: tempC,isstopped: false});
      }
    });
  };

  //render statements etc. irrelevant to the issue I believe

请有人帮忙,我花了几个小时来构建这个东西,而一旦部署后发现它不起作用,我非常失望。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)