Open ziyuan-linn opened 2 months ago
This is exciting to see thank you @ziyuan-linn for working on this! One consideration is that running a stand-along node.js script for downloading the models may involves concepts/steps unfamiliar to beginners especially those used to working only in the p5.js web editor. I think it's ok for this to be the primary way to get the models since this is an "edge" use case, however, we should probably include some detailed documentation for total beginners. I could also record a video tutorial on this topic. Alternatively we could consider providing links to some model downloads directly but this might be hard to maintain.
@shiffman Thank you for the review! The node.js script for downloading models is meant to be only called by the ml5.js maintainers. I could also set it up as a postinstall script that automatically runs after yarn
so we never have to worry about it. The purpose of the script is to download the models so WebPack can bundle the model files into the ml5 library file itself.
As far as the users are concerned, they only need to change ml5.js
to ml5-offline.js
in the script tag. If they want to go fully offline, they can also download the p5.js
and ml5-offline.js
scripts and put them in the sketch folder. We can definitely have a quick guide/documentation for this!
The PR implements an offline version of the ml5.js library.
Webpack will now build
ml5-offline.js
andml5-offline.min.js
in addition toml5.js
andml5.min.js
. The fileml5-offline.js
andml5-offline.min.js
is self-contained and will not make additional network requests. Once a user downloadsml5-offline.min.js
, the ml5 library can run entirely without internet.The entry point for the offline model is
src/offline/index.js
. It imports the ml5 object, injects aloadOfflineModel
function into each ml5 model's prototype, and exports the ml5 object with the modified models. The approach keeps the offline implementation mostly separate from the main source code.Changes:
model.json
is written to a JS file.Uint8Array
and then written to JS files.loadOfflineModel
function.File
objects and creates object URLs for them.landmarkModelURL
) of the ml5 model.ml5-offline.js
build, add theloadOfflineModel
functions into object prototypes of each ml5 model.loadOfflineModel
function from each ml5 model during initialization if the function is defined.To test this implementation, find an example sketch and change
ml5.js
toml5-offline.js
inindex.html
.Note: This is WIP,
ml5-offline.js
only works forHandPose
withmodelType: "lite"
.