-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
211 lines (162 loc) · 4.4 KB
/
index.html
File metadata and controls
211 lines (162 loc) · 4.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>AI Object Detection</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<script src="https://unpkg.com/ml5@0.12.2/dist/ml5.min.js"></script>
<style>
body {
text-align: center;
background: #111;
color: white;
}
video {
width: 0px;
height: 0px;
}
canvas {
border-radius: 12px;
box-shadow: 0 0 20px rgba(0, 255, 0, 0.3);
}
table {
width: auto;
margin: auto;
}
#objectCount {
font-size: 18px;
margin-top: 10px;
font-weight: bold;
color: #00ff99;
}
</style>
</head>
<body>
<h3 id="loadingText">Loading AI Model & Camera...</h3>
<video playsinline autoplay muted id="video"></video>
<br><br>
<canvas id="c1"></canvas>
<div id="objectCount">Objects Detected: 0</div>
<br>
<table>
<tr>
<td>AI:</td>
<td>
<div class="switch">
<label>
Off
<input type="checkbox" id="ai" disabled>
<span class="lever"></span>
On
</label>
</div>
</td>
</tr>
</table>
<script>
/* ---------------- MODEL SETUP ---------------- */
let modelIsLoaded = false;
let detecting = false;
const objectDetector = ml5.objectDetector('cocossd', {}, () => {
console.log("Model Loaded!");
modelIsLoaded = true;
});
/* ---------------- DOM ELEMENTS ---------------- */
const video = document.getElementById("video");
const canvas = document.getElementById("c1");
const ctx = canvas.getContext("2d");
const aiToggle = document.getElementById("ai");
const loadingText = document.getElementById("loadingText");
const objectCountText = document.getElementById("objectCount");
/* ---------------- VARIABLES ---------------- */
let cameraAvailable = false;
let aiEnabled = false;
let fps = 20;
/* ---------------- CAMERA ---------------- */
const constraints = {
audio: false,
video: { facingMode: "environment" }
};
startCamera();
function startCamera() {
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
cameraAvailable = true;
video.srcObject = stream;
video.addEventListener("loadedmetadata", () => {
setResolution();
mainLoop();
});
})
.catch(err => {
loadingText.innerText = "Camera permission required";
console.error(err);
});
}
/* ---------------- READY CHECK ---------------- */
function isReady() {
if (modelIsLoaded && cameraAvailable) {
// loadingText.style.display = "none";
loadingText.innerText = "Ai Object Detection";
aiToggle.disabled = false;
return true;
}
return false;
}
/* ---------------- RESOLUTION ---------------- */
function setResolution() {
if (window.innerWidth < video.videoWidth) {
canvas.width = window.innerWidth * 0.9;
let ratio = canvas.width / video.videoWidth;
canvas.height = video.videoHeight * ratio;
} else {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
}
}
/* ---------------- EVENTS ---------------- */
aiToggle.addEventListener("change", () => {
aiEnabled = aiToggle.checked;
});
/* ---------------- MAIN LOOP ---------------- */
function mainLoop() {
if (isReady()) {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
if (aiEnabled) {
runDetection();
}
}
setTimeout(mainLoop, 1000 / fps);
}
/* ---------------- AI DETECTION ---------------- */
function runDetection() {
if (detecting) return;
detecting = true;
objectDetector.detect(canvas, (err, results) => {
detecting = false;
if (err || !results) return;
objectCountText.innerText = "Objects Detected: " + results.length;
results.forEach(obj => {
// Draw Bounding Box
ctx.beginPath();
ctx.strokeStyle = "#00ff99";
ctx.lineWidth = 3;
ctx.rect(obj.x, obj.y, obj.width, obj.height);
ctx.stroke();
// Draw Label
ctx.fillStyle = "#00ff99";
ctx.font = "16px Arial";
ctx.fillText(
obj.label + " (" + (obj.confidence * 100).toFixed(1) + "%)",
obj.x + 5,
obj.y > 20 ? obj.y - 5 : 20
);
});
});
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</body>
</html>