From 44ae1b517aa963485da82bab1f5c452d726c0aac Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 12 Jan 2021 08:24:00 -0500 Subject: [PATCH] fix safari incopatibility --- config.js | 4 ++-- src/human.js | 63 ++++++++++++++++++++++++++++++++++++++++++---------- wiki | 2 +- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/config.js b/config.js index 6b653ab9..1a39d5e5 100644 --- a/config.js +++ b/config.js @@ -144,14 +144,14 @@ export default { modelType: 'MobileNet', // Human includes MobileNet version, but you can switch to ResNet }, - pose: { + pose: { // TBD: not currently in use enabled: false, scoreThreshold: 0.6, // threshold for deciding when to remove boxes based on score // in non-maximum suppression iouThreshold: 0.3, // threshold for deciding whether boxes overlap too much // in non-maximum suppression modelPath: '../models/blazepose.json', - inputSize: 128, // fixed value + inputSize: 256, // fixed value }, hand: { diff --git a/src/human.js b/src/human.js index a905a33a..4051824d 100644 --- a/src/human.js +++ b/src/human.js @@ -446,27 +446,66 @@ class Human { }); } - async warmup(userConfig) { + async warmupBitmap() { const b64toBlob = (base64, type = 'application/octet-stream') => fetch(`data:${type};base64,${base64}`).then((res) => res.blob()); - - if (userConfig) this.config = mergeDeep(this.config, userConfig); - const video = this.config.videoOptimized; - this.config.videoOptimized = false; let blob; + let res; switch (this.config.warmup) { case 'face': blob = await b64toBlob(sample.face); break; case 'full': blob = await b64toBlob(sample.body); break; default: blob = null; } - if (!blob) return null; - const bitmap = await createImageBitmap(blob); + if (blob) { + const bitmap = await createImageBitmap(blob); + res = await this.detect(bitmap, config); + bitmap.close(); + } + return res; + } + + async warmupCanvas() { + return new Promise((resolve) => { + let src; + let size = 0; + switch (this.config.warmup) { + case 'face': + size = 256; + src = 'data:image/jpeg;base64,' + sample.face; + break; + case 'full': + size = 1200; + src = 'data:image/jpeg;base64,' + sample.body; + break; + default: + src = null; + } + const img = new Image(size, size); + img.onload = () => { + const canvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(size, size) : document.createElement('canvas'); + canvas.width = size; + canvas.height = size; + const ctx = canvas.getContext('2d'); + ctx.drawImage(img, 0, 0); + const data = ctx.getImageData(0, 0, size, size); + this.detect(data, config).then((res) => resolve(res)); + }; + if (src) img.src = src; + else resolve(null); + }); + } + + async warmup(userConfig) { const t0 = now(); - const warmup = await this.detect(bitmap, config); - const t1 = now(); - bitmap.close(); - log('Warmup', this.config.warmup, (t1 - t0), warmup); + if (userConfig) this.config = mergeDeep(this.config, userConfig); + const video = this.config.videoOptimized; + this.config.videoOptimized = false; + let res; + if (typeof createImageBitmap === 'function') res = await this.warmupBitmap(); + else res = await this.warmupCanvas(); this.config.videoOptimized = video; - return warmup; + const t1 = now(); + log('Warmup', this.config.warmup, (t1 - t0), res); + return res; } } diff --git a/wiki b/wiki index 5aa552da..f31fa056 160000 --- a/wiki +++ b/wiki @@ -1 +1 @@ -Subproject commit 5aa552da6706eb513e7dc6b49302b689b682ca40 +Subproject commit f31fa056967450ba8427bb2768db92cbe9b8cd8e