-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkeypoint-stream-controls.js
More file actions
366 lines (325 loc) · 13.3 KB
/
keypoint-stream-controls.js
File metadata and controls
366 lines (325 loc) · 13.3 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* 关键点流控制面板
*/
class KeypointStreamControls {
constructor() {
this.isRunning = false;
this.demoMode = false;
this.container = null;
this.statusElem = null;
this.startBtn = null;
this.stopBtn = null;
this.demoBtn = null;
this.networkSelect = null;
this.streamFrame = null;
// 网络配置文件选项
this.networkProfiles = [
{ id: 'high', name: '高速网络 (5Mbps)' },
{ id: 'medium', name: '中速网络 (1Mbps)' },
{ id: 'low', name: '低速网络 (300Kbps)' },
{ id: 'unstable', name: '不稳定网络' },
{ id: 'mobile', name: '移动网络 (500Kbps)' }
];
}
init() {
// 创建控制面板
this.createPanel();
// 获取初始状态
this.fetchStatus();
// 每5秒更新一次状态
setInterval(() => this.fetchStatus(), 5000);
// 检查是否已有参考帧
this.checkReferenceStatus();
}
createPanel() {
// 创建面板容器
this.container = document.createElement('div');
this.container.id = 'keypoint-stream-controls';
this.container.style.cssText = `
position: fixed;
bottom: 250px;
left: 20px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 15px;
border-radius: 5px;
font-family: Arial, sans-serif;
z-index: 1000;
width: 300px;
`;
// 创建标题
const title = document.createElement('h3');
title.textContent = '关键点流控制';
title.style.cssText = 'margin: 0 0 10px 0; font-size: 16px;';
this.container.appendChild(title);
// 创建状态显示
this.statusElem = document.createElement('div');
this.statusElem.textContent = '状态: 加载中...';
this.statusElem.style.cssText = 'margin-bottom: 15px; font-size: 14px;';
this.container.appendChild(this.statusElem);
// 创建流视图
this.streamFrame = document.createElement('div');
this.streamFrame.style.cssText = `
width: 100%;
height: 200px;
background: #000;
margin-bottom: 15px;
border-radius: 4px;
overflow: hidden;
display: none;
`;
const streamImg = document.createElement('img');
streamImg.id = 'keypoint-stream-preview';
streamImg.style.cssText = 'width: 100%; height: 100%; object-fit: contain;';
streamImg.alt = '关键点流预览';
this.streamFrame.appendChild(streamImg);
this.container.appendChild(this.streamFrame);
// 创建网络配置选择器
const networkLabel = document.createElement('div');
networkLabel.textContent = '网络配置:';
networkLabel.style.cssText = 'margin-bottom: 5px; font-size: 14px;';
this.container.appendChild(networkLabel);
this.networkSelect = document.createElement('select');
this.networkSelect.style.cssText = `
width: 100%;
padding: 8px;
margin-bottom: 15px;
background: #333;
color: white;
border: 1px solid #555;
border-radius: 4px;
`;
this.networkProfiles.forEach(profile => {
const option = document.createElement('option');
option.value = profile.id;
option.textContent = profile.name;
if (profile.id === 'medium') {
option.selected = true;
}
this.networkSelect.appendChild(option);
});
this.networkSelect.addEventListener('change', () => {
this.setNetworkProfile(this.networkSelect.value);
});
this.container.appendChild(this.networkSelect);
// 创建按钮组
const btnContainer = document.createElement('div');
btnContainer.style.cssText = 'display: flex; gap: 10px;';
// 启动按钮
this.startBtn = document.createElement('button');
this.startBtn.textContent = '启动流';
this.startBtn.style.cssText = `
padding: 8px 12px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
flex: 1;
`;
this.startBtn.onclick = () => this.startStream();
btnContainer.appendChild(this.startBtn);
// 停止按钮
this.stopBtn = document.createElement('button');
this.stopBtn.textContent = '停止流';
this.stopBtn.style.cssText = `
padding: 8px 12px;
background: #F44336;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
flex: 1;
`;
this.stopBtn.disabled = true;
this.stopBtn.onclick = () => this.stopStream();
btnContainer.appendChild(this.stopBtn);
this.container.appendChild(btnContainer);
// 演示模式按钮
this.demoBtn = document.createElement('button');
this.demoBtn.textContent = '启用演示模式';
this.demoBtn.style.cssText = `
padding: 8px 12px;
background: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
margin-top: 10px;
`;
this.demoBtn.onclick = () => this.toggleDemoMode();
this.container.appendChild(this.demoBtn);
// 添加到文档
document.body.appendChild(this.container);
}
fetchStatus() {
fetch('/keypoint_stream/status')
.then(response => response.json())
.then(data => {
if (data.success) {
const status = data.status;
this.isRunning = status.running;
this.demoMode = status.demo_mode;
// 更新状态显示
let statusText = `状态: ${this.isRunning ? '运行中' : '已停止'}`;
if (this.isRunning) {
statusText += `<br>演示模式: ${this.demoMode ? '已启用' : '已禁用'}`;
if (status.receiver && status.receiver.current_fps) {
statusText += `<br>FPS: ${status.receiver.current_fps.toFixed(1)}`;
}
if (status.network) {
statusText += `<br>带宽: ${status.network.current_usage_kbps.toFixed(1)}Kbps`;
statusText += `<br>丢包率: ${(status.network.packet_loss * 100).toFixed(1)}%`;
}
}
this.statusElem.innerHTML = statusText;
// 更新按钮状态
this.startBtn.disabled = this.isRunning;
this.stopBtn.disabled = !this.isRunning;
this.demoBtn.textContent = this.demoMode ? '禁用演示模式' : '启用演示模式';
this.demoBtn.style.background = this.demoMode ? '#FF9800' : '#2196F3';
// 更新流预览
if (this.isRunning && !this.streamFrame.style.display) {
this.streamFrame.style.display = 'block';
document.getElementById('keypoint-stream-preview').src = '/keypoint_video_feed';
} else if (!this.isRunning && this.streamFrame.style.display !== 'none') {
this.streamFrame.style.display = 'none';
document.getElementById('keypoint-stream-preview').src = '';
}
}
})
.catch(error => {
console.error('获取关键点流状态失败:', error);
this.statusElem.innerHTML = '状态: 获取失败<br>错误: ' + error.message;
});
}
checkReferenceStatus() {
fetch('/reference_status')
.then(response => response.json())
.then(data => {
if (data.success && data.has_reference) {
this.startBtn.disabled = false;
} else {
this.startBtn.disabled = true;
this.statusElem.innerHTML = '状态: 需要先捕获参考帧';
}
})
.catch(error => {
console.error('获取参考帧状态失败:', error);
});
}
startStream() {
this.statusElem.innerHTML = '状态: 正在启动...';
this.startBtn.disabled = true;
fetch('/keypoint_stream/start', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ demo_mode: this.demoMode })
})
.then(response => response.json())
.then(data => {
if (data.success) {
this.isRunning = true;
this.statusElem.innerHTML = '状态: 已启动';
this.stopBtn.disabled = false;
// 显示流预览
this.streamFrame.style.display = 'block';
document.getElementById('keypoint-stream-preview').src = '/keypoint_video_feed';
// 更新状态
this.fetchStatus();
} else {
this.statusElem.innerHTML = `状态: 启动失败<br>错误: ${data.error || '未知错误'}`;
this.startBtn.disabled = false;
}
})
.catch(error => {
console.error('启动关键点流失败:', error);
this.statusElem.innerHTML = '状态: 启动失败<br>错误: ' + error.message;
this.startBtn.disabled = false;
});
}
stopStream() {
this.statusElem.innerHTML = '状态: 正在停止...';
this.stopBtn.disabled = true;
fetch('/keypoint_stream/stop', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
if (data.success) {
this.isRunning = false;
this.statusElem.innerHTML = '状态: 已停止';
this.startBtn.disabled = false;
// 隐藏流预览
this.streamFrame.style.display = 'none';
document.getElementById('keypoint-stream-preview').src = '';
} else {
this.statusElem.innerHTML = `状态: 停止失败<br>错误: ${data.error || '未知错误'}`;
this.stopBtn.disabled = false;
}
})
.catch(error => {
console.error('停止关键点流失败:', error);
this.statusElem.innerHTML = '状态: 停止失败<br>错误: ' + error.message;
this.stopBtn.disabled = false;
});
}
toggleDemoMode() {
const enable = !this.demoMode;
fetch('/keypoint_stream/demo', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ enable })
})
.then(response => response.json())
.then(data => {
if (data.success) {
this.demoMode = data.demo_mode;
this.demoBtn.textContent = this.demoMode ? '禁用演示模式' : '启用演示模式';
this.demoBtn.style.background = this.demoMode ? '#FF9800' : '#2196F3';
// 如果流已经在运行,需要重启流应用新设置
if (this.isRunning) {
this.statusElem.innerHTML = '状态: 正在重启流...';
this.stopStream();
setTimeout(() => this.startStream(), 1000);
}
}
})
.catch(error => {
console.error('切换演示模式失败:', error);
});
}
setNetworkProfile(profile) {
fetch('/network/set_profile', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ profile })
})
.then(response => response.json())
.then(data => {
if (data.success) {
this.statusElem.innerHTML = `状态: 已设置网络为 ${profile}`;
// 更新状态
setTimeout(() => this.fetchStatus(), 1000);
} else {
this.statusElem.innerHTML = `状态: 设置网络失败<br>错误: ${data.error || '未知错误'}`;
}
})
.catch(error => {
console.error('设置网络配置失败:', error);
this.statusElem.innerHTML = '状态: 设置网络失败<br>错误: ' + error.message;
});
}
}
// 页面加载完成后初始化控制面板
document.addEventListener('DOMContentLoaded', function() {
const controls = new KeypointStreamControls();
controls.init();
});