1
- import { WorkerCommand , WorkerResponse , BodypixFunctionType , BodyPixConfig , BodyPixOperatipnParams } from './const'
2
- import * as bodyPix from '@tensorflow-models/body-pix'
3
- import * as tf from '@tensorflow/tfjs' ;
4
- import { BrowserType } from './BrowserUtil' ;
1
+ import {
2
+ WorkerCommand ,
3
+ WorkerResponse ,
4
+ BodypixFunctionType ,
5
+ BodyPixConfig ,
6
+ BodyPixOperatipnParams ,
7
+ } from "./const" ;
8
+ import * as bodyPix from "@tensorflow-models/body-pix" ;
9
+ import * as tf from "@tensorflow/tfjs" ;
10
+ import { BrowserType } from "./BrowserUtil" ;
5
11
6
- const ctx : Worker = self as any // eslint-disable-line no-restricted-globals
12
+ const ctx : Worker = self as any ; // eslint-disable-line no-restricted-globals
7
13
8
- let model : bodyPix . BodyPix | null
14
+ let model : bodyPix . BodyPix | null ;
9
15
10
16
const load_module = async ( config : BodyPixConfig ) => {
11
17
if ( config . useTFWasmBackend || config . browserType === BrowserType . SAFARI ) {
12
- console . log ( "use wasm backend" )
13
- require ( ' @tensorflow/tfjs-backend-wasm' )
14
- await tf . setBackend ( "wasm" )
18
+ console . log ( "use wasm backend" ) ;
19
+ require ( " @tensorflow/tfjs-backend-wasm" ) ;
20
+ await tf . setBackend ( "wasm" ) ;
15
21
} else {
16
- console . log ( "use webgl backend" )
17
- require ( ' @tensorflow/tfjs-backend-webgl' )
18
- await tf . setBackend ( "webgl" )
22
+ console . log ( "use webgl backend" ) ;
23
+ require ( " @tensorflow/tfjs-backend-webgl" ) ;
24
+ await tf . setBackend ( "webgl" ) ;
19
25
}
20
- }
21
-
22
- const generateImage = ( image : ImageBitmap , prediction : bodyPix . SemanticPersonSegmentation ) => {
26
+ } ;
23
27
28
+ const generateImage = (
29
+ image : ImageBitmap ,
30
+ prediction : bodyPix . SemanticPersonSegmentation
31
+ ) => {
24
32
// generate maskImage from prediction
25
- const pixelData = new Uint8ClampedArray ( prediction . width * prediction . height * 4 )
33
+ const pixelData = new Uint8ClampedArray (
34
+ prediction . width * prediction . height * 4
35
+ ) ;
26
36
for ( let rowIndex = 0 ; rowIndex < prediction . height ; rowIndex ++ ) {
27
37
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
38
+ const seg_offset = rowIndex * prediction . width + colIndex ;
39
+ const pix_offset = ( rowIndex * prediction . width + colIndex ) * 4 ;
30
40
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
41
+ pixelData [ pix_offset ] = 0 ;
42
+ pixelData [ pix_offset + 1 ] = 0 ;
43
+ pixelData [ pix_offset + 2 ] = 0 ;
44
+ pixelData [ pix_offset + 3 ] = 0 ;
35
45
} else {
36
- pixelData [ pix_offset ] = 255
37
- pixelData [ pix_offset + 1 ] = 255
38
- pixelData [ pix_offset + 2 ] = 255
39
- pixelData [ pix_offset + 3 ] = 255
46
+ pixelData [ pix_offset ] = 255 ;
47
+ pixelData [ pix_offset + 1 ] = 255 ;
48
+ pixelData [ pix_offset + 2 ] = 255 ;
49
+ pixelData [ pix_offset + 3 ] = 255 ;
40
50
}
41
51
}
42
52
}
43
- const maskImage = new ImageData ( pixelData , prediction . width , prediction . height ) ;
53
+ const maskImage = new ImageData (
54
+ pixelData ,
55
+ prediction . width ,
56
+ prediction . height
57
+ ) ;
44
58
45
59
// generate maskImage Canvas
46
- const maskOffscreen = new OffscreenCanvas ( prediction . width , prediction . height )
47
- maskOffscreen . getContext ( "2d" ) ! . putImageData ( maskImage , 0 , 0 )
60
+ const maskOffscreen = new OffscreenCanvas (
61
+ prediction . width ,
62
+ prediction . height
63
+ ) ;
64
+ maskOffscreen . getContext ( "2d" ) ! . putImageData ( maskImage , 0 , 0 ) ;
48
65
49
66
// 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
56
- }
67
+ const resizedMaskOffscreen = new OffscreenCanvas ( image . width , image . height ) ;
68
+ const ctx = resizedMaskOffscreen . getContext ( "2d" ) ! ;
69
+ ctx . drawImage ( maskOffscreen , 0 , 0 , image . width , image . height ) ;
70
+ ctx . globalCompositeOperation = " source-in" ;
71
+ ctx . drawImage ( image , 0 , 0 , image . width , image . height ) ;
72
+ return resizedMaskOffscreen ;
73
+ } ;
57
74
58
- const predict = async ( image : ImageBitmap , config : BodyPixConfig , params : BodyPixOperatipnParams ) => {
59
- console . log ( "PREDICT_CHECK" )
75
+ const predict = async (
76
+ image : ImageBitmap ,
77
+ config : BodyPixConfig ,
78
+ params : BodyPixOperatipnParams
79
+ ) => {
80
+ console . log ( "PREDICT_CHECK" ) ;
60
81
// 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
82
+ const processWidth =
83
+ params . processWidth <= 0 || params . processHeight <= 0
84
+ ? image . width
85
+ : params . processWidth ;
86
+ const processHeight =
87
+ params . processWidth <= 0 || params . processHeight <= 0
88
+ ? image . height
89
+ : params . processHeight ;
63
90
64
91
//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 )
92
+ const offscreen = new OffscreenCanvas ( processWidth , processHeight ) ;
93
+ const ctx = offscreen . getContext ( "2d" ) ! ;
94
+ ctx . drawImage ( image , 0 , 0 , processWidth , processHeight ) ;
95
+ const newImg = ctx . getImageData ( 0 , 0 , processWidth , processHeight ) ;
69
96
70
- let prediction
97
+ let prediction ;
71
98
if ( params . type === BodypixFunctionType . SegmentPerson ) {
72
- prediction = await model ! . segmentPerson ( newImg , params . segmentPersonParams )
99
+ prediction = await model ! . segmentPerson (
100
+ newImg ,
101
+ params . segmentPersonParams
102
+ ) ;
73
103
} else if ( params . type === BodypixFunctionType . SegmentPersonParts ) {
74
- prediction = await model ! . segmentPersonParts ( newImg , params . segmentPersonPartsParams )
104
+ prediction = await model ! . segmentPersonParts (
105
+ newImg ,
106
+ params . segmentPersonPartsParams
107
+ ) ;
75
108
} else if ( params . type === BodypixFunctionType . SegmentMultiPerson ) {
76
- prediction = await model ! . segmentMultiPerson ( newImg , params . segmentMultiPersonParams )
109
+ prediction = await model ! . segmentMultiPerson (
110
+ newImg ,
111
+ params . segmentMultiPersonParams
112
+ ) ;
77
113
} 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 )
114
+ prediction = await model ! . segmentMultiPersonParts (
115
+ newImg ,
116
+ params . segmentMultiPersonPartsParams
117
+ ) ;
118
+ } else {
119
+ // segmentPersonに倒す
120
+ prediction = await model ! . segmentPerson (
121
+ newImg ,
122
+ params . segmentPersonParams
123
+ ) ;
81
124
}
82
- return prediction
83
- }
84
-
125
+ return prediction ;
126
+ } ;
85
127
86
128
onmessage = async ( event ) => {
87
129
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
- } )
130
+ const config = event . data . config as BodyPixConfig ;
131
+ await load_module ( config ) ;
132
+ bodyPix . load ( event . data . config . model ) . then ( ( res ) => {
133
+ console . log ( "bodypix loaded default" , event . data . config ) ;
134
+ model = res ;
135
+ ctx . postMessage ( { message : WorkerResponse . INITIALIZED } ) ;
136
+ } ) ;
95
137
} 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
138
+ const config : BodyPixConfig = event . data . config ;
139
+ const image : ImageBitmap = event . data . image ;
140
+ const uid : number = event . data . uid ;
141
+ const params : BodyPixOperatipnParams = event . data . params ;
100
142
101
- const prediction = await predict ( image , config , params )
102
- ctx . postMessage ( { message : WorkerResponse . PREDICTED , uid : uid , prediction : prediction } )
143
+ const prediction = await predict ( image , config , params ) ;
144
+ ctx . postMessage ( {
145
+ message : WorkerResponse . PREDICTED ,
146
+ uid : uid ,
147
+ prediction : prediction ,
148
+ } ) ;
103
149
}
104
- }
150
+ } ;
0 commit comments