6***@qq.com
6***@qq.com
  • 发布:2019-08-31 16:44
  • 更新:2021-12-06 14:10
  • 阅读:26207

谈一下uniapp,自定义camera,来进行人脸识别的

分类:uni-app

本人,小白一个,大神可忽略本内容

由于uniapp,在打包成APP的时候不能自定义相机,很多人脸识别的项目,在APP里面只能使用原生,本人也不会原生,很是苦逼,很羡慕能开发原生的,但是也想搞一下人脸识别,迫于无奈,研究属于自己的一套,h5的人脸识别,喜欢的可以研究研究。

自定义相机启动界面

自定义摄像头界面大小,遮罩等

人脸放到框内,自动拍照,保存base64,也快手动拍照

识别到人脸以后,大家可以自行发挥了,如,人脸对比等...

<script>

    //激活摄像头  
    navigator.getUserMedia  = navigator.getUserMedia ||  
      navigator.webkitGetUserMedia ||  
      navigator.mozGetUserMedia ||  
      navigator.msGetUserMedia;  
    navigator.getUserMedia({video:true}, gotStream, noStream);  

    function gotStream(stream) {  
      myVideo.src = URL.createObjectURL(stream);  
      myVideo.onerror = function () {  
        stream.stop();  
      };  
      stream.onended = noStream;  
      myVideo.onloadedmetadata = function () {  
        console.log('摄像头成功打开!');  
      };  
    }  

    function noStream(err){  
        console.log(err);  
    }  

    //激活摄像头end  
    var saveArray = {};  
    window.onload = function () {  
        var video = document.getElementById('video');  
        var canvas = document.getElementById('canvas');  
        var context = canvas.getContext('2d');  

        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) {  
                context.strokeStyle = '#fff';  
                context.strokeRect(rect.x, rect.y, rect.width, rect.height);  
                //context.font = '11px Helvetica';   
                context.fillStyle = "#fff";  

                   context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);  
                   context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);  

                saveArray.x = rect.x;  
                saveArray.y = rect.y;  
                saveArray.width = rect.width;  
                saveArray.height = rect.height;  

            });  
        });  
        setInterval(function () {    
            //console.log(saveArray);  
            if (saveArray.x > 80 &&  
                    saveArray.x + saveArray.width < 260 &&  
                    saveArray.y > 90 &&  
                    saveArray.y + saveArray.height < 260 &&  
                    saveArray.width < 150  
                    && saveArray.height < 150) {  
                console.log(saveArray);  
                getPhoto();  
                for (var key in saveArray) {  
                    delete saveArray[key];  
                }  
            }   
        }, 2000);  
        function getPhoto() {  

            context2.drawImage(video, 100, 120, 260, 380, 0, 0, 170, 255); //将video对象内指定的区域捕捉绘制到画布上指定的区域,实现拍照。  

        }  

        var btn = document.getElementById("btn");  
        btn.onclick= function () {  
            getPhoto();  
        }  

        var canvas1 = document.getElementById('canvas1');  
        var context1 = canvas1.getContext('2d');  
        var can = document.getElementById('shortCut');  
        var context2 = can.getContext('2d');  
        context1.strokeStyle = "#69fff1";  
          context1.moveTo(63, 66); //上线 x,y  
          context1.lineTo(257, 66); //右  
          context1.lineTo(257, 321); //下  
          context1.lineTo(63, 321);//左  
          context1.lineTo(63, 66);//左  
        context1.stroke();  

        var img = document.getElementById("img")  

        function convertCanvasToImage(canvas) {  
            var image = new Image();  
            image.src = canvas.toDataURL("image/png");  
            return image;  
        }  

        var keepImg = document.getElementById("keepImg");  
        keepImg.onclick = function () {  
            var photoImg = document.createElement("img");  
            photoImg.src = convertCanvasToImage(can).src;  
            img.appendChild(photoImg);  

            console.log(convertCanvasToImage(can).src);  
        }  

    };  

</script>  
1 关注 分享
z***@126.com

要回复文章请先登录注册

善妒的蝎子

善妒的蝎子

回复 智密科技 :
搞定了 谢谢
2021-12-06 14:10
智密科技

智密科技

回复 善妒的蝎子 :
你搞定了吗 如果没搞定 可以进Q群755910061 我们有对应的插件
2021-12-04 11:54
智密科技

智密科技

我们有对应的插件,有需要的话 可以进群沟通
2021-12-04 11:53
善妒的蝎子

善妒的蝎子

大哥 可以借鉴一下你的源码吗?有什么联系方式吗
2021-11-06 14:37
9***@qq.com

9***@qq.com

怎么弄的大佬
2021-08-19 09:41
1***@qq.com

1***@qq.com

大佬,能分享下完整源码吗
2021-04-02 11:49
z***@qq.com

z***@qq.com

zhichi
2021-03-13 16:47
2***@qq.com

2***@qq.com

```
// 激活摄像头
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
navigator.getUserMedia({ video: true }, gotStream, noStream);
function gotStream(stream) {
myVideo.src = URL.createObjectURL(stream);
myVideo.onerror = function () {
stream.stop();
};
stream.onended = noStream;
myVideo.onloadedmetadata = function () {
console.log('摄像头成功打开!');
};
}
function noStream(err) {
console.log(err);
}
//激活摄像头end
var saveArray = {};
window.onload = function () {
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
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) {
context.strokeStyle = '#fff';
context.strokeRect(rect.x, rect.y, rect.width, rect.height);
context.font = '11px Helvetica';
context.fillStyle = "#fff";
context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11);
context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22);
saveArray.x = rect.x; saveArray.y = rect.y; saveArray.width = rect.width; saveArray.height = rect.height;
});
});
setInterval(function () {
console.log(saveArray);
// if (saveArray.x > 80 && saveArray.x + saveArray.width < 90 && saveArray.y + saveArray.height < 90) {

// }
})
}
```
2020-09-07 22:24
1***@163.com

1***@163.com

有没有源码呢
2020-07-01 15:26
6***@qq.com

6***@qq.com

作者方便分享一下源码吗?希望能够学习一下
2020-05-19 23:58