1

I have created a very simple script with p5.js and ml5.js as per this tutorial.

In the setup function, upon clicking on a button, I am adding images to the classifier. However, when I click on the button, I get the pixels error. This is how my directory looks like

enter image description here

let pinkButton
console.log('ml5 version:', ml5.version);

function gotResults(error, results) {
  console.log("ERROR", error);
}

function setup() {
  mobileNet = ml5.featureExtractor('MobileNet')
  classifier = mobileNet.classification()
  pinkButton = createButton('add pink')
  pinkButton.mousePressed(function() {
    classifier.addImage('./images/pink/1.png', 'pink')
    classifier.addImage('./images/pink/2.png', 'pink')
    classifier.addImage('./images/pink/3.png', 'pink')
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.min.js"></script>
<!-- ml5 -->
<script src="https://unpkg.com/ml5@0.4.3/dist/ml5.min.js"></script>
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
  • What version of ml5 and p5 are you using? – yudhiesh Oct 08 '20 at 12:30
  • @yuRa As mentioned in the script tags above, p5 is 1.1.9 while ml5 is ml5@0.4.3 i think –  Oct 08 '20 at 14:04
  • [This github post mentions the same problem](https://github.com/ml5js/ml5-library/issues/241) – yudhiesh Oct 08 '20 at 14:11
  • I don't see it "after loading a custom model". My model loads successfully. None of the solutions on that post seem to help me :( @yuRa –  Oct 08 '20 at 14:43

1 Answers1

0

addImage add an HTMl imageElement and not a url. Therefore to add an image either the image is already loaded in the page and it can be queried or a new image needs to be created.

Option 1: image already loaded

image = document.querySelector("use-css-selector")
classifier.addImage(image, label)

Option 2: create a new image

function load(url){
  return new Promise((resolve, reject) => {
    const im = new Image()
        im.crossOrigin = 'anonymous'
        im.src = 'url'
        im.onload = () => {
          resolve(im)
        }
   })
}

image = await load(url)
classifier.addImage(image, label)
edkeveked
  • 17,989
  • 10
  • 55
  • 93