feat(models): add offline model caching via IndexedDB#304
Draft
alanvww wants to merge 1 commit intoml5js:mainfrom
Draft
feat(models): add offline model caching via IndexedDB#304alanvww wants to merge 1 commit intoml5js:mainfrom
alanvww wants to merge 1 commit intoml5js:mainfrom
Conversation
Implemented `{ cache: true }` initialization option across `HandPose`, `BodyPose`, `FaceMesh` to automatically store downloaded models in the browser's IndexedDB. Subsequent reloads serve models directly from the local cache, enabling full offline functionality for exhibitions and installations.
- Added `src/utils/modelCache.js` with a `CachingIOHandler` for tf.js and caching utilities.
- Added `src/utils/modelRegistry.js` to resolve dynamic tf.js / mediapipe model URLs.
- Added `ml5.isCached()`, `ml5.cacheModel()`, and `ml5.clearCache()` global utility methods.
- Refactored model initialization routines to await `this.ready` to prevent asynchronous `detectStart()` crashes.
- Added `examples/handPose-keypoints-offline` to demonstrate offline mode functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This pull request adds support for offline model caching using the browser's IndexedDB. By passing
{ cache: true }when initializing models likehandPose,bodyPose, andfaceMesh, the models are transparently downloaded and stored locally. On subsequent page loads, the models are served directly from the IndexedDB cache, making the library fully functional without an internet connection (ideal for installations and exhibitions).Changes
CachingIOHandlerinsrc/utils/modelCache.jsto intercept TensorFlow.js model requests and store them in IndexedDB.src/utils/modelRegistry.jsto resolve dynamic model URLs for caching.ml5.isCached(),ml5.cacheModel(), andml5.clearCache()to manually manage the IndexedDB state.await this.ready;acrossdetect()anddetectLoop()methods in all major models to prevent synchronous crashes whenthis.modelisnullduring background initialization.examples/handPose-keypoints-offlineto showcase how to preload and use models in an offline environment, keeping the UI minimal and consistent with other examples.