我一直在与服务工作者和sw-toolbox一起玩弄.两者都是很好的方法,但似乎有它们的弱点.
我的项目开始使用谷歌的服务工作者方法(链接).我看到的方式是你必须手动更新缓存清除的版本号.我也错了,但我不认为用户访问过的网页不会被缓存.
与sw-toolbox方法相比,我需要添加的是以下代码:
self.toolbox.router.default = self.toolbox.networkFirst;
self.toolbox.router.get('/(.*)', function (req, vals, opts) {
return self.toolbox.networkFirst(req, vals, opts)
.catch(function (error) {
if (req.method === 'GET' && req.headers.get('accept').includes('text/html')) {
return self.toolbox.cacheOnly(new Request(OFFLINE_URL), vals, opts);
}
throw error;
});
});
Run Code Online (Sandbox Code Playgroud)
然后将解决缓存页面的问题.这是我的问题:在将sw-toolbox应用到我的项目之后,旧的服务工作者不会被新的服务工作者清除或替换,除非我去开发工具清除它.
任何想法如何解决这个问题?
我已经搜索了互联网的各个角落,如下所示:
它们都有类似的从模型预测的方法:
model.predict()
Run Code Online (Sandbox Code Playgroud)
根据文档,它应该返回一个包含预测的对象。但是,我总是遇到is not a function
错误。下面是我拥有的一段代码。
constructor() {
console.time('Loading of model');
this.mobileNet = new MobileNet();
this.mobileNet.loadMobilenet();
console.timeEnd('Loading of model');
}
const result = tfc.tidy(() => {
// tfc.fromPixels() returns a Tensor from an image element.
const raw = tfc.fromPixels(this.CANVAS).toFloat();
const cropped = this.cropImage(raw);
const resized = tfc.image.resizeBilinear(cropped, [this.IMAGE_SIZE, this.IMAGE_SIZE])
// Normalize the image from [0, 255] to [-1, 1].
const offset = tfc.scalar(127);
const normalized = resized.sub(offset).div(offset);
// Reshape to a single-element batch so …
Run Code Online (Sandbox Code Playgroud)