-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
185 lines (160 loc) · 4.63 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
=============
Params
=============
*/
// let imageSize = {
// width: 640,
// height: 480
// };
let capture;
let pixelNoiseSlider;
let mic;
// ===== BodyPix =====
let modelConfigParams;
let mobilenetConfigParams = {
architecture: 'MobileNetV1',
outputStride: 16,
multiplier: 0.75,
quantBytes: 2
}
let resnetConfigParams = {
architecture: 'ResNet50',
outputStride: 16,
quantBytes: 2
}
let enableMultiPersonSegment;
let numberOfPredict = 10;
let segmentation;
let segmentations;
/*
=============
Function
=============
*/
function setup() {
let cnv = createCanvas(windowWidth,windowHeight);
cnv.mouseClicked(userStartAudio); // Start handling Audio when the screen is clicked
// init bodypix param
//modelConfigParams = mobilenetConfigParams;
modelConfigParams = resnetConfigParams;
enableMultiPersonSegment = false;
// to get microphone volume
mic = new p5.AudioIn();
mic.start();
setupCamera();
background(255);
text("Wait for setup...", 20, 20);
pixelNoiseSlider = createSlider(0,500,300);
pixelNoiseSlider.position(20,50);
}
function setupCamera(id = ""){
if(id == ""){
capture = createCapture(VIDEO, captureLoaded);
}else{
let option = {
video: {
deviceId: id,
// width: imageSize.width,
// height: imageSize.height
},
audio: true
};
capture = createCapture(option, captureLoaded);
}
capture.hide();
}
function captureLoaded(){
console.log("capture loaded...");
loadTfModel();
}
function loadTfModel(){
bodyPix.load(modelConfigParams).then(function(loadedNet){
net = loadedNet;
console.log("bodyPix loaded...");
predict();
})
}
function predict() {
if(enableMultiPersonSegment){
net.segmentMultiPerson(capture.elt, {
flipHorizontal: false,
internalResolution: 'medium',
segmentationThreshold: 0.7,
maxDetections: numberOfPredict
}).then(function(personSegmentation){
segmentations = personSegmentation;
drawWhenPredicted();
predict();
})
}else{
net.segmentPerson(capture.elt, {
flipHorizontal: false,
internalResolution: 'medium',
segmentationThreshold: 0.7
}).then(function(personSegmentation){
segmentation = personSegmentation;
drawWhenPredicted();
predict();
})
}
}
function drawWhenPredicted(){
background(255);
// image(capture,0,0,windowWidth,windowHeight);
push();
scale(-1,1);
image(capture, -windowWidth, 0, windowWidth, windowHeight);
pop();
if(segmentation != undefined || segmentations != undefined){
updateBodyPixPixels().then(() => console.log("updated pixels..."));
}
fill(255,150);
rect(0, 0, 180, 80);
fill(0);
text("'f'key: fullscreen", 20, 20);
text('PixelNoise: '+String(pixelNoiseSlider.value()), pixelNoiseSlider.x, pixelNoiseSlider.y-10);
}
async function updateBodyPixPixels(){
let img = createImage(segmentation.width, segmentation.height);
img.loadPixels();
capture.loadPixels();
// Update bodypix's segmented pixels
for(let y=0;y<img.height;y++){
let param = pixelNoiseSlider.value();
let pixelNoise = Math.floor(noise(y*mic.getLevel()*0.2)*param-param/2);
for(let x=0;x<img.width;x++){
let index = (x + y * img.width);
if(segmentation.data[index]){
let index_ = index+pixelNoise
if(index_>img.width*img.height){
index_=index;
}
img.pixels[index*4] = capture.pixels[index_*4];
img.pixels[index*4 + 1] = capture.pixels[index_*4 + 1];
img.pixels[index*4 + 2] = capture.pixels[index_*4 + 2];
img.pixels[index*4 + 3] = capture.pixels[index_*4 + 3];
}
}
}
img.updatePixels();
capture.updatePixels();
// image(img, 0, 0, windowWidth, windowHeight);
push();
scale(-1,1);
image(img, -windowWidth, 0, windowWidth, windowHeight);
pop();
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function keyTyped() {
switch (key) {
case 'f':
let fs = fullscreen();
fullscreen(!fs);
break;
default:
break;
}
}