-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_websocket.js
executable file
·243 lines (206 loc) · 6.49 KB
/
camera_websocket.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as posenet from '@tensorflow-models/posenet';
import Stats from 'stats.js';
import {drawBoundingBox, drawKeypoints, drawSkeleton, isMobile, toggleLoadingUI} from './demo_util';
import { http } from '@tensorflow/tfjs-core/dist/io/io';
const videoWidth = 900;
const videoHeight = 750;
const stats = new Stats();
const websocketFlag = 1;
const websocketConf = 'ws://127.0.0.1:8091/ws';
var w;
if(websocketFlag){
var w = new WebSocket(websocketConf);
w.onopen = function(){
console.log("Open web socket");
}
w.onmessage = function(e){
console.log(e.data.toString());
}
w.onclose = function(e){
console.log("Session has been closed");
}
w.onerror = function(e){
console.log("Error");
console.log(e);
}
}
/**
* Loads a the camera to be used in the demo
*
*/
async function setupCamera() {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error(
'Browser API navigator.mediaDevices.getUserMedia not available');
}
const video = document.getElementById('video');
video.width = videoWidth;
video.height = videoHeight;
const mobile = isMobile();
const stream = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': {
facingMode: 'user',
width: mobile ? undefined : videoWidth,
height: mobile ? undefined : videoHeight,
},
});
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
};
});
}
async function loadVideo() {
const video = await setupCamera();
video.play();
return video;
}
const defaultQuantBytes = 2;
const defaultMobileNetMultiplier = 0.5;
const defaultMobileNetStride = 16;
const defaultMobileNetInputResolution = 257;
const parameters = {
algorithm: 'multi-pose',
input: {
architecture: 'MobileNetV1',
outputStride: defaultMobileNetStride,
inputResolution: defaultMobileNetInputResolution,
multiplier: defaultMobileNetMultiplier,
quantBytes: defaultQuantBytes
},
multiPoseDetection: {
minPoseConfidence: 0.1,
minPartConfidence: 0.5,
},
output: {
showVideo: true,
showSkeleton: true,
showPoints: true,
showBoundingBox: false,
},
net: null,
};
/**
* Sets up dat.gui controller on the top-right of the window
*/
function setupParam(cameras, net) {
parameters.net = net;
if (cameras.length > 0) {
parameters.camera = cameras[0].deviceId;
}
}
/**
* Sets up a frames per second panel on the top-left of the window
*/
function setupFPS() {
stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
document.getElementById('main').appendChild(stats.dom);
}
/**
* Feeds an image to posenet to estimate poses - this is where the magic
* happens. This function loops with a requestAnimationFrame method.
*/
function detectPoseInRealTime(video, net) {
const canvas = document.getElementById('output');
const ctx = canvas.getContext('2d');
// since images are being fed from a webcam, we want to feed in the
// original image and then just flip the keypoints' x coordinates. If instead
// we flip the image, then correcting left-right keypoint pairs requires a
// permutation on all the keypoints.
const flipPoseHorizontal = true;
canvas.width = videoWidth;
canvas.height = videoHeight;
async function poseDetectionFrame() {
// Begin monitoring code for frames per second
stats.begin();
let poses = [];
let minPoseConfidence;
let minPartConfidence;
const pose = await parameters.net.estimatePoses(video, {
flipHorizontal: flipPoseHorizontal,
decodingMethod: 'multi-person'
});
poses = poses.concat(pose);
minPoseConfidence = +parameters.multiPoseDetection.minPoseConfidence;
minPartConfidence = +parameters.multiPoseDetection.minPartConfidence;
ctx.clearRect(0, 0, videoWidth, videoHeight);
ctx.save();
ctx.scale(-1, 1);
ctx.translate(-videoWidth, 0);
ctx.drawImage(video, 0, 0, videoWidth, videoHeight);
ctx.restore();
// For each pose (i.e. person) detected in an image, loop through the poses
// and draw the resulting skeleton and keypoints if over certain confidence
// scores
if(websocketFlag)
w.send(JSON.stringify(poses));
// console.log(JSON.stringify(poses));
poses.forEach(({score, keypoints}) => {
if (score >= minPoseConfidence) {
if (parameters.output.showPoints) {
drawKeypoints(keypoints, minPartConfidence, ctx);
}
if (parameters.output.showSkeleton) {
drawSkeleton(keypoints, minPartConfidence, ctx);
}
if (parameters.output.showBoundingBox) {
drawBoundingBox(keypoints, ctx);
}
}
});
// End monitoring code for frames per second
stats.end();
requestAnimationFrame(poseDetectionFrame);
}
poseDetectionFrame();
}
/**
* Kicks off the demo by loading the posenet model, finding and loading
* available camera devices, and setting off the detectPoseInRealTime function.
*/
export async function bindPage() {
toggleLoadingUI(true);
const net = await posenet.load({
architecture: parameters.input.architecture,
outputStride: parameters.input.outputStride,
inputResolution: parameters.input.inputResolution,
multiplier: parameters.input.multiplier,
quantBytes: parameters.input.quantBytes
});
toggleLoadingUI(false);
let video;
try {
video = await loadVideo();
} catch (e) {
let info = document.getElementById('info');
info.textContent = 'this browser does not support video capture,' +
'or this device does not have a camera';
info.style.display = 'block';
throw e;
}
setupParam([], net);
// setupFPS();
detectPoseInRealTime(video, net);
}
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// kick off the demo
bindPage();