Skip to content

Instantly share code, notes, and snippets.

@lynnaloo
Created November 17, 2021 15:50
Show Gist options
  • Save lynnaloo/2bac083b97c4d3eb99c06cf39d0aaf2e to your computer and use it in GitHub Desktop.
Save lynnaloo/2bac083b97c4d3eb99c06cf39d0aaf2e to your computer and use it in GitHub Desktop.
Canvas + Custom Vision
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const { createCanvas, loadImage } = require('canvas');
const fs = require('fs');
const rawImage = req.body.image;
const w = rawImage.width;
const h = rawImage.height;
console.log(w, h);
const predictions = req.body.predictions.filter(e => e.probability > MIN_CONFIDENCE);
console.log(predictions);
// Confidence threshold for the analysis
const MIN_CONFIDENCE = 0.75;
const canvas = createCanvas(w, h);
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, w, h);
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
for (const e of predictions) {
const left = e.boundingBox.left * w;
const top = e.boundingBox.top * h;
const width = e.boundingBox.width * w;
const height = e.boundingBox.height * h;
ctx.strokeRect(left, top, width, height);
};
const buffer = await canvas.toBuffer("image/png");
// send back bounded image
context.res = {
status: 200,
body: buffer
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment