liveBook · Manning

Machine learning

As explained in Web Components in Action

Machine learning is a field in which computational models learn patterns from data to perform tasks such as classification or prediction.

Machine learning trains a model against right and wrong examples so it can recognize patterns and perform tasks such as prediction. A hand-tracking model, for example, learns to recognize images of hands. Training may happen in Python with TensorFlow, while the trained model runs in a browser through TensorFlow.js. section 15.4

A browser-based hand-tracking feature can be assembled by:

  • Loading a Handtrack.js model with parameters such as horizontal flipping, detection limits, overlap thresholds, and prediction confidence. section 15.4
  • Passing video frames to the model and receiving hand-location predictions. section 15.4
  • Hiding the model and tracking logic behind a Web Component that exposes hand locations to the page. section 15.4

Hand tracking for a Web Harp

A hand-tracking component can sit over a Web Harp interface, use the camera and display canvas, and report detected points through an event. The harp can then respond to hand movement without exposing the underlying model or TensorFlow dependencies. section 15.4

Listing 15.20. Hand-tracking Web Component
import Video from './video.js';
export default class HandTracker extends Video {                           #1
    static get HAND_LOCATION() { return 'onHandLocation'; }
    constructor() {
        super();
        const modelParams = {
            flipHorizontal: true, // flip e.g for video
            maxNumBoxes: 20,      // maximum number of boxes to detect
            iouThreshold: 0.5,    // ioU threshold for non-max suppression
            scoreThreshold: 0.6,  // confidence threshold for predictions.
        };
        handTrack.load(modelParams)                                        #2
           .then(lmodel => {
                this._model = lmodel;
        });
    }

    runDetection() {
        if (!this._model) { return; }
        this._model.detect(this.dom.video).then(predictions => {
            const pts = [];
            for (let c = 0; c < predictions.length; c++) {
                const centerpoint = {};
                centerpoint.x = (predictions[c].bbox[0] +
                  (predictions[c].bbox[2] / 2));
                centerpoint.y = (predictions[c].bbox[1] +
                  (predictions[c].bbox[3] / 2));
                pts.push(centerpoint);

            }
            this._model.renderPredictions(predictions, this.dom.canvas,
                this.canvasContext, this.dom.video);

            const ce = new CustomEvent( HandTracker.HAND_LOCATION,
                { detail: { points: pts }, bubbles: true, composed: true });
            this.dispatchEvent(ce);                                        #3
        });
    }
    init() {                                                               #4
        super.init();
        handTrack.startVideo(this.dom.video).then((status) => {
            this.onResize();
            console.log(this.visibleVideoRect)
            if (status) { this.runDetection();  }
        });

    }
    getCurrentFrameData(mode, noredraw) {                                  #5
        this.runDetection();
    }
}
if (!customElements.get('wcia-handtracker')) {
    customElements.define('wcia-handtracker', HandTracker);
}

Inspect the component that loads the model, runs detection on video, and turns predictions into hand-location data. section 15.4

How Web Components in Action covers it

How deep it goes
Defines training and browser inference, then stops short of detailed model internals.
Examples
Uses hand-image recognition, a camera-based hand tracker, and a Web Harp integration.
What you can do after
Apply it via TensorFlow.js and Handtrack.js to add browser-based hand tracking behind a custom Web Component.
How it connects
Connects machine learning with computer vision, motion tracking, TensorFlow.js, Handtrack.js, and Web Components.

Also explained in

Compare how 42 books explain machine learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.