You can edit the below JavaScript code to customize the image tool.
Apply Changes
async function processImage(originalImg, boxColor = "#FF0000", fontSize = 24, boxLineWidth = 4) {
// Create and setup the canvas
const canvas = document.createElement('canvas');
canvas.width = originalImg.width;
canvas.height = originalImg.height;
const ctx = canvas.getContext('2d');
// Draw the original image onto the canvas
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
// Make sure numeric parameters are handled correctly as numbers
const parsedFontSize = Number(fontSize) || 24;
const parsedLineWidth = Number(boxLineWidth) || 4;
// Helper function to dynamically load external scripts
const loadScript = (src, globalVar) => {
return new Promise((resolve, reject) => {
if (window[globalVar]) {
resolve(); // Script already loaded
return;
}
const script = document.createElement('script');
script.src = src;
script.onload = () => resolve();
script.onerror = () => reject(new Error(`Failed to load script: ${src}`));
document.head.appendChild(script);
});
};
try {
// Visual indicator that the model is processing/loading
ctx.fillStyle = "rgba(0, 0, 0, 0.6)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FFFFFF";
ctx.font = `${parsedFontSize}px Arial`;
ctx.fillText("Загрузка модели (Loading model)...", 20, parsedFontSize + 20);
// Dynamically load TensorFlow.js and the pre-trained COCO-SSD object detection model
await loadScript('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs', 'tf');
await loadScript('https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd', 'cocoSsd');
// Reload original image to clear the loading overlay
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
// Load the COCO-SSD model
const model = await window.cocoSsd.load();
// Run object detection on the canvas
const predictions = await model.detect(canvas);
// Filter the predictions for the 'bus' class
const buses = predictions.filter(prediction => prediction.class === 'bus');
if (buses.length === 0) {
// No bus found message
ctx.fillStyle = boxColor;
ctx.font = `bold ${parsedFontSize}px Arial`;
ctx.fillText("Автобус не найден (No bus found)", 20, parsedFontSize + 20);
return canvas;
}
// Draw bounding boxes for each detected bus
buses.forEach(bus => {
const [x, y, width, height] = bus.bbox;
// Draw the bounding box
ctx.beginPath();
ctx.lineWidth = parsedLineWidth;
ctx.strokeStyle = boxColor;
ctx.rect(x, y, width, height);
ctx.stroke();
// Prepare label text (Автобус = Bus in Russian)
const certainty = Math.round(bus.score * 100);
const text = `Автобус ${certainty}%`;
ctx.font = `bold ${parsedFontSize}px Arial`;
const textWidth = ctx.measureText(text).width;
// Calculate label coordinates to prevent it from going off-canvas
let labelBgY = y - (parsedFontSize + 8);
let textY = y - 6;
if (labelBgY < 0) {
// If off the top edge, draw label exactly inside the top of the box
labelBgY = y;
textY = y + parsedFontSize;
}
// Draw label background
ctx.fillStyle = boxColor;
ctx.fillRect(x, labelBgY, textWidth + 8, parsedFontSize + 8);
// Draw label text
ctx.fillStyle = "#FFFFFF";
ctx.fillText(text, x + 4, textY);
});
} catch (error) {
// Error handling fallback
console.error(error);
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#FF0000";
ctx.font = `bold ${parsedFontSize}px Arial`;
ctx.fillText("Ошибка загрузки модели (Model load error).", 20, parsedFontSize + 20);
}
return canvas;
}
Apply Changes