Skip to content

Commit 53254a8

Browse files
committed
update
1 parent 79de247 commit 53254a8

File tree

18 files changed

+14979
-9435
lines changed

18 files changed

+14979
-9435
lines changed

001_bodypix-worker-js/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
*#
33
node_modules
44
dist
5+
dist_worker
56
yarn-error.log

001_bodypix-worker-js/package-lock.json

Lines changed: 5793 additions & 2395 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

001_bodypix-worker-js/package.json

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dannadori/bodypix-worker-js",
3-
"version": "1.0.33",
3+
"version": "1.0.34",
44
"description": "",
55
"main": "dist/bodypix-worker.js",
66
"scripts": {
@@ -25,22 +25,24 @@
2525
},
2626
"homepage": "https://github.com/w-okada/image-analyze-workers",
2727
"devDependencies": {
28+
"@babel/preset-env": "^7.15.6",
29+
"@types/node": "^16.10.2",
30+
"before-build-webpack": "^0.2.11",
2831
"npm-run-all": "^4.1.5",
2932
"rimraf": "^3.0.2",
30-
"ts-loader": "^9.2.3",
31-
"tsconfig-paths": "^3.10.1",
32-
"typescript": "^4.3.5",
33-
"webpack": "^5.45.1",
34-
"webpack-cli": "^4.7.2",
35-
"worker-plugin": "^5.0.1"
33+
"ts-loader": "^9.2.6",
34+
"tsconfig-paths": "^3.11.0",
35+
"typescript": "^4.4.3",
36+
"webpack": "^5.55.1",
37+
"webpack-cli": "^4.8.0",
38+
"worker-loader": "^3.0.8"
3639
},
3740
"dependencies": {
3841
"@tensorflow-models/body-pix": "^2.2.0",
39-
"@tensorflow/tfjs": "^3.8.0",
40-
"@tensorflow/tfjs-backend-wasm": "^3.8.0",
41-
"@tensorflow/tfjs-converter": "^3.8.0",
42-
"@tensorflow/tfjs-core": "^3.8.0",
43-
"@types/node": "^16.4.0",
42+
"@tensorflow/tfjs": "^3.9.0",
43+
"@tensorflow/tfjs-backend-wasm": "^3.9.0",
44+
"@tensorflow/tfjs-converter": "^3.9.0",
45+
"@tensorflow/tfjs-core": "^3.9.0",
4446
"await-semaphore": "^0.1.3"
4547
}
4648
}

001_bodypix-worker-js/src/bodypix-worker-worker.ts

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,96 +8,97 @@ const ctx: Worker = self as any // eslint-disable-line no-restricted-globals
88
let model: bodyPix.BodyPix | null
99

1010
const load_module = async (config: BodyPixConfig) => {
11-
if(config.useTFWasmBackend || config.browserType === BrowserType.SAFARI){
12-
console.log("use wasm backend")
13-
require('@tensorflow/tfjs-backend-wasm')
14-
await tf.setBackend("wasm")
15-
}else{
16-
console.log("use webgl backend")
17-
require('@tensorflow/tfjs-backend-webgl')
18-
await tf.setBackend("webgl")
19-
}
11+
if (config.useTFWasmBackend || config.browserType === BrowserType.SAFARI) {
12+
console.log("use wasm backend")
13+
require('@tensorflow/tfjs-backend-wasm')
14+
await tf.setBackend("wasm")
15+
} else {
16+
console.log("use webgl backend")
17+
require('@tensorflow/tfjs-backend-webgl')
18+
await tf.setBackend("webgl")
19+
}
2020
}
2121

