1

I'm getting an Uncaught ReferenceError when I try to load this script, any suggestions?

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>
henriquehbr
  • 1,063
  • 4
  • 20
  • 41
Be-All
  • 91
  • 1
  • 1
  • 12
  • This means mobilenet loads after your code or as it happens with unpkg.com mobilnet is not a global function. – Zydnar Dec 13 '19 at 12:48
  • You can try async import to control it `(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.\''); })();` or just use fetch `fetch('https://unpkg.com/@tensorflow-models/mobilenet@2.0.4').then(r=>r.text()).then((a)=>{const x = new Function('lol', a); x()})` – Zydnar Dec 13 '19 at 13:01

2 Answers2

1

Just change in your <head> tags as the following:

<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>

Additional suggestion:

Use your <script> tags at the end of <body> tag and not in the <head> or just simply use async attribute. It will help the page load performance. Read further in this answer for longer explanation.

I hope this helps!

norbitrial
  • 14,716
  • 7
  • 32
  • 59
0

The script tags are missing closing brackets

Be-All
  • 91
  • 1
  • 1
  • 12