Page 338 - MDP2022-2
P. 338

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);
                    }


                    function  dateFormat(date)  {
                            let  month  =  date.getMonth()  +  1;
                            let  day  =  date.getDate();


                            month  =  month  >=  10  ?  month  :  '0'  +  month;
                            day  =  day  >=  10  ?  day  :  '0'  +  day;


                            return  date.getFullYear()  +  '-'  +  month  +  '-'  +  day;
                    }
                    //  run  the  webcam  image  through  the  image  model
                    async  function  predict()  {
                            //  predict  can  take  in  an  image,  video  or  canvas  html  element
                            const  prediction  =  await  model.predict(webcam.canvas);
                            for  (let  i  =  0;  i  <  maxPredictions;  i++)  {
                                    const  classPrediction  =
                                            prediction[i].className  +  ":  "  +  prediction[i].probability.toFixed(2);
                                    labelContainer.childNodes[i].innerHTML  =  classPrediction;
                                    if  (prediction[i].probability  >  0.85)  {
                                            result  =  prompt(`${prediction[i].className}`,  [
                                                    dateFormat(new  Date())
                                            ]);
                                            fetch('/mdp/material_add.php',  {
                                                    method:  'POST',
                                                    cache:  'no-cache',
                                                    headers:  {
                                                            'Content-Type':  'application/x-www-form-urlencoded'
                                                    },
                                                    body:
            `food_name=${prediction[i].className}&food_type=${foodType[prediction[i].className]}&date=${date
   333   334   335   336   337   338   339   340   341   342   343