forked from sudiptasarkar011/FusionVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.html
812 lines (697 loc) · 38.4 KB
/
camera.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scanner - Fusion Vision</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<!-- TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<!-- COCO-SSD Model -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
</head>
<body>
<header class="header" style="background: rgba(255, 255, 255, 0.5);">
<div class="logo-section">
<img src="Logo.png" alt="Fusion Vision Logo" class="logo zoomable">
<h1 class="brand-title">Fusion Vision</h1>
</div>
<nav class="nav-links">
<a href="index.html" class="nav-link">Home</a>
<a href="about.html" class="nav-link">About</a>
<a href="features.html" class="nav-link">Features</a>
<a href="contact.html" class="nav-link">Contact</a>
</nav>
<div class="auth-buttons">
<button class="btn-secondary" id="backButton">Back</button>
</div>
</header>
<main class="scanner-content">
<div class="scanner-container">
<div class="scanner-header">
<h2 id="welcomeMessage">Object Scanner</h2>
<p class="scanner-subtitle">Position objects in the camera view for detection</p>
</div>
<div class="camera-section">
<!-- Camera Feed -->
<div class="camera-feed-container">
<video id="cameraFeed" autoplay playsinline></video>
<canvas id="detectionCanvas" class="detection-overlay"></canvas>
</div>
<!-- Controls -->
<div class="scanner-controls">
<button class="btn-primary" id="startScan" style="transition: background-color 0.3s ease;"
onmouseover="this.style.backgroundColor='blue'; this.style.color='white';"
onmouseout="this.style.backgroundColor=''; this.style.color='';">
<svg class="icon" viewBox="0 0 24 24" style="transition: background-color 0.3s ease;"
onmouseover="this.style.backgroundColor='blue'; this.style.color='white';"
onmouseout="this.style.backgroundColor=''; this.style.color='';">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 14.5v-9l6 4.5-6 4.5z"/>
</svg>
Start Scanning
</button>
<button class="btn-secondary" id="captureBtn" style="transition: background-color 0.3s ease;"
onmouseover="this.style.backgroundColor='blue'; this.style.color='white';"
onmouseout="this.style.backgroundColor=''; this.style.color='';">
<svg class="icon" viewBox="0 0 24 24">
<path d="M12 8c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4-1.79-4-4-4zm-7 7H3v4c0 1.1.9 2 2 2h4v-2H5v-4zM5 5h4V3H5c-1.1 0-2 .9-2 2v4h2V5zm14-2h-4v2h4v4h2V5c0-1.1-.9-2-2-2zm0 16h-4v2h4c1.1 0 2-.9 2-2v-4h-2v4z"/>
</svg>
Capture
</button>
<button class="btn-secondary" id="stopScan" style="transition: background-color 0.3s ease;"
onmouseover="this.style.backgroundColor='blue'; this.style.color='white';"
onmouseout="this.style.backgroundColor=''; this.style.color='';">
<svg class="icon" viewBox="0 0 24 24">
<path d="M6 6h12v12H6z"/>
</svg>
Stop
</button>
<button class="btn-secondary" id="muteBtn" style="transition: background-color 0.3s ease;"
onmouseover="this.style.backgroundColor='blue'; this.style.color='white';"
onmouseout="this.style.backgroundColor=''; this.style.color='';">
<svg class="icon" viewBox="0 0 24 24">
<path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"/>
</svg>
Toggle Audio
</button>
</div>
</div>
<!-- Results Section -->
<div class="results-section">
<h3>Detection Results</h3>
<div class="results-container" id="detectionResults">
<!-- Results will be populated here -->
</div>
<!-- Captured Images Gallery -->
<div class="captured-gallery" id="capturedGallery">
<h3>Captured Images</h3>
<div class="gallery-grid" id="galleryGrid">
<!-- Captured images will be displayed here -->
</div>
</div>
</div>
</div>
</main>
<footer>
<div class="footer-content" style="display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; padding: 20px; background: rgba(255, 255, 255, 0); color: black;">
<div class="footer-section about" style="flex: 2; min-width: 300px; display: flex; align-items:flex-start; margin: 10px; transition: transform 0.3s ease-in-out;" onmouseover="this.style.transform='scale(1.02)'" onmouseout="this.style.transform='scale(1)'">
<img src="Logo.png" alt="Fusion Vision Logo" style="width: 100px; margin-right: 20px;">
<p style="margin: 0;">Fusion Vision is an advanced object detection platform that leverages cutting-edge AI technology to provide real-time object recognition and analysis.</p>
</div>
<div class="footer-section services" style="flex: 1; min-width: 150px; margin: 10px;">
<h2 style="font-size: 18px; margin-bottom: 10px; transition: color 0.3s ease;" onmouseover="this.style.color='#e67e22'" onmouseout="this.style.color='black'">Fusion Vision</h2>
<ul style="list-style: none; padding: 0;">
<li><a href="#" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Who We Are</a></li>
<li><a href="#" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Blog</a></li>
<li><a href="#" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Work With Us</a></li>
<li><a href="#" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Investor Relations</a></li>
<li><a href="contact.html" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Contact Us</a></li>
</ul>
</div>
<div class="footer-section learn-more" style="flex: 1; min-width: 150px; margin: 10px;">
<h2 style="font-size: 18px; margin-bottom: 10px; transition: color 0.3s ease;" onmouseover="this.style.color='#e67e22'" onmouseout="this.style.color='black'">Learn More</h2>
<ul style="list-style: none; padding: 0;">
<li><a href="privacy.html" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Privacy Policy</a></li>
<li><a href="#" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Security</a></li>
<li><a href="#" style="color: black; text-decoration: none; transition: all 0.3s ease;" onmouseover="this.style.color='#e67e22'; this.style.paddingLeft='10px'" onmouseout="this.style.color='black'; this.style.paddingLeft='0'">Terms of Service</a></li>
</ul>
</div>
<div class="footer-section social-links" style="flex: 1; min-width: 150px; margin: 10px;">
<h2 style="font-size: 18px; margin-bottom: 10px; transition: color 0.3s ease;" onmouseover="this.style.color='#e67e22'" onmouseout="this.style.color='black'">Follow Us</h2>
<ul class="social-icons" style="list-style: none; padding: 0; display: flex; gap: 15px;">
<li><a href="https://www.instagram.com/" target="_blank" style="scale:1.3 ; color: black; text-decoration: none; font-size: 24px; transition: 0.3s; transform: scale(1); display: inline-block;" onmouseover="this.style.color='#e67e22'; this.style.transform='scale(1.2)'; this.style.display='inline-block'" onmouseout="this.style.color='black'; this.style.transform='scale(1)'">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-instagram" viewBox="0 0 16 16">
<path d="M8 0C5.829 0 5.556.01 4.703.048 3.85.088 3.269.222 2.76.42a3.9 3.9 0 0 0-1.417.923A3.9 3.9 0 0 0 .42 2.76C.222 3.268.087 3.85.048 4.7.01 5.555 0 5.827 0 8.001c0 2.172.01 2.444.048 3.297.04.852.174 1.433.372 1.942.205.526.478.972.923 1.417.444.445.89.719 1.416.923.51.198 1.09.333 1.942.372C5.555 15.99 5.827 16 8 16s2.444-.01 3.298-.048c.851-.04 1.434-.174 1.943-.372a3.9 3.9 0 0 0 1.416-.923c.445-.445.718-.891.923-1.417.197-.509.332-1.09.372-1.942C15.99 10.445 16 10.173 16 8s-.01-2.445-.048-3.299c-.04-.851-.175-1.433-.372-1.941a3.9 3.9 0 0 0-.923-1.417A3.9 3.9 0 0 0 13.24.42c-.51-.198-1.092-.333-1.943-.372C10.443.01 10.172 0 7.998 0zm-.717 1.442h.718c2.136 0 2.389.007 3.232.046.78.035 1.204.166 1.486.275.373.145.64.319.92.599s.453.546.598.92c.11.281.24.705.275 1.485.039.843.047 1.096.047 3.231s-.008 2.389-.047 3.232c-.035.78-.166 1.203-.275 1.485a2.5 2.5 0 0 1-.599.919c-.28.28-.546.453-.92.598-.28.11-.704.24-1.485.276-.843.038-1.096.047-3.232.047s-2.39-.009-3.233-.047c-.78-.036-1.203-.166-1.485-.276a2.5 2.5 0 0 1-.92-.598 2.5 2.5 0 0 1-.6-.92c-.109-.281-.24-.705-.275-1.485-.038-.843-.046-1.096-.046-3.233s.008-2.388.046-3.231c.036-.78.166-1.204.276-1.486.145-.373.319-.64.599-.92s.546-.453.92-.598c.282-.11.705-.24 1.485-.276.738-.034 1.024-.044 2.515-.045zm4.988 1.328a.96.96 0 1 0 0 1.92.96.96 0 0 0 0-1.92m-4.27 1.122a4.109 4.109 0 1 0 0 8.217 4.109 4.109 0 0 0 0-8.217m0 1.441a2.667 2.667 0 1 1 0 5.334 2.667 2.667 0 0 1 0-5.334"/>
</svg></a></li>
<li> <a href="https://www.facebook.com/?_rdr" target="_blank" style="scale:1.3 ; color: black; text-decoration: none; font-size: 24px; transition: 0.3s; transform: scale(1); display: inline-block;" onmouseover="this.style.color='#e67e22'; this.style.transform='scale(1.2)'; this.style.display='inline-block'" onmouseout="this.style.color='black'; this.style.transform='scale(1)'">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-facebook" viewBox="0 0 16 16">
<path d="M16 8.049c0-4.446-3.582-8.05-8-8.05C3.58 0-.002 3.603-.002 8.05c0 4.017 2.926 7.347 6.75 7.951v-5.625h-2.03V8.05H6.75V6.275c0-2.017 1.195-3.131 3.022-3.131.876 0 1.791.157 1.791.157v1.98h-1.009c-.993 0-1.303.621-1.303 1.258v1.51h2.218l-.354 2.326H9.25V16c3.824-.604 6.75-3.934 6.75-7.951"/>
</svg>
</a></li>
<li> <a href="https://in.linkedin.com/" target="_blank" style=" scale:1.35; color:black; text-decoration: none; font-size: 24px; transition: 0.3s; transform: scale(1); display: inline-block;" onmouseover="this.style.color='#e67e22'; this.style.transform='scale(1.2)'; this.style.display='inline-block'" onmouseout="this.style.color='black'; this.style.transform='scale(1)'">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-linkedin" viewBox="0 0 16 16">
<path d="M0 1.146C0 .513.526 0 1.175 0h13.65C15.474 0 16 .513 16 1.146v13.708c0 .633-.526 1.146-1.175 1.146H1.175C.526 16 0 15.487 0 14.854zm4.943 12.248V6.169H2.542v7.225zm-1.2-8.212c.837 0 1.358-.554 1.358-1.248-.015-.709-.52-1.248-1.342-1.248S2.4 3.226 2.4 3.934c0 .694.521 1.248 1.327 1.248zm4.908 8.212V9.359c0-.216.016-.432.08-.586.173-.431.568-.878 1.232-.878.869 0 1.216.662 1.216 1.634v3.865h2.401V9.25c0-2.22-1.184-3.252-2.764-3.252-1.274 0-1.845.7-2.165 1.193v.025h-.016l.016-.025V6.169h-2.4c.03.678 0 7.225 0 7.225z"></path>
</svg>
</a></li>
<li><a href="https://twitter.com/?lang=en" target="_blank" style=" scale:1.5; color: black; text-decoration: none; font-size: 24px; transition: 0.3s; transform: scale(1); display: inline-block;" onmouseover="this.style.color='#e67e22'; this.style.transform='scale(1.2)'; this.style.display='inline-block'" onmouseout="this.style.color='black'; this.style.transform='scale(1)'">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-twitter-x" viewBox="0 0 16 16">
<path d="M12.6.75h2.454l-5.36 6.142L16 15.25h-4.937l-3.867-5.07-4.425 5.07H.316l5.733-6.57L0 .75h5.063l3.495 4.633L12.601.75Zm-.86 13.028h1.36L4.323 2.145H2.865z"/>
</svg></a></li>
<li> <a href="https://www.youtube.com/" target="_blank" style="scale:1.5 ; color: black; text-decoration: none; font-size: 24px; transition: 0.3s; transform: scale(1); display: inline-block;" onmouseover="this.style.color='#e67e22'; this.style.transform='scale(1.2)'; this.style.display='inline-block'" onmouseout="this.style.color='black'; this.style.transform='scale(1)'">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-youtube" viewBox="0 0 16 16">
<path d="M8.051 1.999h.089c.822.003 4.987.033 6.11.335a2.01 2.01 0 0 1 1.415 1.42c.101.38.172.883.22 1.402l.01.104.022.26.008.104c.065.914.073 1.77.074 1.957v.075c-.001.194-.01 1.108-.082 2.06l-.008.105-.009.104c-.05.572-.124 1.14-.235 1.558a2.01 2.01 0 0 1-1.415 1.42c-1.16.312-5.569.334-6.18.335h-.142c-.309 0-1.587-.006-2.927-.052l-.17-.006-.087-.004-.171-.007-.171-.007c-1.11-.049-2.167-.128-2.654-.26a2.01 2.01 0 0 1-1.415-1.419c-.111-.417-.185-.986-.235-1.558L.09 9.82l-.008-.104A31 31 0 0 1 0 7.68v-.123c.002-.215.01-.958.064-1.778l.007-.103.003-.052.008-.104.022-.26.01-.104c.048-.519.119-1.023.22-1.402a2.01 2.01 0 0 1 1.415-1.42c.487-.13 1.544-.21 2.654-.26l.17-.007.172-.006.086-.003.171-.007A100 100 0 0 1 7.858 2zM6.4 5.209v4.818l4.157-2.408z"></path>
</svg>
</a></li>
</ul>
</div>
</div>
<div class="footer-bottom" style="padding: 10px; text-align: center; background-color: white;">
<p style="margin: 0; color: black; transition: color 0.3s ease;" onmouseover="this.style.color='#e67e22'" onmouseout="this.style.color='black'">© 2024 Fusion Vision. All rights reserved.</p>
</div>
</footer>
<script>
let stream;
let model;
let isScanning = false;
let isMuted = false;
let narrationCount = 0;
// Initialize camera and model
async function init() {
try {
// Load COCO-SSD model
model = await cocoSsd.load({ base: 'mobilenet_v2' });
// Setup camera
stream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'environment',
width: { ideal: 1280 },
height: { ideal: 720 }
}
});
const video = document.getElementById('cameraFeed');
video.srcObject = stream;
// Setup detection canvas
const canvas = document.getElementById('detectionCanvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Welcome message
const username = localStorage.getItem('currentUser');
if (username) {
document.getElementById('welcomeMessage').textContent = `Welcome, ${username}!`;
}
} catch (error) {
console.error('Initialization error:', error);
alert('Error initializing camera or AI model. Please check permissions.');
}
}
// Add these helper functions for position detection
function getRelativePosition(x, videoWidth) {
const third = videoWidth / 3;
if (x < third) return "on the left";
if (x > third * 2) return "on the right";
return "in the center";
}
// Add these detailed detection functions
class DetailedDetector {
constructor() {
this.personAttributes = {
wearables: ['glasses', 'hat', 'mask', 'headphones'],
clothing: ['shirt', 't-shirt', 'jacket', 'dress'],
colors: ['red', 'blue', 'white', 'black', 'green'],
actions: ['standing', 'sitting', 'walking', 'holding']
};
this.objectAttributes = {
toys: ['soft toy', 'teddy bear', 'doll', 'plush'],
colors: ['red', 'blue', 'white', 'black', 'brown', 'pink'],
size: ['small', 'medium', 'large'],
texture: ['soft', 'hard', 'fluffy', 'smooth']
};
}
async analyzeImage(prediction, imageData) {
const [x, y, width, height] = prediction.bbox;
const attributes = [];
if (prediction.class === 'person') {
// Analyze face region for glasses
const faceRegion = this.extractFaceRegion(imageData, x, y, width, height);
const faceAttributes = await this.detectFaceAttributes(faceRegion);
attributes.push(...faceAttributes);
// Analyze clothing
const clothingRegion = this.extractClothingRegion(imageData, x, y, width, height);
const clothingAttributes = this.detectClothingAttributes(clothingRegion);
attributes.push(...clothingAttributes);
} else if (this.isTypeOf(prediction.class, 'toy')) {
const toyAttributes = this.detectToyAttributes(imageData, x, y, width, height);
attributes.push(...toyAttributes);
}
return attributes;
}
isTypeOf(className, category) {
const toyKeywords = ['teddy', 'toy', 'doll', 'plush', 'stuffed'];
return toyKeywords.some(keyword => className.toLowerCase().includes(keyword));
}
}
// Enhanced detection configuration
const DETECTION_CONFIG = {
scoreThreshold: 0.85, // Increased confidence threshold
iouThreshold: 0.6, // Better overlap handling
maxDetections: 5, // Limit detections
modelSize: 'large' // Use larger model for better accuracy
};
// Detailed attribute detection class
class EnhancedDetector {
constructor() {
this.narrationCount = 0;
this.lastDetections = null;
this.stabilityCounter = 0;
this.requiredStability = 10; // Frames of stable detection before narrating
}
async detectWithConfidence(video) {
// Load enhanced model if not already loaded
if (!this.model) {
this.model = await cocoSsd.load({
base: 'mobilenet_v2',
modelUrl: 'https://tfhub.dev/tensorflow/tfjs-model/ssd_mobilenet_v2/1/default/1'
});
}
const predictions = await this.model.detect(video, {
score: DETECTION_CONFIG.scoreThreshold,
iouThreshold: DETECTION_CONFIG.iouThreshold,
topk: DETECTION_CONFIG.maxDetections
});
return this.stabilizeDetections(predictions);
}
stabilizeDetections(predictions) {
if (!this.lastDetections) {
this.lastDetections = predictions;
return null;
}
// Compare with last detections for stability
const isStable = this.compareDetections(predictions, this.lastDetections);
if (isStable) {
this.stabilityCounter++;
if (this.stabilityCounter >= this.requiredStability) {
return predictions;
}
} else {
this.stabilityCounter = 0;
this.lastDetections = predictions;
}
return null;
}
async analyzeScene(predictions, video) {
let detailedResults = [];
for (const pred of predictions) {
const details = await this.getDetailedAttributes(pred, video);
const position = this.getAccuratePosition(pred.bbox[0], video.videoWidth);
detailedResults.push({ ...details, position });
}
return this.createNaturalDescription(detailedResults);
}
async getDetailedAttributes(prediction, video) {
const attributes = {
person: {
wearables: await this.detectWearables(prediction, video),
clothing: await this.detectClothing(prediction, video),
posture: this.detectPosture(prediction)
},
object: {
type: this.classifyObjectType(prediction.class),
attributes: await this.detectObjectAttributes(prediction, video)
}
};
return this.formatAttributes(prediction.class, attributes);
}
narrate(description) {
if (this.narrationCount < 2) {
this.speakWithEmphasis(description);
this.narrationCount++;
return true;
}
return false;
}
speakWithEmphasis(text) {
const utterance = new SpeechSynthesisUtterance(text);
// Get available voices and select a female voice
const voices = window.speechSynthesis.getVoices();
const femaleVoice = voices.find(voice =>
voice.name.includes('female') ||
voice.name.includes('Female') ||
voice.name.includes('Samantha') ||
voice.name.includes('Google UK English Female')
);
// Configure the voice
utterance.voice = femaleVoice;
utterance.lang = 'en-IN';
utterance.rate = 0.9;
utterance.pitch = 1.2; // Slightly higher pitch for female voice
// Add pauses and emphasis
const processedText = text
.replace(' and ', ', and ')
.replace('wearing', 'wearing,')
.replace('left', 'left side')
.replace('right', 'right side');
utterance.text = processedText;
// Ensure voices are loaded before speaking
if (window.speechSynthesis.getVoices().length === 0) {
window.speechSynthesis.addEventListener('voiceschanged', () => {
window.speechSynthesis.speak(utterance);
});
} else {
window.speechSynthesis.speak(utterance);
}
// Show visual feedback
this.updateVisualFeedback(text, this.narrationCount);
}
compareDetections(current, previous) {
if (current.length !== previous.length) return false;
return current.every((pred, i) => {
const prevPred = previous[i];
const sameClass = pred.class === prevPred.class;
const similarPosition = Math.abs(pred.bbox[0] - prevPred.bbox[0]) < 20;
return sameClass && similarPosition;
});
}
getAccuratePosition(x, videoWidth) {
const third = videoWidth / 3;
if (x < third) return "on the left";
if (x > third * 2) return "on the right";
return "in the center";
}
createNaturalDescription(results) {
if (results.length === 0) return "";
const descriptions = results.map(result => {
const { class: objClass, attributes, position } = result;
if (objClass === 'person') {
return `a person ${attributes.join(' ')} ${position}`;
} else {
return `a ${attributes.join(' ')} ${objClass} ${position}`;
}
});
return descriptions.join(' and ');
}
updateVisualFeedback(text, count) {
const resultsContainer = document.getElementById('detectionResults');
const resultElement = document.createElement('div');
resultElement.className = 'detection-result';
resultElement.setAttribute('data-narration', `Narration ${count + 1} of 2`);
resultElement.textContent = text;
resultsContainer.insertBefore(resultElement, resultsContainer.firstChild);
}
}
// Add this function after your existing scripts
function drawEnhancedVisuals(ctx, detections, description) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
detections.forEach(detection => {
const [x, y, width, height] = detection.bbox;
// Draw detection box
ctx.strokeStyle = '#003366';
ctx.lineWidth = 3;
ctx.strokeRect(x, y, width, height);
// Draw label background
const position = getRelativePosition(ctx.canvas.width - (x + width / 2), ctx.canvas.width);
const label = `${detection.class} (${position})`;
ctx.font = 'bold 16px Inter';
const textWidth = ctx.measureText(label).width;
ctx.fillStyle = 'rgba(0, 40, 87, 0.9)';
ctx.fillRect(x, y - 30, textWidth + 20, 30);
// Draw label text
ctx.fillStyle = 'white';
ctx.fillText(label, x + 10, y - 8);
});
}
// Replace the startEnhancedDetection function with this simpler version
async function startEnhancedDetection() {
if (!isScanning) return;
const video = document.getElementById('cameraFeed');
const canvas = document.getElementById('detectionCanvas');
const ctx = canvas.getContext('2d');
// Make sure canvas dimensions match video
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
// Detect objects
const predictions = await model.detect(video);
if (predictions.length > 0 && narrationCount < 2 && !isMuted) {
// Create description of all objects with positions
let description = "I can see ";
const objects = predictions.map(pred => {
const position = getRelativePosition(video.videoWidth - (pred.bbox[0] + pred.bbox[2] / 2), video.videoWidth);
return `${pred.class} ${position}`;
});
if (objects.length === 1) {
description += objects[0];
} else if (objects.length === 2) {
description += `${objects[0]} and ${objects[1]}`;
} else {
const lastObject = objects.pop();
description += `${objects.join(', ')}, and ${lastObject}`;
}
// Speak the description
const utterance = new SpeechSynthesisUtterance(description);
utterance.lang = 'en-IN';
utterance.rate = 0.9;
utterance.pitch = 1.1;
window.speechSynthesis.speak(utterance);
// Update visual results
const resultsContainer = document.getElementById('detectionResults');
const resultElement = document.createElement('div');
resultElement.className = 'detection-result';
resultElement.setAttribute('data-narration', `Narration ${narrationCount + 1} of 2`);
resultElement.textContent = description;
resultsContainer.insertBefore(resultElement, resultsContainer.firstChild);
narrationCount++;
}
// Draw boxes and labels
drawEnhancedVisuals(ctx, predictions);
// Continue detection if still scanning
if (isScanning) {
requestAnimationFrame(startEnhancedDetection);
}
}
// Update the start scan click handler
document.getElementById('startScan').addEventListener('click', () => {
isScanning = true;
narrationCount = 0;
startEnhancedDetection();
});
// Update the stop scan click handler
document.getElementById('stopScan').addEventListener('click', () => {
isScanning = false;
narrationCount = 0;
const canvas = document.getElementById('detectionCanvas');
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
// Capture image
function captureImage() {
const video = document.getElementById('cameraFeed');
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
ctx.drawImage(video, 0, 0);
// Save to gallery
const galleryGrid = document.getElementById('galleryGrid');
const img = document.createElement('img');
img.src = canvas.toDataURL('image/jpeg');
img.className = 'gallery-image';
galleryGrid.appendChild(img);
}
// Event Listeners
document.getElementById('stopScan').addEventListener('click', () => {
isScanning = false;
narrationCount = 0; // Reset narration count
});
document.getElementById('captureBtn').addEventListener('click', captureImage);
document.getElementById('backButton').addEventListener('click', () => {
window.location.href = 'index.html';
});
document.getElementById('muteBtn').addEventListener('click', () => {
isMuted = !isMuted;
if (isMuted) {
window.speechSynthesis.cancel(); // Stop current speech
document.getElementById('muteBtn').classList.add('muted');
} else {
document.getElementById('muteBtn').classList.remove('muted');
}
});
// Initialize on load
window.addEventListener('load', async () => {
try {
await init();
console.log('Initialization complete');
} catch (error) {
console.error('Failed to initialize:', error);
alert('Failed to initialize camera or AI model. Please check permissions and reload the page.');
}
});
</script>
<style>
.scanner-content {
padding-top: 80px;
max-width: 1200px;
margin: 0 auto;
padding: 80px 20px 20px;
}
.scanner-container {
background: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 2rem;
}
.scanner-header {
text-align: center;
margin-bottom: 2rem;
}
.scanner-subtitle {
color: var(--gray);
margin-top: 0.5rem;
}
.camera-section {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}
.camera-feed-container {
position: relative;
width: 100%;
max-width: 800px;
border-radius: 8px;
overflow: hidden;
}
#cameraFeed {
width: 100%;
height: auto;
display: block;
}
.detection-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.scanner-controls {
display: flex;
gap: 1rem;
margin: 1rem 0;
}
.icon {
width: 24px;
height: 24px;
fill: currentColor;
margin-right: 8px;
}
.results-section {
margin-top: 2rem;
padding-top: 2rem;
border-top: 1px solid var(--gray-light);
}
.detection-result {
position: relative;
padding: 1rem;
margin: 0.5rem 0;
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
border-left: 4px solid var(--primary-color);
}
.detection-result[data-narration="1"],
.detection-result[data-narration="2"] {
animation: highlight 2s ease-in-out;
}
@keyframes highlight {
0% { background: rgba(0,112,243,0.1); }
50% { background: rgba(0,112,243,0.2); }
100% { background: white; }
}
.confidence-indicator {
position: absolute;
right: 1rem;
top: 50%;
transform: translateY(-50%);
display: flex;
align-items: center;
gap: 0.5rem;
}
.confidence-bar {
width: 50px;
height: 4px;
background: var(--gray-light);
border-radius: 2px;
overflow: hidden;
}
.confidence-fill {
height: 100%;
background: var(--primary-color);
transition: width 0.3s ease;
}
.result-label {
font-weight: 500;
}
.result-confidence {
color: var(--gray);
}
.gallery-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.gallery-image {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 6px;
cursor: pointer;
transition: transform 0.2s;
}
.gallery-image:hover {
transform: scale(1.05);
}
@media (max-width: 768px) {
.scanner-controls {
flex-direction: column;
width: 100%;
}
.scanner-controls button {
width: 100%;
}
}
.btn-secondary.muted {
background-color: #ff4444;
color: white;
}
.btn-secondary.muted .icon {
fill: white;
}
/* Animation for speaking state */
@keyframes speaking {
0% { transform: scale(1); }
50% { transform: scale(1.05); }
100% { transform: scale(1); }
}
.detection-result:first-child {
animation: speaking 1s ease-in-out;
border-left: 4px solid var(--primary-color);
}
.detection-result[data-narration]::after {
content: attr(data-narration);
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
font-size: 0.8rem;
color: var(--primary-color);
background: rgba(0, 112, 243, 0.1);
padding: 2px 6px;
border-radius: 4px;
}
/* Animation for group detections */
@keyframes groupHighlight {
0% { background: rgba(0, 112, 243, 0.1); }
50% { background: rgba(0, 112, 243, 0.2); }
100% { background: rgba(0, 112, 243, 0.1); }
}
.detection-result[data-narration] {
animation: groupHighlight 2s infinite;
}
.position-label {
font-size: 0.9rem;
color: var(--primary-color);
margin-left: 0.5rem;
padding: 2px 6px;
background: rgba(0, 112, 243, 0.1);
border-radius: 4px;
}
.detection-group {
background: rgba(0, 112, 243, 0.1);
padding: 10px;
border-radius: 6px;
margin: 0.5rem 0;
}
</style>
</body>
</html>