Machine Learning: TypeScript Libraries for Browser and Node.js

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Machine Learning in TypeScript: Overview of Key Libraries



TypeScript — a statically typed superset of JavaScript — is increasingly used in projects related to machine learning (ML). Thanks to strict typing, improved code readability, and excellent IDE support, TypeScript allows you to create reliable ML applications that run both in the browser and on the server (Node.js). In this article, we will review the main libraries for machine learning in TypeScript, their capabilities, and provide practical examples.



1. TensorFlow.js — The Flagship of Machine Learning in TypeScript



TensorFlow.js is a port of the popular TensorFlow framework for JavaScript and TypeScript. It allows you to run machine learning models directly in the browser or on Node.js without server infrastructure. The library supports all major ML stages: loading pre-trained models, training new models, inference (prediction), and even fine-tuning.



Installation:

npm install @tensorflow/tfjsnpm install @tensorflow/tfjs-node  # for Node.js (with acceleration)


Example: Image classification with a pre-trained MobileNet model

import * as tf from '@tensorflow/tfjs';import { loadGraphModel } from '@tensorflow/tfjs-converter';

async function classifyImage(imageElement: HTMLImageElement) { // Load the pre-trained MobileNet model const model = await loadGraphModel( 'https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v2_100_224/classification/3/default/1' );

// Convert the image to a tensor const tensor = tf.browser.fromPixels(imageElement) .resizeNearestNeighbor([224, 224]) .toFloat() .expandDims();

// Get the prediction const prediction = model.predict(tensor) as tf.Tensor; const topK = tf.topk(prediction, 3); const indices = await topK.indices.data(); const values = await topK.values.data();

console.log('Top-3 predictions:', indices, values);}

// Usageconst img = document.getElementById('myImage') as HTMLImageElement;classifyImage(img);


TensorFlow.js is ideal for tasks in computer vision, text processing (NLP), and time series. In TypeScript, you get autocompletion for all methods, which significantly speeds up development.



2. ML5.js — A Friendly Interface for Creative ML



ML5.js is built on top of TensorFlow.js but offers a simpler and higher-level API. This library is aimed at artists, designers, and developers who want to quickly add ML functionality without diving deep into the mathematics. ML5.js supports tasks such as image classification, object detection, text generation, sound analysis, and much more.



Installation:

npm install ml5


Example: Object detection using COCO-SSD

import ml5 from 'ml5';

async function detectObjects(videoElement: HTMLVideoElement) { // Load the object detection model const objectDetector = await ml5.objectDetector('cocossd');

// Perform detection on the video const results = await objectDetector.detect(videoElement);

results.forEach((obj: any) => { console.log(`Object: ${obj.label}, confidence: ${obj.confidence.toFixed(2)}`); console.log(`Coordinates: x=${obj.x}, y=${obj.y}, width=${obj.width}, height=${obj.height}`); });}

// Usage with webcamconst video = document.getElementById('webcam') as HTMLVideoElement;navigator.mediaDevices.getUserMedia({ video: true }) .then(stream => { video.srcObject = stream; }) .then(() => detectObjects(video));


ML5.js is great for prototyping, educational projects, and interactive installations. In TypeScript, you can define types for detection results, which makes

Recommendations