File size: 27,413 Bytes
a33a4a5
 
 
7a283eb
 
 
 
 
2135c8f
7a283eb
 
 
 
 
 
 
 
 
 
2135c8f
 
 
7a283eb
2135c8f
7a283eb
2135c8f
7a283eb
 
 
2135c8f
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
7a283eb
 
2135c8f
7a283eb
2135c8f
7a283eb
2135c8f
 
 
 
 
 
7a283eb
2135c8f
 
7a283eb
2135c8f
7a283eb
 
 
 
2135c8f
 
7a283eb
 
 
 
 
2135c8f
 
 
7a283eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2135c8f
7a283eb
 
2135c8f
 
 
 
 
 
 
 
 
 
 
 
7a283eb
 
 
2135c8f
 
7a283eb
 
 
2135c8f
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
7a283eb
2135c8f
 
 
 
 
7a283eb
2135c8f
 
 
 
 
7a283eb
2135c8f
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
7a283eb
2135c8f
 
7a283eb
 
2135c8f
 
7a283eb
 
2135c8f
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
7a283eb
 
 
a33a4a5
 
2135c8f
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
7a283eb
 
2135c8f
 
 
 
 
 
 
 
 
7a283eb
 
2135c8f
 
 
7a283eb
 
 
2135c8f
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
 
 
 
 
2135c8f
7a283eb
 
 
 
 
 
 
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
 
2135c8f
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
7a283eb
 
 
 
 
 
 
 
 
 
 
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
7a283eb
 
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
 
2135c8f
 
7a283eb
 
2135c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
7a283eb
2135c8f
 
 
 
 
 
 
 
 
7a283eb
2135c8f
 
 
 
 
 
7a283eb
 
2135c8f
 
 
 
7a283eb
 
2135c8f
 
 
 
 
7a283eb
2135c8f
 
7a283eb
2135c8f
 
7a283eb
 
