File size: 7,951 Bytes
322caed
9b02fee
322caed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbe95b8
322caed
 
 
 
 
23e33b3
322caed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
910cadf
 
 
322caed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c4ac32
 
322caed
 
5c4ac32
322caed
 
898404d
322caed
 
898404d
5c4ac32
322caed
 
 
 
 
 
 
 
 
 
 
 
 
 
5c4ac32
322caed
 
 
5c4ac32
 
 
 
 
b31c659
898404d
 
 
 
 
 
 
 
 
322caed
 
 
b31c659
322caed
910cadf
 
 
 
 
 
 
 
 
5c4ac32
910cadf
898404d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322caed
5c4ac32
898404d
322caed
 
b31c659
322caed
 
898404d
322caed
b31c659
910cadf
 
5c4ac32
910cadf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322caed
 
b31c659
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
<!DOCTYPE html>
<html>
<head>
    <title>AI Night Vision Camera</title>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
    <style>
        body {
            margin: 0;
            background: #000;
            color: #fff;
            font-family: monospace;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .video-container {
            position: relative;
            width: 640px;
            height: 480px;
            border: 2px solid #0f0;
            border-radius: 8px;
            overflow: hidden;
        }
        #video {
            width: 100%;
            height: 100%;
            object-fit: cover;
            
        }
        #canvas {
            position: absolute;
            top: 0;
            left: 0;
            
        }
        .detection-info {
            margin-top: 20px;
            padding: 10px;
            background: rgba(0, 255, 0, 0.1);
            border: 1px solid #0f0;
            border-radius: 4px;
            width: 100%;
            max-width: 620px;
        }
        .stats {
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
            font-size: 14px;
        }
        .night-vision {
            filter: brightness(2) contrast(1.2) hue-rotate(120deg) grayscale(0.5);
        }
        .detection-box {
            position: absolute;
            border: 2px solid #0f0;
            background: rgba(0, 255, 0, 0.1);
        }
        .detection-label {
            position: absolute;
            top: -25px;
            left: 0;
            background: #0f0;
            color: #000;
            padding: 2px 6px;
            font-size: 12px;
            border-radius: 2px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="video-container">
            <video id="video" autoplay playsinline></video>
            <canvas id="canvas"></canvas>
        </div>
        <div class="detection-info">
            <div id="detections"></div>
            <div class="stats">
                <span id="fps">FPS: 0</span>
                <span id="objects">Objects detected: 0</span>
            </div>
        </div>
    </div>

    <script>
        let video = document.getElementById('video');
        let canvas = document.getElementById('canvas');
        let ctx = canvas.getContext('2d');
        let model;
        let isNightVision = true;  // Night vision is enabled by default
        let isDetecting = true;    // Detection is enabled by default
        let lastTime = performance.now();
        let frameCount = 0;

        // Initialize camera and AI model
        async function init() {
            console.log('Loading COCO-SSD model...');
            // Load COCO-SSD model
            model = await cocoSsd.load();
            console.log('COCO-SSD model loaded.');

            // Setup camera
            const constraints = {
                video: {
                    width: 640,
                    height: 480,
                    facingMode: 'environment',
                    advanced: [{
                        exposureMode: 'manual',
                        exposureCompensation: 2
                    }]
                }
            };
            const stream = await navigator.mediaDevices.getUserMedia(constraints);
            video.srcObject = stream;

            // Set canvas size
            canvas.width = 640;
            canvas.height = 480;

            // Enable night vision by default
            if (isNightVision) {
                video.className = 'night-vision';
            }

            // Check if video is playing
            video.onplaying = () => {
                console.log('Video stream started successfully.');
            };

            video.onerror = (e) => {
                console.error('Error starting video stream:', e);
            };

            // Start detection loop
            requestAnimationFrame(detect);
        }

        async function detect() {
            if (isDetecting) {
                // Calculate FPS
                const now = performance.now();
                frameCount++;
                if (now - lastTime >= 1000) {
                    document.getElementById('fps').textContent = `FPS: ${frameCount}`;
                    frameCount = 0;
                    lastTime = now;
                }

                // Detect objects
                try {
                    const predictions = await model.detect(video);

                    // Clear previous detections
                    ctx.clearRect(0, 0, canvas.width, canvas.height);

                    // Draw new detections
                    predictions.forEach(prediction => {
                        // Draw bounding box
                        ctx.strokeStyle = '#00ff00';
                        ctx.lineWidth = 2;
                        ctx.strokeRect(
                            prediction.bbox[0],
                            prediction.bbox[1],
                            prediction.bbox[2],
                            prediction.bbox[3]
                        );

                        // Draw label background
                        ctx.fillStyle = '#00ff00';
                        ctx.fillRect(
                            prediction.bbox[0],
                            prediction.bbox[1] - 20,
                            prediction.bbox[2],
                            20
                        );

                        // Draw label text
                        ctx.fillStyle = '#000000';
                        ctx.font = '16px monospace';
                        ctx.fillText(
                            `${prediction.class} ${Math.round(prediction.score * 100)}%`,
                            prediction.bbox[0] + 5,
                            prediction.bbox[1] - 5
                        );
                    });

                    // Update detection info
                    document.getElementById('objects').textContent =
                        `Objects detected: ${predictions.length}`;

                    document.getElementById('detections').innerHTML =
                        predictions.map(p =>
                            `Detected ${p.class} (${Math.round(p.score * 100)}% confidence)`
                        ).join('<br>');

                } catch (error) {
                    console.error('Error in detection:', error);
                }
            }

            // Continue calling the detection loop
            requestAnimationFrame(detect);
        }

        // Start application
        init().catch(err => {
            console.error('Error initializing application:', err);
        });

        // Add image processing for better night vision
        const imageProcessor = new ImageCapture(video.srcObject.getVideoTracks()[0]);

        async function enhanceNightVision() {
            if (isNightVision) {
                try {
                    const photoCapabilities = await imageProcessor.getPhotoCapabilities();
                    await imageProcessor.setOptions({
                        brightness: photoCapabilities.brightness.max,
                        contrast: photoCapabilities.contrast.max,
                        saturation: 0,
                        sharpness: photoCapabilities.sharpness.max,
                        exposureMode: 'manual',
                        exposureCompensation: 2,
                        whiteBalanceMode: 'manual'
                    });
                } catch (err) {
                    console.log('Night vision enhancement not supported');
                }
            }
        }
    </script>
</body>
</html>