File size: 14,995 Bytes
a566fb0 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enhanced Object Detection</title>
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<style>
body { font-family: 'Roboto', sans-serif; margin: 2em; color: #3d3d3d; background: #f0f2f5; }
h1 { color: #007f8b; text-align: center; }
.container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
/* Controls Section */
.controls { display: flex; gap: 20px; flex-wrap: wrap; margin-bottom: 20px; padding: 15px; background: #e6fcfd; border-radius: 8px; align-items: center; }
.control-group { display: flex; flex-direction: column; min-width: 200px; }
label { font-weight: bold; font-size: 0.9em; margin-bottom: 5px; color: #007f8b; }
input[type=range] { width: 100%; }
.upload-btn { background: #007f8b; color: white; padding: 10px 20px; border-radius: 25px; cursor: pointer; display: inline-block; font-weight: bold; text-align: center; }
.upload-btn:hover { background: #006069; }
input[type="file"] { display: none; }
/* Image Grid */
/* Replace the existing .image-grid and .detect-card styles */
.image-grid {
/* Disable Grid, use Columns instead */
display: block;
column-count: 3; /* Creates 3 columns like Pinterest */
column-gap: 20px;
}
/* Responsive: 2 columns on smaller screens */
@media (max-width: 900px) {
.image-grid { column-count: 2; }
}
@media (max-width: 600px) {
.image-grid { column-count: 1; }
}
.detect-card {
position: relative;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
background: #000;
cursor: pointer;
transition: transform 0.2s;
z-index: 1;
/* NEW: Prevents card from splitting across columns */
break-inside: avoid-column;
margin-bottom: 20px;
}
.detect-card:hover { transform: scale(1.01); }
.detect-card img { display: block; width: 100%; height: auto; transition: opacity 0.3s; }
/* Processing State */
.detect-card.processing { pointer-events: none; } /* Prevent double clicks */
.detect-card.processing img { opacity: 0.6; filter: grayscale(50%); }
/* --- NEW: Inference Loading Bar --- */
.inference-panel {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background: rgba(255, 255, 255, 0.95);
padding: 15px;
box-sizing: border-box;
transform: translateY(100%); /* Hidden by default */
transition: transform 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
z-index: 50;
border-top: 3px solid #007f8b;
}
/* Show panel when processing */
.detect-card.processing .inference-panel {
transform: translateY(0);
}
.inference-status {
display: flex;
justify-content: space-between;
font-weight: bold;
color: #007f8b;
margin-bottom: 8px;
font-size: 0.9rem;
}
.progress-track {
width: 100%;
height: 6px;
background: #e0e0e0;
border-radius: 3px;
overflow: hidden;
}
.progress-bar {
height: 100%;
background: #007f8b;
width: 30%;
border-radius: 3px;
animation: loading 1.5s infinite ease-in-out;
}
@keyframes loading {
0% { transform: translateX(-100%); }
100% { transform: translateX(400%); }
}
/* ---------------------------------- */
/* Bounding Boxes */
.highlighter { position: absolute; border: 2px solid; border-radius: 4px; z-index: 10; pointer-events: none; }
.label-tag { position: absolute; padding: 2px 6px; color: white; font-size: 11px; font-weight: bold; border-radius: 4px; pointer-events: none; z-index: 11; white-space: nowrap; box-shadow: 0 1px 2px rgba(0,0,0,0.2); }
/* Loading Spinner (Initial Model Load) */
#loader { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; background: white; border-radius: 8px; box-shadow: 0 0 20px rgba(0,0,0,0.2); display: none; z-index: 1000; font-weight: bold; }
</style>
</head>
<body>
<div class="container">
<h1>Smart Object Recognition</h1>
<div class="controls">
<div class="control-group">
<label for="imageUpload" class="upload-btn">📂 Upload Image</label>
<input type="file" id="imageUpload" accept="image/*">
</div>
<div class="control-group">
<label>Confidence Threshold: <span id="confValue">50</span>%</label>
<input type="range" id="confidenceSlider" min="10" max="90" value="50">
<small>Increase to remove weak guesses.</small>
</div>
<div class="control-group">
<label>Overlap Fix (NMS): <span id="overlapValue">30</span>%</label>
<input type="range" id="overlapSlider" min="0" max="100" value="30">
<small>Lower value = Fewer overlapping boxes.</small>
</div>
</div>
<div id="loader">Loading AI Model...</div>
<div class="image-grid" id="imageContainer">
<!-- Image 1 -->
<div class="detect-card">
<img src="https://assets.codepen.io/9177687/coupledog.jpeg" crossorigin="anonymous" />
</div>
<!-- Image 2 -->
<div class="detect-card">
<img src="https://assets.codepen.io/9177687/doggo.jpeg" crossorigin="anonymous" />
</div>
<!-- Image 3 (FIXED: Added wrapper div) -->
<div class="detect-card">
<img src="https://tse3.mm.bing.net/th/id/OIP.mIJJ36cXpVujF1wnZnd4VQHaE8?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 4 -->
<div class="detect-card">
<img src="https://images.pexels.com/photos/23409055/pexels-photo-23409055/free-photo-of-cars-on-street-in-town.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" crossorigin="anonymous" />
</div>
<!-- Image 5 -->
<div class="detect-card">
<img src="https://tse4.mm.bing.net/th/id/OIP.bWwaHeR-aoBb3esBRaAEEgHaE8?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 6 -->
<div class="detect-card">
<img src="https://tse4.mm.bing.net/th/id/OIP.vs_d1C-7n4PoNv0GVlaVDwHaFj?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 7 -->
<div class="detect-card">
<img src="https://tse4.mm.bing.net/th/id/OIP.V1zVa5IUI22o0i6gG4or2QHaLH?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 8 -->
<div class="detect-card">
<img src="https://th.bing.com/th/id/R.2be55af1ab4a38df1a9b54bf6b68a8bd?rik=f7wiKdc8N42mgA&pid=ImgRaw&r=0" crossorigin="anonymous" />
</div>
<!-- Image 9 -->
<div class="detect-card">
<img src="https://images.pexels.com/photos/20625972/pexels-photo-20625972.jpeg?cs=srgb&dl=pexels-saturnus99-20625972.jpg&fm=jpg" crossorigin="anonymous" />
</div>
<!-- Image 10 -->
<div class="detect-card">
<img src="https://tse4.mm.bing.net/th/id/OIP.ZMnNqw1GVTa9HpHvsTYcjQAAAA?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 11 -->
<div class="detect-card">
<img src="https://bestbackpacklab.com/wp-content/uploads/2021/05/children-1536x864.jpg" crossorigin="anonymous" />
</div>
<!-- Image 12 -->
<div class="detect-card">
<img src="https://tse1.explicit.bing.net/th/id/OIP.za2l0WGKXbR4Qkj8phu2UwHaE8?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 13 -->
<div class="detect-card">
<img src="https://tse2.mm.bing.net/th/id/OIP.bcOP7ZTpLAyyl3tKpdk5gAHaFB?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 14 -->
<div class="detect-card">
<img src="https://tse3.mm.bing.net/th/id/OIP.PC6Fr2mEuGUsEiaNfCSOaAHaE7?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 15 -->
<div class="detect-card">
<img src="https://tse3.mm.bing.net/th/id/OIF.EABwKojMHBX0uEfpxor95w?rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
<!-- Image 16 -->
<div class="detect-card">
<img src="https://tse4.mm.bing.net/th/id/OIP.qliYrfiREN-ydW4DxWYfSgHaE7?w=626&h=417&rs=1&pid=ImgDetMain&o=7&rm=3" crossorigin="anonymous" />
</div>
</div>
</div>
<script type="module">
import { ObjectDetector, FilesetResolver } from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.2";
const loader = document.getElementById("loader");
let objectDetector;
let runningMode = "IMAGE";
// SETTINGS
let confidenceThreshold = 0.5;
let overlapThreshold = 0.3;
// 1. Initialize MediaPipe
const initializeObjectDetector = async () => {
loader.style.display = "block";
const vision = await FilesetResolver.forVisionTasks("https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.2/wasm");
objectDetector = await ObjectDetector.createFromOptions(vision, {
baseOptions: {
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/object_detector/efficientdet_lite2/float16/1/efficientdet_lite2.tflite`,
delegate: "GPU"
},
scoreThreshold: 0.2,
runningMode: runningMode
});
loader.style.display = "none";
console.log("Model Loaded: EfficientDet-Lite2");
};
initializeObjectDetector();
// 2. NMS Filter Function
function filterDetections(detections, iouLimit) {
detections.sort((a, b) => b.categories[0].score - a.categories[0].score);
const selected = [];
const active = new Array(detections.length).fill(true);
for (let i = 0; i < detections.length; i++) {
if (!active[i]) continue;
if (detections[i].categories[0].score < confidenceThreshold) continue;
selected.push(detections[i]);
const boxA = detections[i].boundingBox;
for (let j = i + 1; j < detections.length; j++) {
if (!active[j]) continue;
const boxB = detections[j].boundingBox;
const x1 = Math.max(boxA.originX, boxB.originX);
const y1 = Math.max(boxA.originY, boxB.originY);
const x2 = Math.min(boxA.originX + boxA.width, boxB.originX + boxB.width);
const y2 = Math.min(boxA.originY + boxA.height, boxB.originY + boxB.height);
if (x2 < x1 || y2 < y1) continue;
const intersection = (x2 - x1) * (y2 - y1);
const areaA = boxA.width * boxA.height;
const areaB = boxB.width * boxB.height;
const union = areaA + areaB - intersection;
const iou = intersection / union;
if (iou > iouLimit) {
active[j] = false;
}
}
}
return selected;
}
// 3. Handle Clicks & Draw
async function handleClick(event) {
if (!objectDetector) return;
const img = event.target;
const card = img.parentNode;
// -- NEW: Inject/Show Inference Bar --
let infoPanel = card.querySelector('.inference-panel');
if (!infoPanel) {
infoPanel = document.createElement('div');
infoPanel.className = 'inference-panel';
infoPanel.innerHTML = `
<div class="inference-status">
<span>Running Inference...</span>
<span>Please wait</span>
</div>
<div class="progress-track">
<div class="progress-bar"></div>
</div>
`;
card.appendChild(infoPanel);
}
// 1. Show Loading State
card.classList.add('processing');
// Clear old boxes immediately so the user sees a "reset"
card.querySelectorAll('.highlighter, .label-tag').forEach(el => el.remove());
// 2. Force a tiny delay so the browser renders the loading bar
// before the heavy synchronous AI detection freezes the thread.
await new Promise(resolve => requestAnimationFrame(() => setTimeout(resolve, 50)));
try {
// 3. Run Detection
const predictions = objectDetector.detect(img);
const filteredDetections = filterDetections(predictions.detections, overlapThreshold);
displayDetections(filteredDetections, img);
} catch(e) {
console.error(e);
alert("Error running model");
} finally {
// 4. Hide Loading State
card.classList.remove('processing');
}
}
function displayDetections(detections, img) {
const ratioX = img.width / img.naturalWidth;
const ratioY = img.height / img.naturalHeight;
detections.forEach(detection => {
const box = detection.boundingBox;
const category = detection.categories[0];
const score = Math.round(category.score * 100);
const color = getColorForLabel(category.categoryName);
const highlighter = document.createElement("div");
highlighter.className = "highlighter";
highlighter.style.left = `${box.originX * ratioX}px`;
highlighter.style.top = `${box.originY * ratioY}px`;
highlighter.style.width = `${box.width * ratioX}px`;
highlighter.style.height = `${box.height * ratioY}px`;
highlighter.style.borderColor = color;
highlighter.style.backgroundColor = color + "20";
const label = document.createElement("div");
label.className = "label-tag";
label.innerText = `${category.categoryName} ${score}%`;
label.style.backgroundColor = color;
const topPos = (box.originY * ratioY) - 25;
label.style.left = `${box.originX * ratioX}px`;
label.style.top = `${topPos > 0 ? topPos : (box.originY * ratioY)}px`;
img.parentNode.appendChild(highlighter);
img.parentNode.appendChild(label);
});
}
function getColorForLabel(label) {
let hash = 0;
for (let i = 0; i < label.length; i++) {
hash = label.charCodeAt(i) + ((hash << 5) - hash);
}
const c = (hash & 0x00FFFFFF).toString(16).toUpperCase();
return "#" + "00000".substring(0, 6 - c.length) + c;
}
// 4. Initialization & Event Listeners
const imageContainer = document.getElementById("imageContainer");
imageContainer.addEventListener('click', (e) => {
if (e.target.tagName === 'IMG') handleClick(e);
});
document.getElementById('imageUpload').addEventListener('change', (e) => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
const div = document.createElement('div');
div.className = 'detect-card';
const img = document.createElement('img');
img.src = event.target.result;
div.appendChild(img);
imageContainer.insertBefore(div, imageContainer.firstChild);
};
reader.readAsDataURL(file);
});
document.getElementById('confidenceSlider').addEventListener('input', (e) => {
confidenceThreshold = e.target.value / 100;
document.getElementById('confValue').innerText = e.target.value;
});
document.getElementById('overlapSlider').addEventListener('input', (e) => {
overlapThreshold = e.target.value / 100;
document.getElementById('overlapValue').innerText = e.target.value;
});
</script>
</body>
</html>
|