CoolFace
Apppublic

perborgen/javascript-object-detector

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
utils.js50 linesDownload Raw Back to root
1function generateRandomColor(){2    let maxVal = 0xFFFFFF; // 167772153    let randomNumber = Math.random() * maxVal; 4    randomNumber = Math.floor(randomNumber);5    randomNumber = randomNumber.toString(16);6    let randColor = randomNumber.padStart(6, 0);   7    return `#${randColor.toUpperCase()}`8}9 10 11function getScaledCoordinates(box, img) {12    // Get the original dimensions of the image13    const originalWidth = img.naturalWidth;14    const originalHeight = img.naturalHeight;15 16    // Get the scaled dimensions of the image17    const scaledWidth = img.offsetWidth; // The image should be exactly 400px wide18    const scaledHeight = img.offsetHeight;19 20    // Calculate the scaling factor21    const scale = scaledWidth / originalWidth;22 23    // Scale the box boundaries according to the scaled image24    let ymax = box.ymax * scale;25    let xmax = box.xmax * scale;26    let ymin = box.ymin * scale;27    let xmin = box.xmin * scale;28    29    // Make sure the minimum values are are at least 0 and the maximum values30    // are less than the width/height of the image31    ymin = Math.max(0, box.ymin * scale) 32    xmin = Math.max(0, box.xmin * scale) 33    ymax = Math.min(scaledHeight, ymax)34    xmax = Math.min(scaledWidth, xmax)35    36    return {37        ymin,38        xmin,39        ymax,40        xmax41    }42}43 44 45function removeElements(className) {46    const HTMLcollection = document.getElementsByClassName(className)47    Array.from(HTMLcollection).forEach(element => element.remove())48}49 50export { generateRandomColor, removeElements, getScaledCoordinates }