Spaces:
Sleeping
Sleeping
File size: 9,185 Bytes
0d186b4 fc9a733 0d186b4 fc9a733 0d186b4 fc9a733 0d186b4 fc9a733 0d186b4 fc9a733 0d186b4 fc9a733 0d186b4 fc9a733 0d186b4 |
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 |
import { AutoModel, AutoProcessor, RawImage } from "@huggingface/transformers";
import { Client } from "@gradio/client";
// Reference the elements that we will need
const status = document.getElementById("status");
const container = document.getElementById("container");
const overlay = document.getElementById("overlay");
const canvas = document.getElementById("canvas");
const video = document.getElementById("video");
const thresholdSlider = document.getElementById("threshold");
const thresholdLabel = document.getElementById("threshold-value");
const sizeSlider = document.getElementById("size");
const sizeLabel = document.getElementById("size-value");
const scaleSlider = document.getElementById("scale");
const scaleLabel = document.getElementById("scale-value");
function setStreamSize(width, height) {
video.width = canvas.width = Math.round(width);
video.height = canvas.height = Math.round(height);
}
status.textContent = "Loading model...";
// Load model and processor
const model_id = "Xenova/gelan-c_all";
const model = await AutoModel.from_pretrained(model_id);
const processor = await AutoProcessor.from_pretrained(model_id);
// Set up controls
let scale = 0.5;
scaleSlider.addEventListener("input", () => {
scale = Number(scaleSlider.value);
setStreamSize(video.videoWidth * scale, video.videoHeight * scale);
scaleLabel.textContent = scale;
});
scaleSlider.disabled = false;
let threshold = 0.80;
thresholdSlider.addEventListener("input", () => {
threshold = Number(thresholdSlider.value);
thresholdLabel.textContent = threshold.toFixed(2);
});
thresholdSlider.disabled = false;
let size = 128;
processor.feature_extractor.size = { shortest_edge: size };
sizeSlider.addEventListener("input", () => {
size = Number(sizeSlider.value);
processor.feature_extractor.size = { shortest_edge: size };
sizeLabel.textContent = size;
});
sizeSlider.disabled = false;
status.textContent = "Ready";
const COLOURS = [
"#EF4444", "#4299E1", "#059669", "#FBBF24", "#4B52B1", "#7B3AC2",
"#ED507A", "#1DD1A1", "#F3873A", "#4B5563", "#DC2626", "#1852B4",
"#18A35D", "#F59E0B", "#4059BE", "#6027A5", "#D63D60", "#00AC9B",
"#E64A19", "#272A34",
];
// Function to send canvas image to Gradio API
// async function sendCanvasImageToAPI(canvas) {
// return new Promise((resolve, reject) => {
// canvas.toBlob(async (blob) => {
// if (!blob) {
// reject("Failed to get Blob from canvas");
// return;
// }
// const file1 = new File([blob], "detected.png", { type: "image/png" });
// try {
// // Fetch image from URL and convert to Blob
// const response = await fetch("https://qvnhhditkzzeudppuezf.supabase.co/storage/v1/object/public/post-images/post-images/1752289670997-kevan.jpg");
// const frame2Blob = await response.blob();
// const file2 = new File([frame2Blob], "frame2.jpg", { type: frame2Blob.type });
// const client = await Client.connect("MiniAiLive/FaceRecognition-LivenessDetection-Demo");
// // Send canvas image as frame1, URL image as frame2
// const result = await client.predict("/face_compare", {
// frame1: file1,
// frame2: file2,
// });
// resolve(result);
// } catch (err) {
// reject(err);
// }
// }, "image/png");
// });
// }
async function sendCanvasImageToAPI(canvas) {
return new Promise((resolve, reject) => {
canvas.toBlob(async (blob) => {
if (!blob) {
reject("Failed to get Blob from canvas");
return;
}
const file = new File([blob], "detected.png", { type: "image/png" });
try {
const formData = new FormData();
formData.append("image", file);
const response = await fetch("http://localhost:8080/call-face-recognition", {
method: "POST",
body: formData,
});
if (!response.ok) {
reject("Failed to send image: " + response.statusText);
return;
}
const result = await response.text(); // or JSON if backend returns JSON
resolve(result);
} catch (err) {
reject(err);
}
}, "image/png");
});
}
// Variables to store current bounding box and label elements
let currentBoxElement = null;
let currentLabelElement = null;
let hasSent = false; // Flag to send API request only once per detection
var color = "yellow";
var text = "Verifying..."
// Render a bounding box and label on the image
function renderBox([xmin, ymin, xmax, ymax, score, id], [w, h]) {
if (score < threshold) return; // Skip boxes with low confidence
// Create bounding box div
let boxElement = document.createElement("div");
boxElement.className = "bounding-box";
Object.assign(boxElement.style, {
borderColor: color,
left: (100 * xmin) / w + "%",
top: (100 * ymin) / h + "%",
width: (100 * (xmax - xmin)) / w + "%",
height: (100 * (ymax - ymin)) / h + "%",
});
// Create label span
let labelElement = document.createElement("span");
labelElement.textContent = text;
labelElement.className = "bounding-box-label";
labelElement.style.backgroundColor = color;
labelElement.style.color = "black";
boxElement.appendChild(labelElement);
overlay.appendChild(boxElement);
// Store references globally for updating after API response
currentBoxElement = boxElement;
currentLabelElement = labelElement;
// Send image to the API on first detection
if (!hasSent) {
hasSent = true;
sendCanvasImageToAPI(canvas)
.then((response) => {
const responseObj = JSON.parse(response);
const confidenceStr = responseObj.result; // "0.982708"
// Extract decimal part only:
// const decimalPart = confidenceStr.slice(confidenceStr.indexOf('.') + 1);
// console.log(decimalPart); // e.g., "982708"
// Convert to float for comparison
const decimalNumber = parseFloat(confidenceStr);
console.log(decimalNumber);
if (decimalNumber !== null && decimalNumber > 0.80) {
// --- MODIFICATION START ---
// Instead of removing and recreating the box, just update its style.
color = "green";
text = "Verified Successfully! " + decimalNumber;
currentLabelElement.style.color = "black"; // Or "white" if it looks better
console.log("Updated box to green");
// --- MODIFICATION END ---
} else if (decimalNumber !== null) {
// Not identified case - update existing box and label to red
color = "red";
text = "Failed to verify"
currentLabelElement.style.color = "black"; // Or "white" if it looks better
console.log("Updated box to red");
} else {
// Fallback yellow verifying state
currentLabelElement.textContent = "Verifying...";
currentLabelElement.style.backgroundColor = "yellow";
currentLabelElement.style.color = "black";
currentBoxElement.style.borderColor = "yellow";
console.log("Fallback to yellow");
}
})
.catch((err) => {
console.error("Error sending image to API:", err);
// On error, fallback to yellow verifying
if (currentLabelElement && currentBoxElement) {
currentLabelElement.textContent = "Verifying...";
currentLabelElement.style.backgroundColor = "yellow";
currentLabelElement.style.color = "black";
currentBoxElement.style.borderColor = "yellow";
}
});
}
}
let isProcessing = false;
let previousTime;
const context = canvas.getContext("2d", { willReadFrequently: true });
function updateCanvas() {
const { width, height } = canvas;
context.drawImage(video, 0, 0, width, height);
if (!isProcessing) {
isProcessing = true;
(async function () {
const pixelData = context.getImageData(0, 0, width, height).data;
const image = new RawImage(pixelData, width, height, 4);
const inputs = await processor(image);
const { outputs } = await model(inputs);
overlay.innerHTML = "";
const sizes = inputs.reshaped_input_sizes[0].reverse();
outputs.tolist().forEach((x) => renderBox(x, sizes));
if (previousTime !== undefined) {
const fps = 1000 / (performance.now() - previousTime);
status.textContent = `FPS: ${fps.toFixed(2)}`;
}
previousTime = performance.now();
isProcessing = false;
})();
}
window.requestAnimationFrame(updateCanvas);
}
// Start the video stream
navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
video.srcObject = stream;
video.play();
const videoTrack = stream.getVideoTracks()[0];
const { width, height } = videoTrack.getSettings();
setStreamSize(width * scale, height * scale);
const ar = width / height;
const [cw, ch] = ar > 720 / 405 ? [720, 720 / ar] : [405 * ar, 405];
container.style.width = `${cw}px`;
container.style.height = `${ch}px`;
window.requestAnimationFrame(updateCanvas);
})
.catch((error) => {
alert(error);
});
|