File size: 8,026 Bytes
7597618
a153c1b
7597618
a153c1b
7597618
 
 
a153c1b
7597618
a153c1b
7597618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a153c1b
 
7597618
 
a153c1b
 
7597618
 
 
 
 
a153c1b
 
 
7597618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a153c1b
 
 
 
7597618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a153c1b
 
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

<!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;
            transform: scaleX(-1);
        }

        #canvas {
            position: absolute;
            top: 0;
            left: 0;
            transform: scaleX(-1);
        }

        .controls {
            margin-top: 20px;
            display: flex;
            gap: 10px;
        }

        button {
            background: #0f0;
            color: #000;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
            transition: all 0.3s;
        }

        button:hover {
            background: #0f0;
            box-shadow: 0 0 10px #0f0;
        }

        .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="controls">
            <button onclick="toggleNightVision()">Toggle Night Vision</button>
            <button onclick="toggleDetection()">Toggle Detection</button>
        </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 = false;
        let isDetecting = false;
        let lastTime = performance.now();
        let frameCount = 0;

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

            // 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;

            // Start detection loop
            requestAnimationFrame(detect);
        }

        function toggleNightVision() {
            isNightVision = !isNightVision;
            video.className = isNightVision ? 'night-vision' : '';
        }

        function toggleDetection() {
            isDetecting = !isDetecting;
        }

        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
                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>');
            }

            requestAnimationFrame(detect);
        }

        // Start application
        init().catch(err => {
            console.error('Error initializing camera:', 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>