Paradise151 commited on
Commit
b4a777f
·
verified ·
1 Parent(s): 1ff78df

Create indexRealTime.html

Browse files
Files changed (1) hide show
  1. indexRealTime.html +133 -0
indexRealTime.html ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script>
2
+ const video = document.getElementById('video');
3
+ const resultImg = document.getElementById('resultImg');
4
+ const jsonOut = document.getElementById('jsonOut');
5
+ const startBtn = document.getElementById('startBtn');
6
+ const stopBtn = document.getElementById('stopBtn');
7
+ const cameraSelect = document.getElementById('cameraSelect');
8
+ const confSlider = document.getElementById('confSlider');
9
+ const iouSlider = document.getElementById('iouSlider');
10
+ const confVal = document.getElementById('confVal');
11
+ const iouVal = document.getElementById('iouVal');
12
+ const modeSelect = document.getElementById('modeSelect');
13
+
14
+ let stream = null;
15
+ let active = false;
16
+ let currentDeviceId = null;
17
+ let busy = false; // сервер занят
18
+ let pendingFrame = null; // последний кадр в буфере
19
+
20
+ confSlider.oninput = () => confVal.textContent = confSlider.value;
21
+ iouSlider.oninput = () => iouVal.textContent = iouSlider.value;
22
+
23
+ async function enumerateCameras() {
24
+ const devices = await navigator.mediaDevices.enumerateDevices();
25
+ cameraSelect.innerHTML = '';
26
+ const cams = devices.filter(d => d.kind === 'videoinput');
27
+ cams.forEach((cam, i) => {
28
+ const opt = document.createElement('option');
29
+ opt.value = cam.deviceId || i;
30
+ opt.textContent = cam.label || `Камера ${i+1}`;
31
+ cameraSelect.appendChild(opt);
32
+ });
33
+ if (cams.length > 0) currentDeviceId = cams[0].deviceId;
34
+ }
35
+
36
+ async function startCamera(deviceId) {
37
+ if (stream) stopCamera();
38
+ const constraints = {
39
+ video: {
40
+ deviceId: deviceId ? { exact: deviceId } : undefined,
41
+ facingMode: 'environment',
42
+ width: { ideal: 720 },
43
+ height: { ideal: 720 }
44
+ },
45
+ audio: false
46
+ };
47
+ stream = await navigator.mediaDevices.getUserMedia(constraints);
48
+ video.srcObject = stream;
49
+ await video.play();
50
+ requestAnimationFrame(captureLoop);
51
+ }
52
+
53
+ function stopCamera() {
54
+ if (stream) {
55
+ stream.getTracks().forEach(t => t.stop());
56
+ stream = null;
57
+ }
58
+ }
59
+
60
+ cameraSelect.onchange = async () => {
61
+ currentDeviceId = cameraSelect.value;
62
+ await startCamera(currentDeviceId);
63
+ };
64
+
65
+ function captureLoop() {
66
+ if (!active || !stream) return;
67
+
68
+ const canvas = document.createElement('canvas');
69
+ const w = Math.min(720, video.videoWidth || 640);
70
+ const h = Math.floor(w * (video.videoHeight / video.videoWidth));
71
+ canvas.width = w;
72
+ canvas.height = h;
73
+ const ctx = canvas.getContext('2d');
74
+ ctx.drawImage(video, 0, 0, w, h);
75
+
76
+ canvas.toBlob(blob => {
77
+ pendingFrame = blob; // сохраняем последний кадр
78
+ if (!busy) sendFrame(); // если сервер свободен — отправляем
79
+ }, 'image/jpeg', 0.9);
80
+
81
+ requestAnimationFrame(captureLoop);
82
+ }
83
+
84
+ async function sendFrame() {
85
+ if (!pendingFrame) return;
86
+ busy = true;
87
+
88
+ const formData = new FormData();
89
+ formData.append('file', pendingFrame, 'frame.jpg');
90
+ formData.append('conf', confSlider.value);
91
+ formData.append('iou', iouSlider.value);
92
+ const returnImage = (modeSelect.value === 'image') ? 1 : 0;
93
+ formData.append('return_image', returnImage.toString());
94
+
95
+ pendingFrame = null; // очищаем буфер
96
+
97
+ try {
98
+ const resp = await fetch('/predict', { method: 'POST', body: formData });
99
+ if (returnImage === 1) {
100
+ const arrBuf = await resp.arrayBuffer();
101
+ const blobRes = new Blob([arrBuf], { type: 'image/jpeg' });
102
+ resultImg.src = URL.createObjectURL(blobRes);
103
+ resultImg.style.display = 'block';
104
+ jsonOut.style.display = 'none';
105
+ } else {
106
+ const data = await resp.json();
107
+ jsonOut.textContent = JSON.stringify(data, null, 2);
108
+ jsonOut.style.display = 'block';
109
+ resultImg.style.display = 'none';
110
+ }
111
+ } catch (e) {
112
+ console.error("Ошибка запроса:", e);
113
+ }
114
+
115
+ busy = false;
116
+ if (pendingFrame) sendFrame(); // если уже есть новый кадр — сразу отправляем
117
+ }
118
+
119
+ startBtn.onclick = async () => {
120
+ await enumerateCameras();
121
+ await startCamera(currentDeviceId);
122
+ active = true;
123
+ startBtn.disabled = true;
124
+ stopBtn.disabled = false;
125
+ };
126
+
127
+ stopBtn.onclick = () => {
128
+ active = false;
129
+ startBtn.disabled = false;
130
+ stopBtn.disabled = true;
131
+ stopCamera();
132
+ };
133
+ </script>