-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemcam.html
More file actions
1396 lines (1186 loc) · 56.3 KB
/
temcam.html
File metadata and controls
1396 lines (1186 loc) · 56.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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Thermal Camera Simulator & Temperature Matrix Extractor</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
color: #333;
line-height: 1.6;
min-height: 100vh;
}
.container {
max-width: 1800px;
margin: 0 auto;
background: white;
min-height: 100vh;
box-shadow: 0 0 40px rgba(0,0,0,0.3);
}
.header {
background: linear-gradient(135deg, #2d1b69 0%, #11998e 100%);
color: white;
padding: 30px 20px;
text-align: center;
position: relative;
overflow: hidden;
}
.header::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 20"><defs><linearGradient id="thermal" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" style="stop-color:rgb(0,0,255);stop-opacity:0.3" /><stop offset="25%" style="stop-color:rgb(0,255,0);stop-opacity:0.3" /><stop offset="50%" style="stop-color:rgb(255,255,0);stop-opacity:0.3" /><stop offset="75%" style="stop-color:rgb(255,165,0);stop-opacity:0.3" /><stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:0.3" /></linearGradient></defs><rect width="100" height="20" fill="url(%23thermal)"/></svg>') repeat-x;
background-size: 200px 20px;
animation: thermalFlow 10s linear infinite;
}
@keyframes thermalFlow {
0% { transform: translateX(0); }
100% { transform: translateX(-200px); }
}
.header h1 {
font-size: 2.8em;
margin-bottom: 10px;
position: relative;
z-index: 1;
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
.header p {
font-size: 1.2em;
opacity: 0.9;
position: relative;
z-index: 1;
}
.main-content {
display: grid;
grid-template-columns: 400px 1fr;
gap: 30px;
padding: 30px;
min-height: calc(100vh - 140px);
}
.control-panel {
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
border-radius: 20px;
padding: 25px;
box-shadow: 0 15px 35px rgba(0,0,0,0.1);
height: fit-content;
position: sticky;
top: 30px;
}
.section {
margin-bottom: 25px;
padding: 20px;
background: white;
border-radius: 15px;
border-left: 5px solid #11998e;
box-shadow: 0 5px 15px rgba(0,0,0,0.08);
}
.section h3 {
color: #1e293b;
margin-bottom: 15px;
font-size: 1.3em;
display: flex;
align-items: center;
gap: 10px;
}
.input-group {
margin-bottom: 20px;
}
.input-group label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #374151;
font-size: 14px;
}
.input-group input, .input-group select {
width: 100%;
padding: 12px;
border: 2px solid #d1d5db;
border-radius: 10px;
font-size: 14px;
transition: all 0.3s;
}
.input-group input:focus, .input-group select:focus {
outline: none;
border-color: #11998e;
box-shadow: 0 0 0 3px rgba(17,153,142,0.1);
}
.input-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.btn {
background: linear-gradient(135deg, #11998e 0%, #2d1b69 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 10px;
cursor: pointer;
font-weight: 600;
transition: all 0.3s;
margin: 5px;
font-size: 14px;
display: inline-flex;
align-items: center;
gap: 8px;
width: 100%;
justify-content: center;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(17,153,142,0.4);
}
.btn-success {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
}
.btn-danger {
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
}
.btn-warning {
background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.camera-panel {
background: white;
border-radius: 20px;
padding: 25px;
box-shadow: 0 15px 35px rgba(0,0,0,0.1);
}
.camera-container {
position: relative;
background: #000;
border-radius: 15px;
overflow: hidden;
margin-bottom: 25px;
aspect-ratio: 4/3;
}
#videoElement {
width: 100%;
height: 100%;
object-fit: cover;
}
#thermalCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0.8;
mix-blend-mode: multiply;
}
.overlay-controls {
position: absolute;
top: 15px;
right: 15px;
display: flex;
gap: 10px;
z-index: 10;
}
.overlay-btn {
background: rgba(0,0,0,0.7);
color: white;
border: none;
padding: 8px 12px;
border-radius: 8px;
cursor: pointer;
font-size: 12px;
transition: all 0.3s;
}
.overlay-btn:hover {
background: rgba(0,0,0,0.9);
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin: 20px 0;
}
.stat-card {
background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
padding: 15px;
border-radius: 10px;
border-left: 4px solid #11998e;
text-align: center;
}
.stat-value {
font-size: 1.4em;
font-weight: 700;
color: #1e293b;
}
.stat-label {
font-size: 0.9em;
color: #374151;
margin-top: 5px;
}
.temperature-scale {
background: linear-gradient(to right, #0000ff, #00ffff, #00ff00, #ffff00, #ff8000, #ff0000);
height: 30px;
border-radius: 15px;
margin: 20px 0;
position: relative;
border: 2px solid #e5e7eb;
}
.scale-labels {
display: flex;
justify-content: space-between;
font-size: 12px;
color: #374151;
margin-top: 5px;
}
.matrix-display {
background: #f8fafc;
padding: 20px;
border-radius: 15px;
margin: 20px 0;
border: 2px solid #e5e7eb;
max-height: 400px;
overflow-y: auto;
}
.matrix-title {
font-weight: 600;
color: #1e293b;
margin-bottom: 15px;
text-align: center;
}
.matrix-table {
font-family: 'Courier New', monospace;
font-size: 10px;
width: 100%;
border-collapse: collapse;
}
.matrix-table td {
padding: 2px 4px;
text-align: center;
border: 1px solid #e5e7eb;
min-width: 35px;
}
.recording-indicator {
position: absolute;
top: 15px;
left: 15px;
background: #ef4444;
color: white;
padding: 8px 12px;
border-radius: 20px;
font-size: 12px;
font-weight: 600;
display: none;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.data-log {
background: #f1f5f9;
padding: 15px;
border-radius: 10px;
margin: 15px 0;
max-height: 200px;
overflow-y: auto;
font-family: 'Courier New', monospace;
font-size: 12px;
border: 2px solid #cbd5e1;
}
.log-entry {
margin-bottom: 5px;
padding: 5px;
background: white;
border-radius: 5px;
border-left: 3px solid #11998e;
}
.thermal-colormap {
display: grid;
grid-template-columns: repeat(10, 1fr);
gap: 2px;
margin: 15px 0;
}
.color-cell {
aspect-ratio: 1;
border-radius: 3px;
border: 1px solid #e5e7eb;
}
.info-panel {
background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
padding: 15px;
border-radius: 10px;
margin: 15px 0;
border-left: 4px solid #11998e;
}
.warning-panel {
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
padding: 15px;
border-radius: 10px;
margin: 15px 0;
border-left: 4px solid #f59e0b;
}
@media (max-width: 1400px) {
.main-content {
grid-template-columns: 1fr;
gap: 20px;
}
.control-panel {
position: static;
}
}
@media (max-width: 768px) {
.stats-grid {
grid-template-columns: 1fr 1fr;
}
.input-row {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🌡️ Thermal Camera Simulator</h1>
<p>Convert webcam feed to thermal imaging with temperature matrix extraction by Claudio Iturra</p>
</div>
<div class="main-content">
<div class="control-panel">
<!-- Camera Controls -->
<div class="section">
<h3>📹 Camera Controls</h3>
<button class="btn btn-success" onclick="startCamera()" id="startBtn">🎥 Start Camera</button>
<button class="btn btn-danger" onclick="stopCamera()" id="stopBtn" disabled>⏹️ Stop Camera</button>
<div class="input-group">
<label for="cameraSelect">Camera Source:</label>
<select id="cameraSelect">
<option value="">Default Camera</option>
</select>
</div>
<div class="input-row">
<div class="input-group">
<label for="resolution">Resolution:</label>
<select id="resolution">
<option value="640x480">640×480</option>
<option value="1280x720" selected>1280×720</option>
<option value="1920x1080">1920×1080</option>
</select>
</div>
<div class="input-group">
<label for="matrixSize">Matrix Size:</label>
<select id="matrixSize">
<option value="16x12">16×12</option>
<option value="32x24" selected>32×24</option>
<option value="64x48">64×48</option>
<option value="80x60">80×60</option>
</select>
</div>
</div>
</div>
<!-- Thermal Settings -->
<div class="section">
<h3>🌡️ Thermal Settings</h3>
<div class="input-row">
<div class="input-group">
<label for="minTemp">Min Temp (°C):</label>
<input type="number" id="minTemp" value="15" step="0.5">
</div>
<div class="input-group">
<label for="maxTemp">Max Temp (°C):</label>
<input type="number" id="maxTemp" value="35" step="0.5">
</div>
</div>
<div class="input-group">
<label for="thermalMode">Thermal Mode:</label>
<select id="thermalMode">
<option value="brightness">Brightness-based</option>
<option value="motion" selected>Motion-enhanced</option>
<option value="edge">Edge-detection</option>
<option value="combined">Combined Analysis</option>
</select>
</div>
<div class="input-group">
<label for="sensitivity">Sensitivity:</label>
<input type="range" id="sensitivity" min="0.1" max="2.0" step="0.1" value="1.0">
<span id="sensitivityValue">1.0</span>
</div>
<div class="input-group">
<label for="smoothing">Smoothing:</label>
<input type="range" id="smoothing" min="0" max="5" step="1" value="2">
<span id="smoothingValue">2</span>
</div>
</div>
<!-- Data Recording -->
<div class="section">
<h3>📊 Data Recording</h3>
<div class="input-group">
<label for="recordInterval">Record Interval:</label>
<select id="recordInterval">
<option value="10">10 seconds</option>
<option value="30">30 seconds</option>
<option value="60" selected>1 minute</option>
<option value="120">2 minutes</option>
<option value="300">5 minutes</option>
</select>
</div>
<button class="btn btn-warning" onclick="startRecording()" id="recordBtn" disabled>🔴 Start Recording</button>
<button class="btn" onclick="stopRecording()" id="stopRecordBtn" disabled>⏹️ Stop Recording</button>
<button class="btn btn-success" onclick="exportData()" id="exportBtn" disabled>💾 Export Data</button>
</div>
<!-- Display Options -->
<div class="section">
<h3>🎨 Display Options</h3>
<div class="input-group">
<label>
<input type="checkbox" id="showThermal" checked> Show Thermal Overlay
</label>
</div>
<div class="input-group">
<label>
<input type="checkbox" id="showGrid" checked> Show Temperature Grid
</label>
</div>
<div class="input-group">
<label>
<input type="checkbox" id="showMatrix"> Show Live Matrix
</label>
</div>
<div class="input-group">
<label for="colormap">Color Map:</label>
<select id="colormap">
<option value="thermal" selected>Thermal (Blue-Red)</option>
<option value="iron">Iron</option>
<option value="rainbow">Rainbow</option>
<option value="grayscale">Grayscale</option>
</select>
</div>
</div>
</div>
<div class="camera-panel">
<!-- Live Statistics -->
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value" id="avgTemp">--</div>
<div class="stat-label">Avg Temp (°C)</div>
</div>
<div class="stat-card">
<div class="stat-value" id="minTempStat">--</div>
<div class="stat-label">Min Temp (°C)</div>
</div>
<div class="stat-card">
<div class="stat-value" id="maxTempStat">--</div>
<div class="stat-label">Max Temp (°C)</div>
</div>
<div class="stat-card">
<div class="stat-value" id="hotspots">--</div>
<div class="stat-label">Hot Spots</div>
</div>
<div class="stat-card">
<div class="stat-value" id="recordCount">0</div>
<div class="stat-label">Records</div>
</div>
</div>
<!-- Camera Feed -->
<div class="camera-container">
<video id="videoElement" autoplay muted></video>
<canvas id="thermalCanvas"></canvas>
<div class="recording-indicator" id="recordingIndicator">● REC</div>
<div class="overlay-controls">
<button class="overlay-btn" onclick="captureFrame()">📸 Capture</button>
<button class="overlay-btn" onclick="toggleFullscreen()">⛶ Fullscreen</button>
</div>
</div>
<!-- Temperature Scale -->
<div class="temperature-scale"></div>
<div class="scale-labels">
<span id="scaleMin">15°C</span>
<span>Temperature Scale</span>
<span id="scaleMax">35°C</span>
</div>
<!-- Live Temperature Matrix -->
<div class="matrix-display" id="matrixDisplay" style="display: none;">
<div class="matrix-title">Live Temperature Matrix (°C)</div>
<table class="matrix-table" id="matrixTable"></table>
</div>
<!-- Data Log -->
<div class="data-log" id="dataLog">
<div class="log-entry">System ready. Start camera to begin thermal analysis.</div>
</div>
<!-- Information Panel -->
<div class="info-panel">
<strong>🔬 How it works:</strong><br>
This simulator converts visual camera data into thermal-like temperature matrices using advanced image analysis.
It analyzes brightness, motion, edge patterns, and color temperature to estimate relative temperatures, creating realistic thermal imaging data for research and development.
<br><br>
<strong>📋 Quick Start:</strong><br>
1. Click "Start Camera" and allow camera access<br>
2. Select your preferred thermal analysis mode<br>
3. Adjust temperature range and sensitivity<br>
4. Start recording to capture temperature matrices<br>
5. Export data as CSV or JSON for analysis
</div>
<div class="warning-panel">
<strong>⚠️ Camera Access Requirements:</strong><br>
• This page must be served over HTTPS or localhost<br>
• Allow camera permissions when prompted<br>
• Close other apps using your camera<br>
• Try different resolutions if camera fails to start<br><br>
<strong>Note:</strong> This is a simulation using regular camera data.
For actual thermal measurements, use dedicated thermal imaging hardware with infrared sensors.
</div>
</div>
</div>
</div>
<script>
let videoElement, thermalCanvas, ctx;
let stream = null;
let isRecording = false;
let recordingInterval = null;
let temperatureData = [];
let frameCount = 0;
let previousFrame = null;
// Thermal analysis parameters
let thermalSettings = {
minTemp: 15,
maxTemp: 35,
sensitivity: 1.0,
smoothing: 2,
mode: 'motion'
};
// Check camera permissions
async function checkCameraPermissions() {
try {
const result = await navigator.permissions.query({ name: 'camera' });
switch (result.state) {
case 'granted':
logMessage('✅ Camera permission granted');
return true;
case 'prompt':
logMessage('⚠️ Camera permission will be requested when you start');
return false;
case 'denied':
logMessage('❌ Camera permission denied. Please enable in browser settings.');
return false;
}
} catch (error) {
logMessage('⚠️ Cannot check camera permissions (older browser)');
return false;
}
}
// Initialize the application
window.onload = async function() {
videoElement = document.getElementById('videoElement');
thermalCanvas = document.getElementById('thermalCanvas');
ctx = thermalCanvas.getContext('2d');
// Check if we're on HTTPS or localhost (required for camera access)
if (location.protocol !== 'https:' && location.hostname !== 'localhost' && location.hostname !== '127.0.0.1') {
logMessage('⚠️ Camera access requires HTTPS or localhost');
alert('⚠️ For camera access, this page needs to be served over HTTPS or accessed via localhost');
}
// Set up event listeners
setupEventListeners();
// Check camera permissions
await checkCameraPermissions();
// Enumerate cameras
await enumerateCameras();
// Initialize thermal canvas
initializeThermalCanvas();
logMessage('🌡️ Thermal Camera Simulator ready!');
logMessage('📋 Instructions: 1) Click "Start Camera" 2) Allow camera access 3) Begin thermal analysis');
};
function setupEventListeners() {
// Sensitivity slider
document.getElementById('sensitivity').addEventListener('input', function() {
thermalSettings.sensitivity = parseFloat(this.value);
document.getElementById('sensitivityValue').textContent = this.value;
});
// Smoothing slider
document.getElementById('smoothing').addEventListener('input', function() {
thermalSettings.smoothing = parseInt(this.value);
document.getElementById('smoothingValue').textContent = this.value;
});
// Temperature range inputs
document.getElementById('minTemp').addEventListener('change', function() {
thermalSettings.minTemp = parseFloat(this.value);
document.getElementById('scaleMin').textContent = this.value + '°C';
});
document.getElementById('maxTemp').addEventListener('change', function() {
thermalSettings.maxTemp = parseFloat(this.value);
document.getElementById('scaleMax').textContent = this.value + '°C';
});
// Thermal mode
document.getElementById('thermalMode').addEventListener('change', function() {
thermalSettings.mode = this.value;
logMessage(`Thermal mode changed to: ${this.value}`);
});
// Display options
document.getElementById('showMatrix').addEventListener('change', function() {
document.getElementById('matrixDisplay').style.display = this.checked ? 'block' : 'none';
});
}
async function enumerateCameras() {
try {
// First request basic permission to get device labels
let permissionGranted = false;
try {
const tempStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false });
tempStream.getTracks().forEach(track => track.stop());
permissionGranted = true;
} catch (e) {
logMessage('⚠️ Camera permission needed to show device names');
}
const devices = await navigator.mediaDevices.enumerateDevices();
const videoDevices = devices.filter(device => device.kind === 'videoinput');
const select = document.getElementById('cameraSelect');
select.innerHTML = '<option value="">Default Camera</option>';
videoDevices.forEach((device, index) => {
const option = document.createElement('option');
option.value = device.deviceId;
if (device.label) {
option.textContent = device.label;
} else if (permissionGranted) {
option.textContent = `Camera ${index + 1}`;
} else {
option.textContent = `Camera ${index + 1} (grant permission to see name)`;
}
select.appendChild(option);
});
if (videoDevices.length === 0) {
logMessage('❌ No cameras found. Please connect a camera and refresh.');
select.innerHTML = '<option value="">No cameras available</option>';
} else {
logMessage(`📹 Found ${videoDevices.length} camera(s)`);
}
} catch (error) {
logMessage('❌ Error detecting cameras: ' + error.message);
const select = document.getElementById('cameraSelect');
select.innerHTML = '<option value="">Camera detection failed</option>';
}
}
function initializeThermalCanvas() {
const container = document.querySelector('.camera-container');
thermalCanvas.width = container.clientWidth;
thermalCanvas.height = container.clientHeight;
}
async function startCamera() {
try {
// First check if getUserMedia is supported
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error('Camera access not supported in this browser');
}
const deviceId = document.getElementById('cameraSelect').value;
const resolution = document.getElementById('resolution').value.split('x');
const constraints = {
video: {
width: { ideal: parseInt(resolution[0]) },
height: { ideal: parseInt(resolution[1]) },
frameRate: { ideal: 30 },
facingMode: 'environment' // Prefer back camera for better thermal analysis
},
audio: false // We don't need audio
};
if (deviceId) {
constraints.video.deviceId = { exact: deviceId };
delete constraints.video.facingMode; // Remove facingMode when specific device is selected
}
logMessage('Requesting camera access...');
// Request camera permission explicitly
stream = await navigator.mediaDevices.getUserMedia(constraints);
videoElement.srcObject = stream;
// Wait for video to load and start playing
await new Promise((resolve, reject) => {
videoElement.onloadedmetadata = () => {
videoElement.play().then(resolve).catch(reject);
};
videoElement.onerror = reject;
});
// Resize thermal canvas to match video
thermalCanvas.width = videoElement.videoWidth;
thermalCanvas.height = videoElement.videoHeight;
// Start thermal analysis
startThermalAnalysis();
// Update UI
document.getElementById('startBtn').disabled = true;
document.getElementById('stopBtn').disabled = false;
document.getElementById('recordBtn').disabled = false;
logMessage(`✅ Camera started successfully: ${videoElement.videoWidth}×${videoElement.videoHeight}`);
logMessage('🌡️ Thermal analysis active - analyzing visual patterns for temperature estimation');
} catch (error) {
let errorMessage = 'Camera access failed: ';
if (error.name === 'NotAllowedError') {
errorMessage += 'Permission denied. Please allow camera access and refresh the page.';
} else if (error.name === 'NotFoundError') {
errorMessage += 'No camera found. Please connect a camera and try again.';
} else if (error.name === 'NotReadableError') {
errorMessage += 'Camera is already in use by another application.';
} else if (error.name === 'OverconstrainedError') {
errorMessage += 'Camera does not support the requested resolution.';
} else {
errorMessage += error.message;
}
logMessage('❌ ' + errorMessage);
alert(errorMessage + '\n\nTroubleshooting:\n1. Allow camera permissions\n2. Close other apps using the camera\n3. Try a different resolution\n4. Refresh the page');
}
}
function stopCamera() {
if (stream) {
stream.getTracks().forEach(track => track.stop());
stream = null;
}
videoElement.srcObject = null;
// Stop recording if active
if (isRecording) {
stopRecording();
}
// Clear thermal canvas
ctx.clearRect(0, 0, thermalCanvas.width, thermalCanvas.height);
// Update UI
document.getElementById('startBtn').disabled = false;
document.getElementById('stopBtn').disabled = true;
document.getElementById('recordBtn').disabled = true;
// Reset stats
updateStats(null);
logMessage('Camera stopped');
}
function startThermalAnalysis() {
function analyze() {
if (videoElement.videoWidth > 0 && videoElement.videoHeight > 0) {
processFrame();
}
if (stream) {
requestAnimationFrame(analyze);
}
}
analyze();
}
function processFrame() {
const width = videoElement.videoWidth;
const height = videoElement.videoHeight;
// Create temporary canvas for image processing
const tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
const tempCtx = tempCanvas.getContext('2d');
// Draw current frame
tempCtx.drawImage(videoElement, 0, 0, width, height);
const currentFrame = tempCtx.getImageData(0, 0, width, height);
// Generate temperature matrix
const temperatureMatrix = generateTemperatureMatrix(currentFrame, width, height);
// Update thermal overlay if enabled
if (document.getElementById('showThermal').checked) {
drawThermalOverlay(temperatureMatrix, width, height);
}
// Update live matrix display if enabled
if (document.getElementById('showMatrix').checked) {
updateMatrixDisplay(temperatureMatrix);
}
// Update statistics
updateStats(temperatureMatrix);
// Store previous frame for motion analysis
previousFrame = currentFrame;
frameCount++;
}
function generateTemperatureMatrix(imageData, width, height) {
const matrixSize = document.getElementById('matrixSize').value.split('x');
const matrixWidth = parseInt(matrixSize[0]);
const matrixHeight = parseInt(matrixSize[1]);
const matrix = [];
const cellWidth = width / matrixWidth;
const cellHeight = height / matrixHeight;
for (let row = 0; row < matrixHeight; row++) {
const matrixRow = [];
for (let col = 0; col < matrixWidth; col++) {
const temperature = calculateCellTemperature(
imageData, width, height,
col * cellWidth, row * cellHeight,
cellWidth, cellHeight
);
matrixRow.push(temperature);
}
matrix.push(matrixRow);
}
return matrix;
}
function calculateCellTemperature(imageData, width, height, startX, startY, cellWidth, cellHeight) {
let totalBrightness = 0;
let motionIntensity = 0;
let edgeIntensity = 0;
let redIntensity = 0;
let infraredEstimate = 0;
let pixelCount = 0;
const endX = Math.min(startX + cellWidth, width);
const endY = Math.min(startY + cellHeight, height);
for (let y = Math.floor(startY); y < Math.floor(endY); y++) {
for (let x = Math.floor(startX); x < Math.floor(endX); x++) {
const index = (y * width + x) * 4;
if (index < imageData.data.length - 3) {
const r = imageData.data[index];
const g = imageData.data[index + 1];
const b = imageData.data[index + 2];
// Calculate brightness (luminance)
const brightness = 0.299 * r + 0.587 * g + 0.114 * b;
totalBrightness += brightness;
// Red channel intensity (correlates with heat in visible spectrum)
redIntensity += r;
// Estimate infrared-like signature from color temperature
// Warmer objects tend to emit more red/yellow light
const colorTemp = (r - b) / 255; // Red minus blue normalized
infraredEstimate += Math.max(0, colorTemp);
// Calculate motion if previous frame exists
if (previousFrame && thermalSettings.mode !== 'brightness') {
const prevR = previousFrame.data[index];
const prevG = previousFrame.data[index + 1];
const prevB = previousFrame.data[index + 2];
const prevBrightness = 0.299 * prevR + 0.587 * prevG + 0.114 * prevB;
motionIntensity += Math.abs(brightness - prevBrightness);
}
// Calculate edge intensity for edge detection mode
if (thermalSettings.mode === 'edge' || thermalSettings.mode === 'combined') {
if (x > 0 && y > 0) {
const leftIndex = (y * width + (x - 1)) * 4;
const topIndex = ((y - 1) * width + x) * 4;
if (leftIndex < imageData.data.length - 3 && topIndex < imageData.data.length - 3) {
const leftBrightness = 0.299 * imageData.data[leftIndex] + 0.587 * imageData.data[leftIndex + 1] + 0.114 * imageData.data[leftIndex + 2];
const topBrightness = 0.299 * imageData.data[topIndex] + 0.587 * imageData.data[topIndex + 1] + 0.114 * imageData.data[topIndex + 2];
edgeIntensity += Math.abs(brightness - leftBrightness) + Math.abs(brightness - topBrightness);
}
}
}
pixelCount++;
}