tracking.js example

inspired by computer vision question on so: https://stackoverflow.com/questions/30122658/how-can-centre-image-on-face

HTML

<script src="https://raw.githack.com/eduardolundgren/tracking.js/master/build/tracking.js"></script>
<script src="https://raw.githack.com/eduardolundgren/tracking.js/master/build/data/face.js"></script>
<div class="demo-title">
    <p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - hover image to see all faces detected</p>
</div>
<div class="demo-frame">
    <div class="demo-container"> <span id="photo"><img id="img" src="https://raw.githubusercontent.com/eduardolundgren/tracking.js/master/examples/assets/faces.jpg" /></span>

    </div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
    font-family: Helvetica, Arial, sans-serif;
}
.demo-title {
    position: absolute;
    width: 100%;
    background: #2e2f33;
    z-index: 2;
    padding: .7em 0;
}
.demo-title a {
    color: #fff;
    border-bottom: 1px dotted #a64ceb;
    text-decoration: none;
}
.demo-title p {
    color: #fff;
    text-align: center;
    text-transform: lowercase;
    font-size: 15px;
}
.demo-frame {
    background: url(frame.png) no-repeat;
    width: 854px;
    height: 658px;
    position: fixed;
    top: 50%;
    left: 50%;
    margin: -329px 0 0 -429px;
    padding: 95px 20px 45px 34px;
    overflow: hidden;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}
.demo-container {
    width: 100%;
    height: 530px;
    position: relative;
    background: #eee;
    overflow: hidden;
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
}
.dg.ac {
    z-index: 100 !important;
    top: 50px !important;
}
/* example's CSS */
 #photo:hover .rect {
    opacity: .75;
    transition: opacity .75s ease-out;
}
.rect:hover * {
    opacity: 1;
}
.rect {
    border-radius: 2px;
    border: 3px solid white;
    box-shadow: 0 16px 28px 0 rgba(0, 0, 0, 0.3);
    cursor: pointer;
    left: -1000px;
    opacity: 0;
    position: absolute;
    top: -1000px;
}
.arrow {
    border-bottom: 10px solid white;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    height: 0;
    width: 0;
    position: absolute;
    left: 50%;
    margin-left: -5px;
    bottom: -12px;
    opacity: 0;
}
input {
    border: 0px;
    bottom: -42px;
    color: #a64ceb;
    font-size: 15px;
    height: 30px;
    left: 50%;
    margin-left: -90px;
    opacity: 0;
    outline: none;
    position: absolute;
    text-align: center;
    width: 180px;
    transition: opacity .35s ease-out;
}
#img {
    position: absolute;
    top: 50%;
    left: 50%;
   ...

JavaScript

document.addEventListener("DOMContentLoaded", function () {
    var img = document.getElementById('img');
    var tracker = new tracking.ObjectTracker('face');
    tracking.track(img, tracker);
    tracker.on('track', function (event) {
        event.data.forEach(function (rect) {
            plotRectangle(rect.x, rect.y, rect.width, rect.height);
        });
    });
    var friends = ['Thomas Middleditch', 'Martin Starr', 'Zach Woods'];
    var plotRectangle = function (x, y, w, h) {
        var rect = document.createElement('div');
        var arrow = document.createElement('div');
        var input = document.createElement('input');
        input.value = friends.pop();
        rect.onclick = function name() {
            input.select();
        };
        arrow.classList.add('arrow');
        rect.classList.add('rect');
        rect.appendChild(input);
        rect.appendChild(arrow);
        rect.style.width = w + 'px';
        rect.style.height = h + 'px';
        rect.style.left = (img.offsetLeft + x) + 'px';
        rect.style.top = (img.offsetTop + y) + 'px';
		console.log("image at", { x:(img.offsetLeft + x), y:(img.offsetTop + y), width:w, height:h });
        document.getElementById('photo').appendChild(rect);
    };
});
// Create the event
var event = new CustomEvent("DOMContentLoaded", { "detail": "Content Loaded trigger" });

// Dispatch/Trigger/Fire the event
document.dispatchEvent(event);