CoolFace
Apppublic

dilums/object-detection

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
utils.ts34 linesDownload Raw Back to detect
1import sharp from "sharp";2 3export async function isImageValid(arrayBuffer: ArrayBuffer): Promise<boolean> {4  try {5    const metadata = await sharp(Buffer.from(arrayBuffer)).metadata();6    return metadata.format !== undefined;7  } catch (error) {8    return false;9  }10}11 12export async function getImageDimensions(13  arrayBuffer: ArrayBuffer14): Promise<{ width: number; height: number }> {15  const metadata = await sharp(Buffer.from(arrayBuffer)).metadata();16  return { width: metadata.width || 0, height: metadata.height || 0 };17}18 19export async function extractPart(20  arrayBuffer: ArrayBuffer,21  {22    x,23    y,24    width,25    height,26  }: { x: number; y: number; width: number; height: number }27) {28  const out = await sharp(arrayBuffer)29    .extract({ left: x, top: y, width, height })30    .toBuffer();31  const img = out.toString("base64");32  return img;33}34