{"size": 1247, "ext": "js", "max_stars_repo_path": "assets/js/ImageClassifier.js", "max_stars_repo_name": "adevandry/Awesome-JavaScript-Projects", "max_stars_repo_stars_event_min_datetime": "2021-11-05T00:49:30.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-05T00:49:40.000Z", "max_issues_repo_path": "assets/js/ImageClassifier.js", "max_issues_repo_name": "adevandry/Awesome-JavaScript-Projects", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "assets/js/ImageClassifier.js", "max_forks_repo_name": "adevandry/Awesome-JavaScript-Projects", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.9743589744, "max_line_length": 94, "alphanum_fraction": 0.6992782678}
const fileSelector = document.getElementById("fileSelector");
const result = document.getElementById("result"); // The result tag in the HTML
const probability = document.getElementById("probability"); // The probability tag in the HTML
const img = document.getElementById("image"); // The image we want to classify
const spinner = document.querySelector(".spinner");
const resultBox = document.querySelector(".resultbox");

fileSelector.addEventListener("change", (event) => {
  const file = event.target.files[0];
  if (file) {
    spinner.style.display = "inline-block";
    resultBox.style.display = "none";
    readImage(file);
  }
});

function readImage(file) {
  const reader = new FileReader();
  reader.addEventListener("load", (event) => {
    img.src = event.target.result;
  });

  reader.readAsDataURL(file);
  detectImage(img);
}

// Initialize the Image Classifier method with MobileNet
function detectImage(image) {
  ml5
    .imageClassifier("MobileNet")
    .then((classifier) => classifier.classify(image))
    .then((results) => {
      spinner.style.display = "none";
      resultBox.style.display = "flex";
      result.innerText = results[0].label;
      probability.innerText = results[0].confidence.toFixed(4);
    });
}
