Skip to content

Commit 6b126b6

Browse files
committed
Create camera.html
1 parent 073eb35 commit 6b126b6

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

ng_sandbox/camera.html

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
6+
<title>Camera + Visual Search</title>
7+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.11.0/dist/tf.min.js"></script>
8+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@3.1.0/dist/mobilenet.min.js"></script>
9+
<style>
10+
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+
</style>
14+
</head>
15+
<body>
16+
17+
<h1>Visual Search from Camera</h1>
18+
19+
<video id="video" autoplay playsinline></video>
20+
<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+
}
40+
41+
// 2️⃣ Load MobileNet
42+
async function loadModel() {
43+
model = await mobilenet.load();
44+
console.log("MobileNet loaded");
45+
}
46+
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+
}
55+
56+
// 4️⃣ Compute embedding
57+
async function getEmbedding(imgElement) {
58+
const logits = model.infer(imgElement, "conv_preds");
59+
return logits.dataSync(); // embedding vector
60+
}
61+
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+
}
72+
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;
100+
}
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>
131+
132+
</body>
133+
</html>

0 commit comments

Comments
 (0)