Skip to content

Commit 02e0d6c

Browse files
committed
Update camera.html
1 parent 6b126b6 commit 02e0d6c

File tree

1 file changed

+135
-108
lines changed

1 file changed

+135
-108
lines changed

ng_sandbox/camera.html

Lines changed: 135 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -3,131 +3,158 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6-
<title>Camera + Visual Search</title>
6+
<title>Camera Visual Search (Cached Embeddings)</title>
7+
78
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.11.0/dist/tf.min.js"></script>
89
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@3.1.0/dist/mobilenet.min.js"></script>
10+
911
<style>
1012
body { font-family: Arial, sans-serif; text-align: center; padding: 1em; }
11-
video, canvas { width: 100%; max-width: 400px; }
12-
#result-img { margin-top: 1em; max-width: 400px; }
13+
video, canvas, img { width: 100%; max-width: 400px; }
14+
#status { margin: 1em 0; font-weight: bold; }
1315
</style>
1416
</head>
1517
<body>
1618

17-
<h1>Visual Search from Camera</h1>
19+
<h1>Visual Search</h1>
20+
<p id="status">Loading model…</p>
1821

1922
<video id="video" autoplay playsinline></video>
2023
<br />
21-
<button id="capture-btn">Take Photo</button>
22-
23-
<canvas id="capture-canvas" style="display:none;"></canvas>
24-
25-
<h2>Most Similar Match:</h2>
26-
<img id="result-img" alt="Matching image will appear here">
27-
28-
<script>
29-
let model;
30-
const video = document.getElementById("video");
31-
const canvas = document.getElementById("capture-canvas");
32-
const ctx = canvas.getContext("2d");
33-
const resultImg = document.getElementById("result-img");
34-
35-
// 1️⃣ Start camera
36-
async function startCamera() {
37-
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
38-
video.srcObject = stream;
39-
}
24+
<button id="capture-btn" disabled>Take Photo</button>
25+
26+
<canvas id="canvas" style="display:none;"></canvas>
27+
28+
<h2>Best Match</h2>
29+
<img id="result-img" />
30+
31+
<script>
32+
let model;
33+
let collectionEmbeddings = []; // { url, embedding }
34+
35+
const video = document.getElementById("video");
36+
const canvas = document.getElementById("canvas");
37+
const ctx = canvas.getContext("2d");
38+
const statusEl = document.getElementById("status");
39+
const resultImg = document.getElementById("result-img");
40+
const captureBtn = document.getElementById("capture-btn");
41+
42+
/* ------------------ Camera ------------------ */
43+
async function startCamera() {
44+
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
45+
video.srcObject = stream;
46+
}
47+
48+
/* ------------------ Model ------------------ */
49+
async function loadModel() {
50+
model = await mobilenet.load({ version: 2, alpha: 1.0 });
51+
console.log("MobileNet loaded");
52+
}
53+
54+
/* ------------------ JSON ------------------ */
55+
async function loadCollection() {
56+
const res = await fetch(
57+
"https://johnstack.github.io/JavaScript-Sandpit/ng_sandbox/collection.sample.json"
58+
);
59+
const json = await res.json();
60+
return json.items; // adjust if structure differs
61+
}
62+
63+
/* ------------------ Embeddings ------------------ */
64+
async function getEmbedding(img) {
65+
const tensor = tf.browser.fromPixels(img)
66+
.resizeNearestNeighbor([224, 224])
67+
.toFloat()
68+
.expandDims();
69+
70+
const embedding = model.infer(tensor, "conv_preds");
71+
const data = embedding.dataSync();
72+
73+
tensor.dispose();
74+
embedding.dispose();
75+
76+
return data;
77+
}
78+
79+
/* ------------------ Similarity ------------------ */
80+
function cosineSimilarity(a, b) {
81+
let dot = 0, magA = 0, magB = 0;
82+
for (let i = 0; i < a.length; i++) {
83+
dot += a[i] * b[i];
84+
magA += a[i] * a[i];
85+
magB += b[i] * b[i];
86+
}
87+
return dot / (Math.sqrt(magA) * Math.sqrt(magB));
88+
}
89+
90+
/* ------------------ Preload Embeddings ------------------ */
91+
async function preloadEmbeddings() {
92+
statusEl.textContent = "Loading image collection…";
93+
94+
const items = await loadCollection();
95+
96+
for (let item of items) {
97+
const img = new Image();
98+
img.crossOrigin = "anonymous";
99+
img.src = item.url; // adjust key if needed
100+
101+
await new Promise(resolve => img.onload = resolve);
102+
103+
const embedding = await getEmbedding(img);
104+
collectionEmbeddings.push({
105+
url: item.url,
106+
embedding
107+
});
40108

41-
// 2️⃣ Load MobileNet
42-
async function loadModel() {
43-
model = await mobilenet.load();
44-
console.log("MobileNet loaded");
45-
}
109+
statusEl.textContent = `Indexed ${collectionEmbeddings.length}/${items.length}`;
110+
}
46111

47-
// 3️⃣ Fetch JSON collection
48-
async function loadCollection() {
49-
const res = await fetch(
50-
"https://johnstack.github.io/JavaScript-Sandpit/ng_sandbox/collection.sample.json"
51-
);
52-
const json = await res.json();
53-
return json.items; // adjust based on JSON structure
54-
}
112+
statusEl.textContent = "Ready! Take a photo.";
113+
captureBtn.disabled = false;
114+
}
55115

