微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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 举报,一经查实,本站将立刻删除。