HTML5 Zone is brought to you in partnership with:

As the founder and software architect of subzin.com, Luis drives the development of subzin search. Luis is a DZone MVB and is not an employee of DZone and has posted 3 posts at DZone. You can read more from them at their website. View Full User Profile

Track a Moving Ball in Real Time using HTML5 and JavaScript

02.19.2012
| 11859 views |
  • submit to reddit

This is an experiment for a real-time tracking of a football match using only web technologies. In this case I’ve chosen to track a football match from Futbol Club Barcelona (fcb).

All the work is made by the browser and it’s interactive (there are four checkboxes to activate/deactivate each tracker). It is based on tracking the location of colored objects in the frames using the HSV colour space.

As a practical use for the tracker, I’ve added a smart volume feature that activates the sound only when there are goal chances (every time the ball is near the area). So you can browse other pages and change to the video one only when chances are happening.

It works based on the quantity of green color in the sides of the video.

The path tracker shows ‘possible’ passes between fcb players

If you want to watch it in action here is the demo

So far it only works in chrome and firefox and because of the low resolution of the video (and my limited skills), the tracking is not perfect, but it could be improved for tracking videos from external sources like Youtube, Veetle, etc.

Here is the code:

function rgbToHsv(r, g, b){
    r = r/255, g = g/255, b = b/255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, v = max;

    var d = max - min;
    s = max == 0 ? 0 : d / max;

    if(max == min){
        h = 0; // achromatic
    }else{
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
                case g: h = (b - r) / d + 2; break;
                    case b: h = (r - g) / d + 4; break;
                        }
        h /= 6;
    }

    return [h, s, v];
}