56-
// 4️⃣ Compute embedding
57-
async function getEmbedding(imgElement) {
58-
const logits = model.infer(imgElement, "conv_preds");
59-
return logits.dataSync(); // embedding vector
60-
}
116+
/* ------------------ Search ------------------ */
117+
async function findBestMatch(capturedImg) {
118+
const captureEmbedding = await getEmbedding(capturedImg);
61119

62-
// 5️⃣ Compute cosine similarity
63-
function cosineSim(a, b) {
64-
let dot = 0, magA = 0, magB = 0;
65-
for (let i = 0; i < a.length; i++) {
66-
dot += a[i] * b[i];
67-
magA += a[i] * a[i];
68-
magB += b[i] * b[i];
69-
}
70-
return dot / (Math.sqrt(magA) * Math.sqrt(magB));
71-
}
120+
let bestScore = -1;
121+
let bestMatch = null;
72122

73-
// 6️⃣ Find nearest
74-
async function findNearest(captureImg, collection) {
75-
const captureEmbedding = await getEmbedding(captureImg);
76-
77-
let bestSim = -1;
78-
let bestImg = null;
79-
80-
for (let item of collection) {
81-
// load collection image
82-
const tempImg = new Image();
83-
tempImg.crossOrigin = "anonymous";
84-
tempImg.src = item.url; // adjust if JSON key differs
85-
86-
await new Promise((resolve) => {
87-
tempImg.onload = resolve;
88-
tempImg.onerror = resolve; // skip broken links
89-
});
90-
91-
const emb = await getEmbedding(tempImg);
92-
const sim = cosineSim(captureEmbedding, emb);
93-
94-
if (sim > bestSim) {
95-
bestSim = sim;
96-
bestImg = item.url;
97-
}
98-
}
99-
return bestImg;
123+
for (let item of collectionEmbeddings) {
124+
const score = cosineSimilarity(captureEmbedding, item.embedding);
125+
if (score > bestScore) {
126+
bestScore = score;
127+
bestMatch = item.url;
100128
}
101-
102-
103-
// 🔘 Capture button logic
104-
document.getElementById("capture-btn").addEventListener("click", async () => {
105-
canvas.width = video.videoWidth;
106-
canvas.height = video.videoHeight;
107-
ctx.drawImage(video, 0, 0);
108-
109-
const dataUrl = canvas.toDataURL("image/jpeg");
110-
const captureImg = new Image();
111-
captureImg.src = dataUrl;
112-
113-
captureImg.onload = async () => {
114-
resultImg.src = "Searching...";
115-
const collection = await loadCollection();
116-
const nearestUrl = await findNearest(captureImg, collection);
117-
118-
if (nearestUrl) {
119-
resultImg.src = nearestUrl;
120-
} else {
121-
resultImg.alt = "No similar image found";
122-
}
123-
};
124-
});
125-
126-
127-
// Initialize
128-
startCamera();
129-
loadModel();
130-
</script>
129+
}
130+
131+
return bestMatch;
132+
}
133+
134+
/* ------------------ Capture ------------------ */
135+
captureBtn.addEventListener("click", async () => {
136+
canvas.width = video.videoWidth;
137+
canvas.height = video.videoHeight;
138+
ctx.drawImage(video, 0, 0);
139+
140+
const img = new Image();
141+
img.src = canvas.toDataURL("image/jpeg");
142+
143+
img.onload = async () => {
144+
statusEl.textContent = "Searching…";
145+
const match = await findBestMatch(img);
146+
resultImg.src = match;
147+
statusEl.textContent = "Match found!";
148+
};
149+
});
150+
151+
/* ------------------ Init ------------------ */
152+
(async function init() {
153+
await startCamera();
154+
await loadModel();
155+
await preloadEmbeddings();
156+
})();
157+
</script>
131158

132159
</body>
133160
</html>

0 commit comments

Comments
 (0)