Face-api.js: loading models in react

Created on 13 Sep 2019  路  3Comments  路  Source: justadudewhohacks/face-api.js

import React, { Component } from 'react';
import * as faceapi from 'face-api.js'

class camera extends Component{

constructor(props) {
    super(props);
    this.videoTag = React.createRef()
    this.state={
        video:null
    }
    this.detect=this.detect.bind(this);
}

componentDidMount() {
    // getting access to webcam
        navigator.mediaDevices
        .getUserMedia({video: true})
        .then(stream =>
         this.videoTag.current.srcObject = stream,
         this.detect()
           )
          .catch(console.log);
         }

      detect=async ()=>{
      const videoTag=document.getElementById('videoTag');
      console.log("geeting")
      const canvas=document.getElementById('myCanvas');
      await faceapi.nets.ssdMobilenetv1.loadFromUri('/models') 
      faceapi.nets.ssdMobilenetv1.loadFromUri('./models')
      const displaySize = { width: videoTag.width, height: videoTag.height }
      faceapi.matchDimensions(canvas, displaySize)
      setInterval(async () => {
      const detections = await faceapi.detectAllFaces(videoTag)
      console.log(detections.length);
      const resizedDetections = faceapi.resizeResults(detections, displaySize)
      canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
      faceapi.draw.drawDetections(canvas, resizedDetections)
    }, 200)
  }


render() {
    return( 
        <div>
            <video id="videoTag"
                    ref={this.videoTag}
                    width={500}
                    height={500}
                    autoPlay
                    ></video>
                  <canvas id="myCanvas"
                            height={500}
                            width={500}></canvas>
                  </div>                      
    );
}

}
export default camera;

while loading ssd mobilenet model in react this error is geeting

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

question solution provided

Most helpful comment

Check you have copied the models correctly. It happened to me. Maybe you copied the json incorrectly.

All 3 comments

please check wheather i've had mistaken or missing something

Check you have copied the models correctly. It happened to me. Maybe you copied the json incorrectly.

Also check the network tab an make sure the uris your models are fetched from are correct.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hawxxx picture hawxxx  路  5Comments

joesalloum picture joesalloum  路  6Comments

gmanojisaac picture gmanojisaac  路  6Comments

XDmoyang picture XDmoyang  路  6Comments

salamanders picture salamanders  路  3Comments