ml5js / ml5-library

Friendly machine learning for the web! 🤖
https://ml5js.org
Other
6.5k stars 901 forks source link

pix2pix promise example broken & props inconsistent #873

Open joeyklee opened 5 years ago

joeyklee commented 5 years ago

→ Description 📝

The pix2pix promise example - https://github.com/ml5js/ml5-examples/tree/release/p5js/Pix2Pix/Pix2Pix_promise - breaks on load.

sketch.js:58 Uncaught TypeError: ml5.pix2pix(...).then is not a function
    at drawImage (sketch.js:58)
    at Image.i.onload (p5.min.js:3)

→ Screenshots 🖼

Screen Shot 2019-05-16 at 13 16 37

→ Helpful Information 🦄

A list of relevant information for this issue. This will help people navigate the project and offer some clues of where to start. Depending on the nature of this issue, important and helpful information might include:

ashishbairwa commented 5 years ago

It's still not working.

ashishbairwa commented 5 years ago

How should I get started contributing to such issues? :/

juandspy commented 4 years ago

How should I get started contributing to such issues? :/

Here is the guide! :) https://github.com/ml5js/ml5-library/blob/development/CONTRIBUTING.md

verdonarthur commented 4 years ago

FYI: For non-documented reason and only when loading p5.js, ml5.pix2pix without callback argument give back an object with two props -> image

So for the example with promise to work, you'll have to call pix2pix like that:

ml5.pix2pix('models/edges2pikachu.pict').ready
    .then(model => {
      // ... your code here
    })
lindapaiste commented 2 years ago

FYI: For non-documented reason and only when loading p5.js, ml5.pix2pix without callback argument give back an object with two props -> image

The object that you are looking at here is the pix2pix class instance. You'll see the methods if you expand the prototype: image

only when loading p5.js

Thank you! You've uncovered some extremely weird behavior. I was looking at the pix2pix source code and it definitely returns a promise when there is no callback. I can confirm that calling ml5.pix2pix('models/edges2pikachu.pict') works as intended (returns a promise) when there is no p5. Somehow p5 causes it to return the instance directly.

This must have something to do with the way that we are registering the functions with p5 in order to support preloading when they are called inside preload(). That is not something that is specific to pix2pix, so I checked out some other models. It turns out that this is a problem everywhere!

https://github.com/ml5js/ml5-library/blob/c088cb89af201b7891289559bf8257671281cae1/src/index.js#L75 https://github.com/ml5js/ml5-library/blob/main/src/utils/p5PreloadHelper.js