22-
const generateImage = (image:ImageBitmap, prediction: bodyPix.SemanticPersonSegmentation) => {
22+
const generateImage = (image: ImageBitmap, prediction: bodyPix.SemanticPersonSegmentation) => {
2323

24-
// generate maskImage from prediction
25-
const pixelData = new Uint8ClampedArray(prediction.width * prediction.height * 4)
26-
for (let rowIndex = 0; rowIndex < prediction.height; rowIndex++) {
27-
for (let colIndex = 0; colIndex < prediction.width; colIndex++) {
28-
const seg_offset = ((rowIndex * prediction.width) + colIndex)
29-
const pix_offset = ((rowIndex * prediction.width) + colIndex) * 4
30-
if (prediction.data[seg_offset] === 0) {
31-
pixelData[pix_offset] = 0
32-
pixelData[pix_offset + 1] = 0
33-
pixelData[pix_offset + 2] = 0
34-
pixelData[pix_offset + 3] = 0
35-
} else {
36-
pixelData[pix_offset] = 255
37-
pixelData[pix_offset + 1] = 255
38-
pixelData[pix_offset + 2] = 255
39-
pixelData[pix_offset + 3] = 255
40-
}
24+
// generate maskImage from prediction
25+
const pixelData = new Uint8ClampedArray(prediction.width * prediction.height * 4)
26+
for (let rowIndex = 0; rowIndex < prediction.height; rowIndex++) {
27+
for (let colIndex = 0; colIndex < prediction.width; colIndex++) {
28+
const seg_offset = ((rowIndex * prediction.width) + colIndex)
29+
const pix_offset = ((rowIndex * prediction.width) + colIndex) * 4
30+
if (prediction.data[seg_offset] === 0) {
31+
pixelData[pix_offset] = 0
32+
pixelData[pix_offset + 1] = 0
33+
pixelData[pix_offset + 2] = 0
34+
pixelData[pix_offset + 3] = 0
35+
} else {
36+
pixelData[pix_offset] = 255
37+
pixelData[pix_offset + 1] = 255
38+
pixelData[pix_offset + 2] = 255
39+
pixelData[pix_offset + 3] = 255
40+
}
41+
}
4142
}
42-
}
43-
const maskImage = new ImageData(pixelData, prediction.width, prediction.height);
43+
const maskImage = new ImageData(pixelData, prediction.width, prediction.height);
4444

45-
// generate maskImage Canvas
46-
const maskOffscreen = new OffscreenCanvas(prediction.width, prediction.height)
47-
maskOffscreen.getContext("2d")!.putImageData(maskImage, 0, 0)
45+
// generate maskImage Canvas
46+
const maskOffscreen = new OffscreenCanvas(prediction.width, prediction.height)
47+
maskOffscreen.getContext("2d")!.putImageData(maskImage, 0, 0)
4848

49-
// resize mask Image
50-
const resizedMaskOffscreen = new OffscreenCanvas(image.width, image.height)
51-
const ctx = resizedMaskOffscreen.getContext("2d")!
52-
ctx.drawImage(maskOffscreen, 0, 0, image.width, image.height)
53-
ctx.globalCompositeOperation = 'source-in';
54-
ctx.drawImage(image, 0, 0, image.width, image.height)
55-
return resizedMaskOffscreen
49+
// resize mask Image
50+
const resizedMaskOffscreen = new OffscreenCanvas(image.width, image.height)
51+
const ctx = resizedMaskOffscreen.getContext("2d")!
52+
ctx.drawImage(maskOffscreen, 0, 0, image.width, image.height)
53+
ctx.globalCompositeOperation = 'source-in';
54+
ctx.drawImage(image, 0, 0, image.width, image.height)
55+
return resizedMaskOffscreen
5656
}
5757

58-
const predict = async (image: ImageBitmap, config: BodyPixConfig, params:BodyPixOperatipnParams) => {
59-
// ImageData作成
60-
const processWidth = (params.processWidth <= 0 || params.processHeight <= 0) ? image.width : params.processWidth
61-
const processHeight = (params.processWidth <= 0 || params.processHeight <= 0) ? image.height : params.processHeight
58+
const predict = async (image: ImageBitmap, config: BodyPixConfig, params: BodyPixOperatipnParams) => {
59+
console.log("PREDICT_CHECK")
60+
// ImageData作成
61+
const processWidth = (params.processWidth <= 0 || params.processHeight <= 0) ? image.width : params.processWidth
62+
const processHeight = (params.processWidth <= 0 || params.processHeight <= 0) ? image.height : params.processHeight
6263

63-
//console.log("process image size:", processWidth, processHeight)
64-
const offscreen = new OffscreenCanvas(processWidth, processHeight)
65-
const ctx = offscreen.getContext("2d")!
66-
ctx.drawImage(image, 0, 0, processWidth, processHeight)
67-
const newImg = ctx.getImageData(0, 0, processWidth, processHeight)
64+
//console.log("process image size:", processWidth, processHeight)
65+
const offscreen = new OffscreenCanvas(processWidth, processHeight)
66+
const ctx = offscreen.getContext("2d")!
67+
ctx.drawImage(image, 0, 0, processWidth, processHeight)
68+
const newImg = ctx.getImageData(0, 0, processWidth, processHeight)
6869

69-
let prediction
70-
if(params.type === BodypixFunctionType.SegmentPerson){
71-
prediction = await model!.segmentPerson(newImg, params.segmentPersonParams)
72-
}else if(params.type === BodypixFunctionType.SegmentPersonParts){
73-
prediction = await model!.segmentPersonParts(newImg, params.segmentPersonPartsParams)
74-
}else if(params.type === BodypixFunctionType.SegmentMultiPerson){
75-
prediction = await model!.segmentMultiPerson(newImg, params.segmentMultiPersonParams)
76-
}else if(params.type === BodypixFunctionType.SegmentMultiPersonParts){
77-
prediction = await model!.segmentMultiPersonParts(newImg, params.segmentMultiPersonPartsParams)
78-
}else{// segmentPersonに倒す
79-
prediction = await model!.segmentPerson(newImg, params.segmentPersonParams)
80-
}
81-
return prediction
70+
let prediction
71+
if (params.type === BodypixFunctionType.SegmentPerson) {
72+
prediction = await model!.segmentPerson(newImg, params.segmentPersonParams)
73+
} else if (params.type === BodypixFunctionType.SegmentPersonParts) {
74+
prediction = await model!.segmentPersonParts(newImg, params.segmentPersonPartsParams)
75+
} else if (params.type === BodypixFunctionType.SegmentMultiPerson) {
76+
prediction = await model!.segmentMultiPerson(newImg, params.segmentMultiPersonParams)
77+
} else if (params.type === BodypixFunctionType.SegmentMultiPersonParts) {
78+
prediction = await model!.segmentMultiPersonParts(newImg, params.segmentMultiPersonPartsParams)
79+
} else {// segmentPersonに倒す
80+
prediction = await model!.segmentPerson(newImg, params.segmentPersonParams)
81+
}
82+
return prediction
8283
}
8384

8485

8586
onmessage = async (event) => {
86-
if (event.data.message === WorkerCommand.INITIALIZE) {
87-
const config = event.data.config as BodyPixConfig
88-
await load_module(config)
89-
bodyPix.load(event.data.config.model).then(res => {
90-
console.log("bodypix loaded default", event.data.config)
91-
model = res
92-
ctx.postMessage({ message: WorkerResponse.INITIALIZED })
93-
})
94-
} else if (event.data.message === WorkerCommand.PREDICT) {
95-
const config:BodyPixConfig = event.data.config
96-
const image: ImageBitmap = event.data.image
97-
const uid: number = event.data.uid
98-
const params:BodyPixOperatipnParams = event.data.params
87+
if (event.data.message === WorkerCommand.INITIALIZE) {
88+
const config = event.data.config as BodyPixConfig
89+
await load_module(config)
90+
bodyPix.load(event.data.config.model).then(res => {
91+
console.log("bodypix loaded default", event.data.config)
92+
model = res
93+
ctx.postMessage({ message: WorkerResponse.INITIALIZED })
94+
})
95+
} else if (event.data.message === WorkerCommand.PREDICT) {
96+
const config: BodyPixConfig = event.data.config
97+
const image: ImageBitmap = event.data.image
98+
const uid: number = event.data.uid
99+
const params: BodyPixOperatipnParams = event.data.params
99100

100-
const prediction = await predict(image, config, params)
101-
ctx.postMessage({ message: WorkerResponse.PREDICTED, uid: uid, prediction: prediction})
102-
}
101+
const prediction = await predict(image, config, params)
102+
ctx.postMessage({ message: WorkerResponse.PREDICTED, uid: uid, prediction: prediction })
103+
}
103104
}

001_bodypix-worker-js/src/bodypix-worker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export { BodyPixInternalResolution } from '@tensorflow-models/body-pix/dist/type
1111

1212
import * as tf from '@tensorflow/tfjs';
1313

14+
// @ts-ignore
15+
import workerJs from "worker-loader?inline=no-fallback!./bodypix-worker-worker.ts";
1416

1517
const load_module = async (config: BodyPixConfig) => {
1618
if (config.useTFWasmBackend) {
@@ -154,13 +156,11 @@ export class BodypixWorkerManager {
154156
})
155157
}
156158

157-
console.log("load bodypix1")
158-
const workerBP = new Worker(this.config.workerPath, { type: 'module' })
159-
console.log("load bodypix2")
160-
159+
const workerBP = new workerJs()
160+
161161
workerBP!.postMessage({ message: WorkerCommand.INITIALIZE, config: this.config })
162162
const p = new Promise<void>((onResolve, onFail) => {
163-
workerBP!.onmessage = (event) => {
163+
workerBP!.onmessage = (event: any) => {
164164
if (event.data.message === WorkerResponse.INITIALIZED) {
165165
console.log("WORKERSS INITIALIZED")
166166
this.workerBP = workerBP
@@ -180,8 +180,8 @@ export class BodypixWorkerManager {
180180
const prediction = await this.localBP.predict(targetCanvas, this.config, params)
181181
return prediction
182182
} else {
183-
if(!this.workerBP){
184-
return Array.from(new Array(params.processWidth), () => new Array(params.processHeight).fill(1));
183+
if (!this.workerBP) {
184+
return Array.from(new Array(params.processWidth), () => new Array(params.processHeight).fill(1));
185185
}
186186
const offscreen = new OffscreenCanvas(targetCanvas.width, targetCanvas.height)
187187
const offctx = offscreen.getContext("2d")!
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module "worker-loader!*" {
2+
class WebpackWorker extends Worker {
3+
constructor();
4+
}
5+
export default WebpackWorker;
6+
}

001_bodypix-worker-js/tsconfig.json

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,14 @@
11
{
22
"compilerOptions": {
3-
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4-
5-
/* Basic Options */
6-
// "incremental": true, /* Enable incremental compilation */
73
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
84
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
95
"lib": ["es2018", "dom"], /* Specify library files to be included in the compilation. */
10-
// "allowJs": true, /* Allow javascript files to be compiled. */
11-
// "checkJs": true, /* Report errors in .js files. */
12-
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
136
"declaration": true, /* Generates corresponding '.d.ts' file. */
14-
//"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
157
"sourceMap": true, /* Generates corresponding '.map' file. */
16-
//"outFile": "./dist", /* Concatenate and emit output to single file. */
178
"outDir": "./dist", /* Redirect output structure to the directory. */
18-
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19-
// "composite": true, /* Enable project compilation */
20-
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21-
// "removeComments": true, /* Do not emit comments to output. */
22-
// "noEmit": true, /* Do not emit outputs. */
23-
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
24-
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25-
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
269

27-
/* Strict Type-Checking Options */
2810
"strict": true, /* Enable all strict type-checking options. */
29-
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30-
// "strictNullChecks": true, /* Enable strict null checks. */
31-
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
32-
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33-
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34-
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35-
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36-
37-
/* Additional Checks */
38-
// "noUnusedLocals": true, /* Report errors on unused locals. */
39-
// "noUnusedParameters": true, /* Report errors on unused parameters. */
40-
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41-
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42-
43-
/* Module Resolution Options */
44-
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
45-
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
46-
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
47-
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
48-
// "typeRoots": [], /* List of folders to include type definitions from. */
49-
// "types": [], /* Type declaration files to be included in compilation. */
50-
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
5111
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
52-
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
53-
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
54-
55-
/* Source Map Options */
56-
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
57-
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
58-
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
59-
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
60-
61-
/* Experimental Options */
62-
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
63-
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
64-
65-
/* Advanced Options */
6612
"skipLibCheck": true, /* Skip type checking of declaration files. */
6713
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
6814
}
Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const path = require('path');
2-
const WorkerPlugin = require('worker-plugin');
2+
33
const manager = {
44
mode: 'production',
55
entry: './src/bodypix-worker.ts',
@@ -11,7 +11,7 @@ const manager = {
1111
},
1212
module: {
1313
rules: [
14-
{ test: /\.ts$/, loader: 'ts-loader' },
14+
{ test: /\.ts$/, loader: 'ts-loader'},
1515
],
1616
},
1717
output: {
@@ -20,35 +20,8 @@ const manager = {
2020
libraryTarget: 'umd',
2121
globalObject: 'typeof self !== \'undefined\' ? self : this'
2222
},
23-
plugins: [               
24-
new WorkerPlugin()
25-
]
26-
};
27-
const worker = {
28-
mode: 'production',
29-
entry: './src/bodypix-worker-worker.ts',
30-
resolve: {
31-
extensions: [".ts", ".js"],
32-
fallback: {
33-
"os": false
34-
}
35-
},
36-
module: {
37-
rules: [
38-
{ test: /\.ts$/, loader: 'ts-loader' },
39-
],
40-
},
41-
output: {
42-
filename: 'bodypix-worker-worker.js',
43-
path: path.resolve(__dirname, 'dist'),
44-
libraryTarget: 'umd',
45-
globalObject: 'typeof self !== \'undefined\' ? self : this'
46-
},
47-
plugins: [                
48-
new WorkerPlugin()
49-
]
5023
};
5124

5225
module.exports = [
53-
manager, worker
26+
manager
5427
];

001_bodypix-worker-js_singlejs/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)