You can edit the below JavaScript code to customize the image tool.
/**
* Identifies potential subjects in an image, which can help infer a cartoon title.
* This function uses a pre-trained machine learning model (MobileNet) to classify
* the contents of the image. Note: As this is a general-purpose model trained on
* real-world objects, its ability to identify specific cartoon characters or titles
* may be limited, but it will attempt to identify recognizable subjects.
*
* @param {Image} originalImg The original image object to be processed.
* @param {number} topK The number of top predictions to display.
* @returns {Promise<HTMLCanvasElement>} A canvas element with the original image and the top predictions written on it.
*/
async function processImage(originalImg, topK = 3) {
// Create a canvas to draw on
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions to match the image
canvas.width = originalImg.naturalWidth || originalImg.width;
canvas.height = originalImg.naturalHeight || originalImg.height;
// Draw the original image onto the canvas
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
// Helper function to display messages on the canvas
const showMessage = (message, bgColor = 'rgba(0, 0, 0, 0.7)') => {
ctx.fillStyle = bgColor;
const padding = 20;
const textMetrics = ctx.measureText(message);
const rectHeight = 60;
const rectY = canvas.height / 2 - rectHeight / 2;
ctx.fillRect(0, rectY, canvas.width, rectHeight);
ctx.fillStyle = 'white';
ctx.font = `bold 20px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(message, canvas.width / 2, canvas.height / 2, canvas.width - padding);
};
// Helper function to dynamically load a script and return a promise
const loadScript = (url) => {
return new Promise((resolve, reject) => {
// Check if script is already loaded
if (document.querySelector(`script[src="${url}"]`)) {
return resolve();
}
const script = document.createElement('script');
script.src = url;
script.onload = () => resolve();
script.onerror = () => reject(new Error(`Failed to load script: ${url}`));
document.head.appendChild(script);
});
};
// Show a loading message while we load the model and process
showMessage('Analyzing image, please wait...');
try {
// Dynamically load TensorFlow.js and the MobileNet model
await loadScript('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js');
await loadScript('https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@latest/dist/mobilenet.min.js');
// Ensure the models are loaded and available on the window object
if (!window.mobilenet || !window.tf) {
throw new Error('Machine learning models could not be loaded.');
}
// Load the MobileNet model
const model = await window.mobilenet.load();
// Classify the image
const predictions = await model.classify(originalImg, topK);
// Clear the loading message by redrawing the original image
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
// Display the predictions on the canvas
if (predictions && predictions.length > 0) {
const boxHeight = (predictions.length * 28) + 20;
const boxY = canvas.height - boxHeight;
ctx.fillStyle = 'rgba(0, 0, 0, 0.75)';
ctx.fillRect(0, boxY, canvas.width, boxHeight);
ctx.fillStyle = 'white';
ctx.font = `18px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif`;
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
predictions.forEach((prediction, index) => {
const probability = (prediction.probability * 100).toFixed(2);
// Split class names that can be long (e.g., "comic book, comic")
const className = prediction.className.split(',')[0];
const text = `${index + 1}. ${className} (${probability}%)`;
ctx.fillText(text, 15, boxY + 12 + (index * 28));
});
} else {
showMessage('Could not identify any subjects.');
}
} catch (error) {
console.error('Error during image processing:', error);
// Redraw the original image to clear the loading message
ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
// Show a user-friendly error message on the canvas
showMessage('Failed to analyze image. Please check your network connection.', 'rgba(200, 0, 0, 0.8)');
}
return canvas;
}
Free Image Tool Creator
Can't find the image tool you're looking for? Create one based on your own needs now!
The Image Cartoon Title Identifier tool analyzes images to identify potential subjects, which may help infer cartoon titles. It employs a machine learning model to classify recognizable objects within the image, attempting to provide relevant predictions. Users can utilize this tool for various purposes, such as identifying cartoon characters in images, enhancing entertainment content, or aiding content creators in recognizing and referencing cartoons and characters based on visual input.