Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error loading model: SyntaxError: Unexpected token '<', "<!DOCTYPE "… is not valid JSON

I’m creating a yoga ai trainer using ml5 and p5 on React.

I created a component where it takes an individual pose as a prop from a local JSON file. The component also loads a model which I added in the public folder. The goal of this component is to detect a certain yoga pose and the component dynamically returns the pose name that is detected from the webcam.

I tested two webcam pages. let’s call it page 1 and page 2.
page 1 is works. the url is /practice. page 1 leads to webcam 1. webcam 1 works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

page 2 the url is /practice/poseId. page 2 leads to a different webcam component, webcam 2 has the exact same code as webcam 1 except it takes in a prop and that prop is the specific pose that matches the id.

On page two, I get this error
Error loading model: SyntaxError: Unexpected token ‘<‘, "<!DOCTYPE "… is not valid JSON

It is pointing to this code

        const modelInfo = {
          model: "model/model.json",
          metadata: "model/model_meta.json",
          weights: "model/model.weights.bin",
        };

        fetch(modelInfo.model)
          .then((response) => {
            if (!response.ok) {
              throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
          })
          .then((data) => {
            console.log("Model JSON:", data);
            brain.load(modelInfo, brainLoaded);
          })
          .catch((error) => {
            console.error("Error loading model:", error);
          });

I don’t understand why my component works on the /practice url, but when I add poseId (/practice/:poseID), it shows that error even though it’s the same code.

The error is on the url that ends in /practice/:poseId e.g. /practice/1.

Error example (you don’t see the pose label at the bottom):
error

Example (it works if the page url is /practice)
working

This is my repo: https://github.com/laura-nguyen/yoga-ai/tree/feature/page-pose-cam

Thanks!

>Solution :

The problem is that you’re using the same relative path from two pages in different directories. On localhost:5174/practice, model/model.json means localhost:5174/model/model.json, but on localhost:5174/practice/1, it means localhost:5174/practice/model/model.json.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading