使用Canvas和Traking.js进行视频裁剪

时间:2018-09-25 13:11:51

标签: jquery html5-canvas html5-video tracking face-detection

下午好。我在tracking.js上方使用<video>来检测人脸。

https://i.stack.imgur.com/O3dwh.png

现在,我正在尝试仅裁剪和抓取该正方形的内容,但是在裁剪时,我的脸部图像位于正方形中,但未被检测到。

我的下面的代码:

<style>

    video {     
        width: 100%;
        height: auto;
    }
    .box{
        border:1px solid red;
        position: absolute;
        z-index:99999;
    }
</style>
<script src="<?php url(); ?>/assets/build/tracking-min.js"></script>
<script src="<?php url(); ?>/assets/build/data/face-min.js"></script>
<div class="demo-frame">
                                        <div class="demo-container">
                                          <div class='box' id='box'></div>                                      
                                          <video id="video" preload autoplay loop muted></video>                                          
                                        </div>
                                    </div>                                  
                                    <center>&nbsp;<br><button type="button" value="Snap It" onClick="shoot()" class='btn btn-lg btn-warning'><i class="fa fa-camera" aria-hidden="true"></i></button></center><br>
<div id="results" id="contentimg"><img id="base64image" src="<?php url(); ?>/img/nopicface.jpg" class="embed-responsive-item"/></div>


<script language="JavaScript">
    var xG,yG,wG,hG = 0;
    window.onload = function() {



        var video = document.getElementById('video');   

        var tracker = new tracking.ObjectTracker('face');
        tracker.setInitialScale(4);
        tracker.setStepSize(2);
        tracker.setEdgesDensity(0.1);
        tracking.track('#video', tracker, { camera: true });
        tracker.on('track', function(event) {
                //context.clearRect(0, 0, canvas.width, canvas.height);
                event.data.forEach(function(rect) {               
                  //console.log('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
                  //console.log('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
                  $(".box").css('margin-left',rect.x);
                  $(".box").css('margin-top',rect.y);
                  $(".box").css('height',rect.height);
                  $(".box").css('width',rect.width);

                  xG = rect.x;
                  yG = rect.y;
                  hG = rect.height;
                  wG = rect.width;
                  //console.log(rect.width+ " | " +rect.height);


              });
            });

    };

function capture(video, scaleFactor) {
    if(scaleFactor == null){
        scaleFactor = 1;
    }

    var w = video.videoWidth * scaleFactor;
    var h = video.videoHeight * scaleFactor;

    var box  = document.getElementById('box'); 

    var canvas = document.createElement('canvas');
    canvas.width  = wG;
    canvas.height = hG;
    var ctx = canvas.getContext('2d');
        ctx.drawImage(video, xG, yG, wG, hG,0,0, wG, hG);
    return canvas;
} 

function shoot(){

    var video  = document.getElementById('video'); 
    var img  = document.getElementById('base64image');    
    var canvas = capture(video, 1);  

    img.src = canvas.toDataURL();

}


</script>  

the result of this needs to be something similar to this here.

另外,我注意到每个监视器的裁剪都不同。 我已经尝试通过将方形设置为我的脸来进行手动校正,但是当我单击以进行裁剪时,它是不正确的。

0 个答案:

没有答案