a33a4a5
7a283eb
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>複合認識システム</title>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.3.1/dist/tf.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@0.8/dist/teachablemachine-image.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/pose@0.8/dist/teachablemachine-pose.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/face_mesh.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }
        .camera-container {
            position: relative;
            margin-bottom: 20px;
            border: 2px solid #333;
            border-radius: 8px;
            overflow: hidden;
        }
        .camera-info {
            display: flex;
            justify-content: space-between;
            background-color: #333;
            color: white;
            padding: 10px;
        }
        .detection-strength {
            display: flex;
            gap: 20px;
        }
        .strength-bar {
            height: 20px;
            width: 100px;
            background-color: #ddd;
            border-radius: 4px;
            overflow: hidden;
        }
        .strength-fill {
            height: 100%;
            background-color: #4CAF50;
            width: 0%;
        }
        .tile-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 20px;
        }
        .tile {
            background-color: white;
            border-radius: 8px;
            padding: 15px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            width: calc(33% - 20px);
            position: relative;
        }
        .tile-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
        }
        .tile-title {
            font-weight: bold;
        }
        .delete-btn {
            background-color: #ff4444;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 2px 8px;
            cursor: pointer;
        }
        .chart-container {
            height: 150px;
            margin-bottom: 10px;
            display: flex;
            align-items: flex-end;
            gap: 5px;
        }
        .chart-bar {
            flex-grow: 1;
            background-color: #4CAF50;
            transition: height 0.3s;
        }
        .add-tile {
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #ddd;
            cursor: pointer;
            font-size: 24px;
        }
        .add-tile:hover {
            background-color: #ccc;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }
        .modal-content {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            width: 400px;
        }
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        .close-btn {
            font-size: 24px;
            cursor: pointer;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .submit-btn {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 4px;
            cursor: pointer;
        }
        .alarm {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(255,0,0,0.3);
            z-index: 999;
            display: none;
        }
        .eye-status {
            display: flex;
            gap: 10px;
            align-items: center;
        }
        .eye-status-label {
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>複合認識システム</h1>
        </div>
        
        <div class="camera-container">
            <video id="webcam" width="640" height="480" autoplay playsinline></video>
            <canvas id="output" width="640" height="480"></canvas>
            <div class="camera-info">
                <div class="eye-status">
                    <span class="eye-status-label">目の状態:</span>
                    <span id="eye-status-text">検出中...</span>
                </div>
                <div class="detection-strength">
                    <div>
                        <div>認識強度</div>
                        <div class="strength-bar">
                            <div class="strength-fill" id="strength-fill"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <h2>認識モデル設定</h2>
        <div class="tile-container" id="tile-container">
            <!-- タイルはここに動的に追加されます -->
        </div>
        
        <div class="tile add-tile" id="add-tile"></div>
    </div>
    
    <div class="alarm" id="alarm"></div>
    
    <div class="modal" id="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>新しいモデルを追加</h3>
                <span class="close-btn" id="close-modal">&times;</span>
            </div>
            <div class="form-group">
                <label for="model-type">モデルタイプ</label>
                <select id="model-type">
                    <option value="image">画像認識</option>
                    <option value="pose">ポーズ認識</option>
                </select>
            </div>
            <div class="form-group">
                <label for="model-id">モデルID (TeachableMachineのURLの最後の部分)</label>
                <input type="text" id="model-id" placeholder="例: E7pp4SoMG">
            </div>
            <div class="form-group">
                <label for="alarm-class">警報を鳴らすクラス名 (カンマ区切り)</label>
                <input type="text" id="alarm-class" placeholder="例: 危険,警告">
            </div>
            <button class="submit-btn" id="submit-model">追加</button>
        </div>
    </div>
    
    <audio id="alarm-sound" src="https://assets.mixkit.co/sfx/preview/mixkit-alarm-digital-clock-beep-989.mp3" preload="auto"></audio>
    
    <script>
        // グローバル変数
        let faceMesh = null;
        let eyeStatus = "検出しない";
        let eyeDetectionCount = 0;
        let eyeClosedCount = 0;
        let eyeOpenCount = 0;
        let alarmSound = document.getElementById("alarm-sound");
        let alarmElement = document.getElementById("alarm");
        let modelTiles = [];
        let activeModels = [];
        let detectionHistory = [];
        const HISTORY_SIZE = 50;
        const ALARM_THRESHOLD = 40;
        
        // 目の状態を更新する関数
        function updateEyeStatus(newStatus) {
            const eyeStatusText = document.getElementById("eye-status-text");
            
            if (newStatus !== eyeStatus) {
                eyeStatus = newStatus;
                eyeStatusText.textContent = eyeStatus;
                
                // 警報条件チェック
                checkEyeAlarmCondition();
            }
        }
        
        // 目の警報条件チェック
        function checkEyeAlarmCondition() {
            if (eyeStatus === "検出しない") {
                // 警報を止める
                stopAlarm();
                return;
            }
            
            // 50回の検出で40回以上条件を満たしたら警報を鳴らす
            if (eyeDetectionCount >= HISTORY_SIZE) {
                if (eyeStatus === "目をつぶっている場合" && eyeClosedCount >= ALARM_THRESHOLD) {
                    startAlarm();
                } else if (eyeStatus === "目を開けている場合" && eyeOpenCount >= ALARM_THRESHOLD) {
                    startAlarm();
                } else {
                    stopAlarm();
                }
            }
        }
        
        // 警報を開始
        function startAlarm() {
            alarmElement.style.display = "block";
            alarmSound.currentTime = 0;
            alarmSound.play().catch(e => console.log("Audio play failed:", e));
        }
        
        // 警報を停止
        function stopAlarm() {
            alarmElement.style.display = "none";
            alarmSound.pause();
        }
        
        // 目の検出を初期化
        async function initEyeDetection() {
            const videoElement = document.getElementById('webcam');
            const canvasElement = document.getElementById('output');
            const canvasCtx = canvasElement.getContext('2d');
            
            faceMesh = new FaceMesh({
                locateFile: (file) => {
                    return `https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/${file}`;
                }
            });
            
            faceMesh.setOptions({
                maxNumFaces: 1,
                refineLandmarks: true,
                minDetectionConfidence: 0.5,
                minTrackingConfidence: 0.5
            });
            
            faceMesh.onResults((results) => {
                canvasCtx.save();
                canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
                canvasCtx.drawImage(results.image, 0, 0, canvasElement.width, canvasElement.height);
                
                if (results.multiFaceLandmarks) {
                    for (const landmarks of results.multiFaceLandmarks) {
                        // 左目のEAR (Eye Aspect Ratio) を計算
                        const leftEAR = getEAR(
                            landmarks,
                            159, 145, 133, 33  // 左目のlandmarkインデックス
                        );
                        
                        // 右目のEARを計算
                        const rightEAR = getEAR(
                            landmarks,
                            386, 374, 362, 263  // 右目のlandmarkインデックス
                        );
                        
                        // 両目の平均EAR
                        const ear = (leftEAR + rightEAR) / 2;
                        
                        // EARに基づいて目の状態を判定
                        const EAR_THRESHOLD = 0.25;
                        const isBlinking = ear < EAR_THRESHOLD;
                        
                        // 検出強度を更新
                        updateDetectionStrength(ear);
                        
                        // 目の状態を更新
                        if (eyeStatus !== "検出しない") {
                            eyeDetectionCount = (eyeDetectionCount + 1) % HISTORY_SIZE;
                            
                            if (isBlinking) {
                                eyeClosedCount = Math.min(eyeClosedCount + 1, HISTORY_SIZE);
                                if (eyeOpenCount > 0) eyeOpenCount--;
                                
                                if (eyeStatus === "目をつぶっている場合") {
                                    updateEyeStatus("目をつぶっている場合");
                                }
                            } else {
                                eyeOpenCount = Math.min(eyeOpenCount + 1, HISTORY_SIZE);
                                if (eyeClosedCount > 0) eyeClosedCount--;
                                
                                if (eyeStatus === "目を開けている場合") {
                                    updateEyeStatus("目を開けている場合");
                                }
                            }
                        }
                    }
                }
                
                canvasCtx.restore();
            });
            
            const camera = new Camera(videoElement, {
                onFrame: async () => {
                    await faceMesh.send({image: videoElement});
                },
                width: 640,
                height: 480
            });
            
            camera.start();
        }
        
        // EAR (Eye Aspect Ratio) を計算する関数
        function getEAR(landmarks, topIdx, bottomIdx, leftIdx, rightIdx) {
            const vertical = Math.hypot(
                landmarks[topIdx].x - landmarks[bottomIdx].x,
                landmarks[topIdx].y - landmarks[bottomIdx].y
            );
            const horizontal = Math.hypot(
                landmarks[leftIdx].x - landmarks[rightIdx].x,
                landmarks[leftIdx].y - landmarks[rightIdx].y
            );
            return vertical / horizontal;
        }
        
        // 検出強度を更新
        function updateDetectionStrength(ear) {
            const strengthFill = document.getElementById("strength-fill");
            // EARが0.15-0.45の範囲で0-100%にマッピング
            const strength = Math.min(Math.max((ear - 0.15) / (0.45 - 0.15) * 100, 0), 100);
            strengthFill.style.width = `${strength}%`;
            
            // 色を変更 (緑→黄→赤)
            if (strength > 50) {
                strengthFill.style.backgroundColor = "#4CAF50"; // 緑
            } else if (strength > 20) {
                strengthFill.style.backgroundColor = "#FFC107"; // 黄
            } else {
                strengthFill.style.backgroundColor = "#F44336"; // 赤
            }
        }
        
        // モデルをロードしてタイルに追加
        async function addModelTile(modelType, modelId, alarmClasses) {
            const tileContainer = document.getElementById("tile-container");
            
            // タイル要素を作成
            const tile = document.createElement("div");
            tile.className = "tile";
            tile.dataset.modelType = modelType;
            tile.dataset.modelId = modelId;
            tile.dataset.alarmClasses = alarmClasses;
            
            // タイルヘッダー
            const tileHeader = document.createElement("div");
            tileHeader.className = "tile-header";
            
            const tileTitle = document.createElement("div");
            tileTitle.className = "tile-title";
            tileTitle.textContent = `${modelType === 'image' ? '画像認識' : 'ポーズ認識'}: ${modelId}`;
            
            const deleteBtn = document.createElement("button");
            deleteBtn.className = "delete-btn";
            deleteBtn.textContent = "×";
            deleteBtn.onclick = () => {
                tileContainer.removeChild(tile);
                modelTiles = modelTiles.filter(t => t !== tile);
                activeModels = activeModels.filter(m => m.tile !== tile);
            };
            
            tileHeader.appendChild(tileTitle);
            tileHeader.appendChild(deleteBtn);
            tile.appendChild(tileHeader);
            
            // チャートコンテナ
            const chartContainer = document.createElement("div");
            chartContainer.className = "chart-container";
            tile.appendChild(chartContainer);
            
            // モデル情報
            const modelInfo = document.createElement("div");
            modelInfo.textContent = `警報クラス: ${alarmClasses}`;
            tile.appendChild(modelInfo);
            
            // タイルを追加
            tileContainer.appendChild(tile);
            modelTiles.push(tile);
            
            // モデルをロード
            const modelUrl = `https://storage.googleapis.com/tm-model/${modelId}/`;
            
            try {
                let model;
                let maxPredictions;
                
                if (modelType === 'image') {
                    model = await tmImage.load(`${modelUrl}model.json`, `${modelUrl}metadata.json`);
                    maxPredictions = model.getTotalClasses();
                    
                    // チャート用のバーを作成
                    for (let i = 0; i < maxPredictions; i++) {
                        const bar = document.createElement("div");
                        bar.className = "chart-bar";
                        bar.dataset.classIndex = i;
                        chartContainer.appendChild(bar);
                    }
                    
                    // Webcamをセットアップ
                    const webcam = new tmImage.Webcam(200, 200, true);
                    await webcam.setup();
                    await webcam.play();
                    
                    // 予測ループ
                    const predictLoop = async () => {
                        const prediction = await model.predict(webcam.canvas);
                        
                        // 警報条件チェック
                        let shouldAlarm = false;
                        
                        // チャートを更新
                        for (let i = 0; i < maxPredictions; i++) {
                            const probability = prediction[i].probability;
                            const className = prediction[i].className;
                            
                            // バーの高さを更新
                            const bars = chartContainer.querySelectorAll(".chart-bar");
                            if (bars[i]) {
                                bars[i].style.height = `${probability * 100}%`;
                                
                                // 警報クラスの場合は色を赤に
                                if (alarmClasses.split(',').includes(className)) {
                                    bars[i].style.backgroundColor = probability > 0.5 ? "#F44336" : "#4CAF50";
                                    
                                    if (probability > 0.5) {
                                        shouldAlarm = true;
                                    }
                                } else {
                                    bars[i].style.backgroundColor = "#4CAF50";
                                }
                            }
                        }
                        
                        // 警報条件を履歴に追加
                        detectionHistory.push(shouldAlarm);
                        if (detectionHistory.length > HISTORY_SIZE) {
                            detectionHistory.shift();
                        }
                        
                        // 警報条件チェック
                        if (detectionHistory.length === HISTORY_SIZE) {
                            const alarmCount = detectionHistory.filter(Boolean).length;
                            if (alarmCount >= ALARM_THRESHOLD) {
                                startAlarm();
                            } else {
                                stopAlarm();
                            }
                        }
                        
                        requestAnimationFrame(predictLoop);
                    };
                    
                    predictLoop();
                    
                    activeModels.push({
                        tile,
                        model,
                        webcam,
                        predictLoop
                    });
                    
                } else if (modelType === 'pose') {
                    model = await tmPose.load(`${modelUrl}model.json`, `${modelUrl}metadata.json`);
                    maxPredictions = model.getTotalClasses();
                    
                    // チャート用のバーを作成
                    for (let i = 0; i < maxPredictions; i++) {
                        const bar = document.createElement("div");
                        bar.className = "chart-bar";
                        bar.dataset.classIndex = i;
                        chartContainer.appendChild(bar);
                    }
                    
                    // Webcamをセットアップ
                    const webcam = new tmPose.Webcam(200, 200, true);
                    await webcam.setup();
                    await webcam.play();
                    
                    // 予測ループ
                    const predictLoop = async () => {
                        const { pose, posenetOutput } = await model.estimatePose(webcam.canvas);
                        const prediction = await model.predict(posenetOutput);
                        
                        // 警報条件チェック
                        let shouldAlarm = false;
                        
                        // チャートを更新
                        for (let i = 0; i < maxPredictions; i++) {
                            const probability = prediction[i].probability;
                            const className = prediction[i].className;
                            
                            // バーの高さを更新
                            const bars = chartContainer.querySelectorAll(".chart-bar");
                            if (bars[i]) {
                                bars[i].style.height = `${probability * 100}%`;
                                
                                // 警報クラスの場合は色を赤に
                                if (alarmClasses.split(',').includes(className)) {
                                    bars[i].style.backgroundColor = probability > 0.5 ? "#F44336" : "#4CAF50";
                                    
                                    if (probability > 0.5) {
                                        shouldAlarm = true;
                                    }
                                } else {
                                    bars[i].style.backgroundColor = "#4CAF50";
                                }
                            }
                        }
                        
                        // 警報条件を履歴に追加
                        detectionHistory.push(shouldAlarm);
                        if (detectionHistory.length > HISTORY_SIZE) {
                            detectionHistory.shift();
                        }
                        
                        // 警報条件チェック
                        if (detectionHistory.length === HISTORY_SIZE) {
                            const alarmCount = detectionHistory.filter(Boolean).length;
                            if (alarmCount >= ALARM_THRESHOLD) {
                                startAlarm();
                            } else {
                                stopAlarm();
                            }
                        }
                        
                        requestAnimationFrame(predictLoop);
                    };
                    
                    predictLoop();
                    
                    activeModels.push({
                        tile,
                        model,
                        webcam,
                        predictLoop
                    });
                }
            } catch (error) {
                console.error("モデルのロードに失敗しました:", error);
                tileTitle.textContent += " (ロード失敗)";
            }
        }
        
        // モーダルを開く
        document.getElementById("add-tile").addEventListener("click", () => {
            document.getElementById("modal").style.display = "flex";
        });
        
        // モーダルを閉じる
        document.getElementById("close-modal").addEventListener("click", () => {
            document.getElementById("modal").style.display = "none";
        });
        
        // モデルを追加
        document.getElementById("submit-model").addEventListener("click", () => {
            const modelType = document.getElementById("model-type").value;
            const modelId = document.getElementById("model-id").value.trim();
            const alarmClasses = document.getElementById("alarm-class").value.trim();
            
            if (modelId && alarmClasses) {
                addModelTile(modelType, modelId, alarmClasses);
                document.getElementById("modal").style.display = "none";
                
                // フォームをリセット
                document.getElementById("model-id").value = "";
                document.getElementById("alarm-class").value = "";
            } else {
                alert("モデルIDと警報クラスを入力してください");
            }
        });
        
        // 目の状態選択 (デモ用)
        function setupEyeStatusSelector() {
            const eyeStatusText = document.getElementById("eye-status-text");
            
            eyeStatusText.addEventListener("click", () => {
                const currentStatus = eyeStatusText.textContent;
                let newStatus;
                
                if (currentStatus === "検出しない") {
                    newStatus = "目をつぶっている場合";
                } else if (currentStatus === "目をつぶっている場合") {
                    newStatus = "目を開けている場合";
                } else {
                    newStatus = "検出しない";
                }
                
                updateEyeStatus(newStatus);
                eyeDetectionCount = 0;
                eyeClosedCount = 0;
                eyeOpenCount = 0;
            });
        }
        
        // 初期化
        async function init() {
            await initEyeDetection();
            setupEyeStatusSelector();
            
            // デフォルトで目の状態を「検出しない」に設定
            updateEyeStatus("検出しない");
        }
        
        // ページ読み込み時に初期化
        window.onload = init;
    </script>
</body>
</html>