【发布时间】: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 < new Date().getHours() < 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=>r.text()).then((a)=>{const x = new Function('lol', a); x()})
标签: javascript html mobilenet