Paradise151 commited on
Commit
3d7f80c
·
verified ·
1 Parent(s): ca55620

Create index.html

Browse files
Files changed (1) hide show
  1. index.html +160 -0
index.html ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>GrechnikNet — Snapshot Detection</title>
6
+ <style>
7
+ body { background:#111; color:#eee; font-family:sans-serif; margin:0; text-align:center; }
8
+ header { padding:16px; }
9
+ video, img { width: min(100vw, 720px); height:auto; border:1px solid #333; border-radius:8px; margin:10px 0; }
10
+ .controls { display:flex; gap:16px; flex-wrap:wrap; justify-content:center; margin:16px 0; }
11
+ .control { background:#1b1b1b; padding:10px 12px; border-radius:8px; }
12
+ label { display:block; font-size:14px; margin-bottom:6px; }
13
+ input[type="range"] { width:200px; }
14
+ button { padding:10px 16px; border:none; border-radius:6px; background:#3a6df0; color:#fff; cursor:pointer; }
15
+ button:disabled { background:#555; cursor:not-allowed; }
16
+ pre { text-align:left; background:#0e0e0e; padding:12px; border-radius:8px; width:min(100vw,720px); margin:10px auto; }
17
+ </style>
18
+ </head>
19
+ <body>
20
+ <header>
21
+ <h2>GrechnikNet — Snapshot Detection</h2>
22
+ <p>Сверху поток с камеры, снизу результат последнего снимка</p>
23
+ </header>
24
+
25
+ <!-- Панель управления -->
26
+ <div class="controls">
27
+ <div class="control">
28
+ <label>Камера:</label>
29
+ <select id="cameraSelect"></select>
30
+ </div>
31
+ <div class="control">
32
+ <label>Confidence: <span id="confVal">0.25</span></label>
33
+ <input type="range" id="confSlider" min="0" max="1" step="0.05" value="0.25">
34
+ </div>
35
+ <div class="control">
36
+ <label>IoU: <span id="iouVal">0.45</span></label>
37
+ <input type="range" id="iouSlider" min="0" max="1" step="0.05" value="0.45">
38
+ </div>
39
+ <div class="control">
40
+ <label>Режим ответа:</label>
41
+ <select id="modeSelect">
42
+ <option value="image">Аннотированное изображение</option>
43
+ <option value="json">Только боксы (JSON)</option>
44
+ </select>
45
+ </div>
46
+ <div class="control">
47
+ <button id="snapBtn">Сделать фото</button>
48
+ </div>
49
+ </div>
50
+
51
+ <!-- Верхнее окно: поток -->
52
+ <video id="video" autoplay playsinline muted></video>
53
+
54
+ <!-- Нижнее окно: результат -->
55
+ <img id="resultImg" alt="Результат появится здесь">
56
+ <pre id="jsonOut" style="display:none;"></pre>
57
+
58
+ <script>
59
+ const video = document.getElementById('video');
60
+ const resultImg = document.getElementById('resultImg');
61
+ const jsonOut = document.getElementById('jsonOut');
62
+ const snapBtn = document.getElementById('snapBtn');
63
+ const cameraSelect = document.getElementById('cameraSelect');
64
+ const confSlider = document.getElementById('confSlider');
65
+ const iouSlider = document.getElementById('iouSlider');
66
+ const confVal = document.getElementById('confVal');
67
+ const iouVal = document.getElementById('iouVal');
68
+ const modeSelect = document.getElementById('modeSelect');
69
+
70
+ let stream = null;
71
+ let currentDeviceId = null;
72
+
73
+ confSlider.oninput = () => confVal.textContent = confSlider.value;
74
+ iouSlider.oninput = () => iouVal.textContent = iouSlider.value;
75
+
76
+ async function enumerateCameras() {
77
+ const devices = await navigator.mediaDevices.enumerateDevices();
78
+ cameraSelect.innerHTML = '';
79
+ const cams = devices.filter(d => d.kind === 'videoinput');
80
+ cams.forEach((cam, i) => {
81
+ const opt = document.createElement('option');
82
+ opt.value = cam.deviceId || i;
83
+ opt.textContent = cam.label || `Камера ${i+1}`;
84
+ cameraSelect.appendChild(opt);
85
+ });
86
+ if (cams.length > 0) currentDeviceId = cams[0].deviceId;
87
+ }
88
+
89
+ async function startCamera(deviceId) {
90
+ if (stream) stopCamera();
91
+ const constraints = {
92
+ video: {
93
+ deviceId: deviceId ? { exact: deviceId } : undefined,
94
+ facingMode: 'environment',
95
+ width: { ideal: 720 },
96
+ height: { ideal: 720 }
97
+ },
98
+ audio: false
99
+ };
100
+ stream = await navigator.mediaDevices.getUserMedia(constraints);
101
+ video.srcObject = stream;
102
+ await video.play();
103
+ }
104
+
105
+ function stopCamera() {
106
+ if (stream) {
107
+ stream.getTracks().forEach(t => t.stop());
108
+ stream = null;
109
+ }
110
+ }
111
+
112
+ cameraSelect.onchange = async () => {
113
+ currentDeviceId = cameraSelect.value;
114
+ await startCamera(currentDeviceId);
115
+ };
116
+
117
+ async function takePhoto() {
118
+ if (!video.srcObject) return;
119
+
120
+ const canvas = document.createElement('canvas');
121
+ const w = Math.min(720, video.videoWidth || 640);
122
+ const h = Math.floor(w * (video.videoHeight / video.videoWidth));
123
+ canvas.width = w;
124
+ canvas.height = h;
125
+ const ctx = canvas.getContext('2d');
126
+ ctx.drawImage(video, 0, 0, w, h);
127
+
128
+ const blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/jpeg', 0.9));
129
+ const formData = new FormData();
130
+ formData.append('file', blob, 'frame.jpg');
131
+ formData.append('conf', confSlider.value);
132
+ formData.append('iou', iouSlider.value);
133
+ const returnImage = (modeSelect.value === 'image') ? 1 : 0;
134
+ formData.append('return_image', returnImage.toString());
135
+
136
+ const resp = await fetch('/predict', { method: 'POST', body: formData });
137
+ if (returnImage === 1) {
138
+ const arrBuf = await resp.arrayBuffer();
139
+ const blobRes = new Blob([arrBuf], { type: 'image/jpeg' });
140
+ resultImg.src = URL.createObjectURL(blobRes);
141
+ resultImg.style.display = 'block';
142
+ jsonOut.style.display = 'none';
143
+ } else {
144
+ const data = await resp.json();
145
+ jsonOut.textContent = JSON.stringify(data, null, 2);
146
+ jsonOut.style.display = 'block';
147
+ resultImg.style.display = 'none';
148
+ }
149
+ }
150
+
151
+ snapBtn.onclick = takePhoto;
152
+
153
+ // Автозапуск камеры при загрузке
154
+ (async () => {
155
+ await enumerateCameras();
156
+ await startCamera(currentDeviceId);
157
+ })();
158
+ </script>
159
+ </body>
160
+ </html>