Page 297 - MDP2022-2
P. 297

//  Load  the  image  model  and  setup  the  webcam
                          async  function  init()  {
                                  const  modelURL  =  URL  +  "model.json";
                                  const  metadataURL  =  URL  +  "metadata.json";


                                  //  load  the  model  and  metadata
                                  //  Refer  to  tmImage.loadFromFiles()  in  the  API  to  support  files  from  a  file
                  picker
                                  //  or  files  from  your  local  hard  drive
                                  //  Note:  the  pose  library  adds  "tmImage"  object  to  your  window
                  (window.tmImage)
                                  model  =  await  tmImage.load(modelURL,  metadataURL);
                                  maxPredictions  =  model.getTotalClasses();


                                  //  Convenience  function  to  setup  a  webcam
                                  const  flip  =  true;  //  whether  to  flip  the  webcam
                                  webcam  =  new  tmImage.Webcam(430,430,  flip);  //  width,  height,  flip
                                  await  webcam.setup();  //  request  access  to  the  webcam
                                  await  webcam.play();
                                  window.requestAnimationFrame(loop);


                                  //  append  elements  to  the  DOM
                                  document.getElementById("webcam-container").appendChild(webcam.canvas);
                                  labelContainer  =  document.getElementById("label-container");
                                  for  (let  i  =  0;  i  <  maxPredictions;  i++)  {  //  and  class  labels
                                          labelContainer.appendChild(document.createElement("div"));
                                  }
                          }


                          async  function  loop()  {
                                  webcam.update();  //  update  the  webcam  frame
                                  await  predict();
                                  window.requestAnimationFrame(loop);
                          }


                          async  function  predict_back()  {
                                  const  prediction  =  await  model.predict(webcam.canvas);
                        var  max_probability  =  "";
                                  for  (let  i  =  0;  i  <  maxPredictions;  i++)  {
                                          const   classPrediction   =   prediction[i].className   +   ":   "   +
   292   293   294   295   296   297   298   299   300   301   302