-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
187 lines (128 loc) · 4.08 KB
/
script.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
let RUNNING = false;
let is_primary = true;
let server_url = undefined;
(function(){
let params = ((location.search+" ").substring(1)).trim().split("&");
params = params.map(param => param.split("="));
for(let i=0;i<params.length;i++){
let key = params[i][0];
let value = params[i][1];
if(key == "ws"){
is_primary = false;
server_url = value;
}
}
if(is_primary){
load_visualization();
}
document.body.setAttribute("data-primary",is_primary);
})();
document.querySelector(".button").onclick = function(){
RUNNING = !RUNNING;
if(RUNNING){
this.innerHTML = "Stop";
}else{
this.innerHTML = "Start";
}
}
const process_pose = (function(){
let websocket;
let connected = false;
const setup_websocket = async function(ws_url = undefined){
let qr_code_url;
if(ws_url == undefined){
[ws_url, qr_code_url] = await find_local_url();
}
let websocket = await connect_to_websocket(ws_url);
if(qr_code_url != undefined){
new QRCode(document.querySelector(".qr-code"), qr_code_url);
}
websocket.onmessage = function(message){
if(typeof websocket.data_callback !== 'function'){
return;
}
let json = JSON.parse(message.data);
let vr_trackers = json.vr_trackers;
let current_positions = vr_trackers.map(tracker => new VRObject(tracker));
websocket.data_callback(current_positions);
}
return websocket;
}
const ping_pong = function(){
return new Promise(
function(resolve, reject){
websocket.data_callback = resolve;
websocket.send("");
setTimeout(function(){
reject();
}, 1000)
}
);
}
return async function(best_pose){
if(!connected){
websocket = await setup_websocket(server_url);
connected = true;
update_connected_status(connected);
}
let latest_devices = await ping_pong();
let [HMD, left_controller, right_controller] = find_hmd_controllers(latest_devices);
const openvr_tracked_pose = new Pose3D();
copy_position(HMD, openvr_tracked_pose.hmd, 1);
copy_position(left_controller, openvr_tracked_pose.left_controller, 1);
copy_position(right_controller, openvr_tracked_pose.right_controller, 1);
copy_position(best_pose.nose, best_pose.hmd, 1);
copy_position(best_pose.left_index, best_pose.left_controller, 1);
copy_position(best_pose.right_index, best_pose.right_controller, 1);
const openvr_mapped_pose = align_poses(best_pose, openvr_tracked_pose, false);
const output = [];
point_to_websocket("left_elbow", openvr_mapped_pose, output);
point_to_websocket("right_elbow", openvr_mapped_pose, output);
point_to_websocket("left_hip", openvr_mapped_pose, output);
point_to_websocket("right_hip", openvr_mapped_pose, output);
point_to_websocket("left_heel", openvr_mapped_pose, output);
point_to_websocket("right_heel", openvr_mapped_pose, output);
websocket.send(JSON.stringify(output));
try{
openvr_visualiser.display_pose(openvr_mapped_pose);
}catch(error){}
}
})();
const video_element = document.createElement("video");
const canvas_element = document.createElement("canvas");
document.querySelector("section .video-feed").appendChild(video_element);
document.querySelector("section .video-feed").appendChild(canvas_element);
update_status("Initializing - Creating camera");
const pose_network = new PoseNetwork();
const neural_net_results = (function(){
const queued_process_pose = new QueueFunction(process_pose);
return function(results){
if(!results.poseWorldLandmarks){
return;
}
try{
annotate_camera(canvas_element, results);
}catch(error){};
update_status("Running");
const pose = new Pose3D(results.poseWorldLandmarks);
try{
pose_visualiser.display_pose(pose);
}catch(error){}
queued_process_pose.run(pose);
}
})();
const new_frame = async function(){
if(RUNNING){
await pose_network.send({ image: video_element });
}
}
pose_network.onResults = neural_net_results;
const camera = new Camera(video_element, {
onFrame: async() => {
await new_frame();
},
width: 1280,
height: 720
});
update_status("Ready, waiting start");
camera.start();