【问题标题】:Attempt to Load Mobilenet results in Uncaught ReferenceError尝试加载 Mobilenet 导致未捕获的 ReferenceError
【发布时间】:2020-04-06 20:51:06
【问题描述】:

当我尝试加载此脚本时收到Uncaught ReferenceError,有什么建议吗?

index.html:12 Uncaught ReferenceError: mobilenet is not defined at index.html:12

<html>
    <head>
        <title>Something else Introduction</title>
        <!----  Import Script---->
        <script src="https://unpkg.com/@tensorflow/tfjs@1.2.8" type="text/javascript" </script>                   
        <script src="https://unpkg.com/@tensorflow-models/mobilenet@2.0.4" type="text/javascript"</script>
    </head>
    <body>
        <img id="img" crossOrigin src = "https://i.imgur.com/e5KD2lt.jpg">
        <h1 id="message">Hello, I'm a simple web page!</h1>
        <script>
            mobilenet.load().then(net => {
            console.log('Model loaded to memory!')
            const theImage = document.getElementById('img')
        net.classify(theImage).then(result=> {
            document.getElementById('message').innerText = `
                Detected: ${result[0].className}
                Probability: ${result[0].Probability}
                `
            })
        })   
        </script>
    </body>
</html>

【问题讨论】:

  • 这意味着 mobilenet 在您的代码之后加载或与 unpkg.com 发生的情况一样 mobilnet 不是全局函数。
  • 你可以尝试异步导入来控制它(async function() { let run; ({run} = (await import('https://coffeescript.org/browser-compiler-modern/coffeescript.js'))); return run('if 5 &lt; new Date().getHours() &lt; 9\n alert \'Time to make the coffee!\'\nelse\n alert \'Time to get some work done.\''); })();或者直接使用fetchfetch('https://unpkg.com/@tensorflow-models/mobilenet@2.0.4').then(r=&gt;r.text()).then((a)=&gt;{const x = new Function('lol', a); x()})

标签: javascript html mobilenet


【解决方案1】:

脚本标签缺少右括号

【讨论】:

    【解决方案2】:

    只需将您的&lt;head&gt; 标记更改如下:

    <script src="https://unpkg.com/@tensorflow/tfjs@1.2.8" type="text/javascript"></script>                   
    <script src="https://unpkg.com/@tensorflow-models/mobilenet@2.0.4" type="text/javascript"></script>
    

    补充建议:

    &lt;body&gt; 标记的末尾使用&lt;script&gt; 标记,而不是在&lt;head&gt; 中,或者只使用async 属性。它将有助于页面加载性能。进一步阅读in this answer 以获得更长的解释。

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 1970-01-01
      • 2023-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多