Please bookmark this page to avoid losing your image tool!

Image Product Identifier

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
/**
 * Identifies products in an image using a pre-trained machine learning model (COCO-SSD).
 * This function dynamically loads TensorFlow.js and the model from a CDN,
 * so the first run may be slow. Subsequent runs will be much faster.
 * It then draws the original image on a canvas and overlays bounding boxes and
 * labels for each identified product.
 *
 * @param {HTMLImageElement} originalImg The original image element to process.
 * @param {number} scoreThreshold The minimum confidence score (0-1) for a detection to be shown. Default is 0.5.
 * @param {number} maxResults The maximum number of identified products to display. Default is 10.
 * @returns {Promise<HTMLCanvasElement>} A promise that resolves to a canvas element with the identified products highlighted.
 */
async function processImage(originalImg, scoreThreshold = 0.5, maxResults = 10) {

    // Helper function to dynamically load a script and return a promise
    const loadScript = (url) => new Promise((resolve, reject) => {
        const script = document.createElement('script');
        script.src = url;
        script.crossOrigin = 'anonymous';
        script.onload = resolve;
        script.onerror = (err) => reject(new Error(`Script load error for ${url}: ${err}`));
        document.head.appendChild(script);
    });

    // URLs for the required machine learning libraries from a CDN
    const TFJS_URL = 'https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.11.0/dist/tf.min.js';
    const COCOSSD_URL = 'https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd@2.2.2/dist/coco-ssd.min.js';

    // Create the output canvas and get its 2D rendering context
    const canvas = document.createElement('canvas');
    canvas.width = originalImg.naturalWidth;
    canvas.height = originalImg.naturalHeight;
    const ctx = canvas.getContext('2d');
    
    // Draw the original image onto the canvas to start
    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);

    // Helper function to display messages (like "Loading" or "Error") on the canvas
    const showMessage = (message, color = 'white') => {
        ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = color;
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
        ctx.font = '20px Arial';
        ctx.fillText(message, canvas.width / 2, canvas.height / 2);
    };
    
    showMessage('Loading AI model...');

    try {
        // Load TensorFlow.js and the COCO-SSD model scripts if they aren't already available
        if (typeof cocoSsd === 'undefined') {
            await loadScript(TFJS_URL);
            await loadScript(COCOSSD_URL);
        }
        
        // Load the model. We cache the model promise on the window object to prevent
        // reloading it on subsequent calls.
        if (!window.cocoSsdModelPromise) {
            window.cocoSsdModelPromise = cocoSsd.load();
        }
        
        showMessage('Analyzing image...');
        const model = await window.cocoSsdModelPromise;

        // Perform object detection
        const predictions = await model.detect(originalImg);
        
        // Clear the canvas and redraw the original image to remove the message overlay
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
        
        // Filter the predictions based on the score threshold and max results
        const filteredPredictions = predictions
            .filter(p => p.score >= scoreThreshold)
            .slice(0, maxResults);

        if (filteredPredictions.length === 0) {
           showMessage('No products identified with high confidence.', 'yellow');
        } else {
            // Draw bounding boxes and labels for each valid detection
            filteredPredictions.forEach(prediction => {
                const [x, y, width, height] = prediction.bbox;
                const label = `${prediction.class} (${Math.round(prediction.score * 100)}%)`;
                
                // Bounding box
                ctx.strokeStyle = '#00FF00';
                ctx.lineWidth = 3;
                ctx.strokeRect(x, y, width, height);
                
                // Text background
                ctx.font = '18px Arial';
                const textWidth = ctx.measureText(label).width;
                const textHeight = 22;
                ctx.fillStyle = '#00FF00';
                ctx.fillRect(x, y, textWidth + 10, textHeight);
                
                // Text label
                ctx._fillStyle = '#000000';
                ctx.fillStyle = 'black';
                ctx.textBaseline = 'top';
                ctx.fillText(label, x + 5, y + 2);
            });
        }

    } catch (error) {
        console.error("Error during product identification:", error);
        showMessage('Error: Could not analyze the image.', 'red');
    }

    // Return the final canvas with the results
    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

The Image Product Identifier tool utilizes advanced machine learning to identify and label products within an uploaded image. By loading the COCO-SSD model, it can accurately detect various objects and overlay bounding boxes with labels indicating the class and confidence of each identified product. This tool is ideal for users looking to analyze images for specific products, such as e-commerce businesses wanting to automate inventory management, educational purposes for understanding object detection, or any scenario where product recognition in images is beneficial.

Leave a Reply

Your email address will not be published. Required fields are marked *