CoolFace
Apppublic

atlury/vision-test

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
utils.js34 linesDownload Raw Back to root
1// Utility function to convert an uploaded image to base64
2function getImageDataFromFile(file) {
3  return new Promise((resolve, reject) => {
4    const reader = new FileReader();
5    reader.onload = function(event) {
6      const img = new Image();
7      img.onload = () => {
8        const canvas = document.getElementById("image-canvas");
9        const ctx = canvas.getContext("2d");
10        canvas.width = img.width;
11        canvas.height = img.height;
12        ctx.drawImage(img, 0, 0);
13        const imageData = ctx.getImageData(0, 0, img.width, img.height);
14        resolve(imageData);
15      };
16      img.onerror = () => reject(new Error("Failed to load image"));
17      img.src = event.target.result;
18    };
19    reader.onerror = () => reject(new Error("Failed to read file"));
20    reader.readAsDataURL(file);
21  });
22}
23
24// Function to convert ImageData to base64
25async function imageFileToBase64(file) {
26  const imageData = await getImageDataFromFile(file);
27  const canvas = document.createElement("canvas");
28  const ctx = canvas.getContext("2d");
29  canvas.width = imageData.width;
30  canvas.height = imageData.height;
31  ctx.putImageData(imageData, 0, 0);
32  return canvas.toDataURL();
33}
34