Skip to content

kevinanielsen/tfjs-image-node

Repository files navigation

tfjs-image-node

Known Vulnerabilities npm version CI

A simple image classifier using tfjs, that can run in Node.js

Install

npm i tfjs-image-node
# or
pnpm add tfjs-image-node

Import

tfjs-image-node has two different exports. One for the tfjs-node platform and one for the regular tfjs package - one package is preferred for operations in the node runtime, the other is preferred for regular javascript.

const classifyImage = require("tfjs-image-node");
// or
import classifyImage from "tfjs-image-node";

Example

import classifyImage from "tfjs-image-node/node";

const model = "https://teachablemachine.withgoogle.com/models/jAIOHvmge";
const image = "https://www.stgeorges.nhs.uk/wp-content/uploads/2014/03/hand-2.jpeg";

// With tfjs-node as the platform
(async () => {
	const prediction = await classifyImage(model, image);
	console.log(prediction[0]);
})();

// With classic tfjs as the platform
(async () => {
	const prediction = await classifyImage(model, image, "classic");
	console.log(prediction[0]);
})();

// expected output:
// { label: 'Hand', probability: 0.9999574422836304 }

Parameters

Name Type Description
MODEL_URL string The URL to your AI model (currently only supports teachable machine URLs like "https://teachablemachine.withgoogle.com/models/{model_id}".
IMAGE_FILE_PATH string The file path or URL to the image you want classified.
PLATFORM "node" or "classic" (optional) Choose the platform to use for the computation of the prediction. If you want to use the tfjs-node platform, use "node" as the parameter, otherwise use "classic".
METADATA metadata.json (optional) If you want to specify a set of metadata for the model.