//Main object
var processor = {
    isFirefox35: function() {
        // Let a chance to the navigator to deal with it:
        return true; var ua = navigator.userAgent;
        // Gecko ?
        if (ua.indexOf("Gecko") == -1)
            return false;
        // Geck >= 1.9.1 ?
        return !(ua.indexOf("rv:1.9.1") == -1 && ua.indexOf("rv:1.9.2") == -1);
    },
    // Init
    doLoad: function() {
        if (!this.isFirefox35()) {
            document.getElementById("nofirefoxbeta").style.display = "block"; }
        // Some init
        this.displayBackground = true;
        this.video = document.getElementById("video");
        this.mirrorVideo = document.getElementById("mirrorVideo");
        this.mirrorVideoCtx = this.mirrorVideo.getContext("2d");
        var self = this;
        // If the videos end, play again
        this.video.addEventListener("ended", function(){
            try {
                clearTimeout(self.timeout);
            } catch(e){}
            self.video.play();
            // Work around: https://bugzilla.mozilla.org/show_bug.cgi?id=488287
            self.videoIsPlaying();
        }, true);
        // Set the events listeners for the main video (update button)
        this.video.addEventListener("pause", function() { self.updateButtons(false); }, false);
        this.pageLoaded = true;
        this.startPlayer();
    },
    videoIsPlaying: function() {
        this.updateButtons(true);
        this.timerCallback();
    },
    videoIsReady: function() {
        this.videoLoaded = true;
        this.startPlayer();
    },
    startPlayer: function() {
        if (!this.videoLoaded || !this.pageLoaded) return;
            document.getElementById("wait").style.display = "none";
        document.getElementById("player").style.display = "block";
        this.width = this.video.videoWidth;
        this.height = this.video.videoHeight;
        this.mirrorVideo.width = this.width;
        this.mirrorVideo.height = this.height;
    },
    playVideo:function(){
        this.video.play();
        this.videoIsPlaying();
    },
    stopVideo: function(){
        this.video.pause();
        clearTimeout(this.timeout);
    },
    volumeDown:function(){
        if(this.video.volume>0)
            this.video.volume=Math.round((this.video.volume - 0.1)*10)/10;
    },
    volumeUp:function(){
        if(this.video.volume<1)
            this.video.volume=Math.round((this.video.volume + 0.1)*10)/10;
    },
    // Main loop
    timerCallback: function() {
        if (this.video.paused || this.video.ended) { return; }
        this.computeFrame();
        var self = this;
        this.timeout = setTimeout(function () {
            self.timerCallback(); }, 50);
    },
    // Update the SVG button
    updateButtons: function(play) {
        document.getElementById("playButton").setAttribute("play", play);
        document.getElementById("stopButton").setAttribute("play", play);
    }, // Handling some patterns (text, drawing)
    has_green_around: function(frameData, pos) {
        pos_left = pos+ 24;
        pos_right = pos - 24;
        r_left = frameData[pos_left+0];
        g_left = frameData[pos_left+1];
        b_left = frameData[pos_left+2];
        r_right = frameData[pos_right+0];
        g_right = frameData[pos_right+1];
        b_right = frameData[pos_right+2];
        return ((r_left < 125 && g_left > 125 && b_left < 80) &&
                (r_right < 125 && g_right > 125 && b_right < 80));

    },
    is_team_color: function(frameData, pos){
        for (i=pos-4; i<=pos+4; i+=4){
            r = frameData[i+0];
            g = frameData[i+1];
            b = frameData[i+2];
            hsv = rgbToHsv(r,g,b);
            //if(hsv[0] < 0.60|| hsv[1] < 0.35)
            if((hsv[0] < 0.40 || hsv[1] < 0.25 || hsv[2] < 0) ||
               (hsv[0] > 0.70 || hsv[1] > 1 || hsv[2] > 1))
                return false;
        }
        return true;
    },
    is_ball_color: function(frameData, pos){
        for (i=pos-4; i<=pos+4; i+=4){
            r = frameData[i+0];
            g = frameData[i+1];
            b = frameData[i+2];
            hsv = rgbToHsv(r,g,b);
            if((hsv[0] < 0.23 || hsv[1] < 0.40 || hsv[2] < 0.90) ||
               (hsv[0] > 0.27 || hsv[1] > 0.50 || hsv[2] > 1))
                return false;
        }
        return true;
    },
    dist: function(x1, y1, x2, y2) {
        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); },
    computeFrame: function() {
        try {
            this.mirrorVideoCtx.clearRect(0, 0, this.width, this.height);
            this.mirrorVideoCtx.drawImage(this.video, 0, 0, this.width, this.height);
        }
        catch(e) { return; }
        var frame = this.mirrorVideoCtx.getImageData(0, 0, 640, 360);
        this.mirrorVideoCtx.fillStyle = 'rgba(200,200,200,0.5)';
        this.mirrorVideoCtx.strokeStyle = 'rgba(200,200,200,0.5)';
        this.mirrorVideoCtx.lineWidth = 1;
        var x, y;
        var weight = 0;
        var team_shape = null;
        var ball_shape = null;
        var ball_found = false;
        var ball_tracker = document.getElementById("balltracker").checked;
        var team_tracker = document.getElementById("teamtracker").checked;
        var path_tracker = document.getElementById("pathtracker").checked;
        var smart_volume = document.getElementById("smartvolume").checked;
        var shapes = [];
        var x, y;
        var green_bar_left = 0;
        var green_bar_rigth =0;
        var green_bar_down = 0;
        var frame_length = frame.data.length/4;
        // We dont' need to compute each pixels
        var step = 4;
        for (var i = 0; i < frame_length; i += step) {
            pos = i*4;
            x = i % this.width;
            y = Math.round(i / this.width);
            if (x == 4)
                green_bar_left += g;
            else
                if (x == this.width-4) {
                    green_bar_rigth += g;
                    if (y == 4)
                        green_bar_down += g;
                }
            ball_found = ball_tracker && this.is_ball_color(frame.data, pos);
            team_found = team_tracker && this.is_team_color(frame.data, pos);
            if (this.has_green_around(frame.data, pos) && (ball_found || team_found)){
                if (ball_found) {
                    ball_shape = {};
                    ball_shape.x = x;
                    ball_shape.y = y;
                }
                if(team_found)
                {
                    if (!team_shape) {
                        // no shape yet, create the first one
                        team_shape = {};
                        team_shape.x = x;
                        team_shape.y = y;
                        team_shape.weight = 1;
                        shapes.push(team_shape);
                    } else {
                        var d = this.dist(x, y, team_shape.x, team_shape.y);
                        if (d>25){
                            team_shape = {};
                            team_shape.x = x;
                            team_shape.y = y;
                            team_shape.weight = 1;
                            shapes.push(team_shape);
                        }

                    }
                }
            }
        }
        if (shapes.length>0)
        {
            if (path_tracker){
                this.mirrorVideoCtx.beginPath();
                this.mirrorVideoCtx.moveTo(shapes[0].x, shapes[0].y);
            }
            for( var s=0; s<shapes.length; s+=1){
                tracker_size = shapes[s].weight+10;
                this.mirrorVideoCtx.strokeRect(shapes[s].x-tracker_size/2,
                shapes[s].y-tracker_size/2, tracker_size+2, tracker_size+2);
                if (path_tracker)
                    this.mirrorVideoCtx.lineTo(shapes[s].x, shapes[s].y);
            }
            if (path_tracker){
                this.mirrorVideoCtx.closePath();
                this.mirrorVideoCtx.stroke();
            }
        }
        if (ball_shape) {
            this.mirrorVideoCtx.strokeStyle = "red";
            this.mirrorVideoCtx.strokeRect(ball_shape.x-5, ball_shape.y-5, 10, 10);
        }

        if(smart_volume)
        {
            green_right_average = Math.round(green_bar_rigth / this.height);
            green_left_average = Math.round(green_bar_left / this.height);

            if ( shapes.length>0 &&
                Math.abs(green_left_average-green_right_average)>15){
                //this.volumeUp();
                this.video.volume=1;
                document.getElementById("soundIcon").setAttribute("mute", false);
            }else{
                this.volumeDown();
                //this.video.volume=0;
                document.getElementById("soundIcon").setAttribute("mute", true);
            }
        }
        return;
            }
}

 

Source: http://lusob.com/2012/02/tracking-a-football-match-with-html5-and-javascript/

Published at DZone with permission of Luis Sobrecueva, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)