昨天好多人在@微信官方,求一顶帽子:
最后知道真相的你,眼泪掉下来。
这次就用程序来实现一个自动带圣诞帽的头像。
准备:
头像+圣诞帽(png类型)
2.新建目录
npm init
npm install jquery.facedetection
3.新建face.html,加入如下代码:
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>圣诞帽</title>
</head>
<body>
<imgsrc="./face.jpg"alt="face"id="j-face">
<imgsrc="./maozi.png"alt="maozi"id="j-maozi">
<scriptsrc="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<scriptsrc="./node_modules/jquery.facedetection/dist/jquery.facedetection.js"></script>
<script>
let ratio=1.5;
$('#j-face').faceDetection({
complete:function(faces){
let maozi=document.querySelector('#j-maozi');
let{width,height,positionX,positionY}=faces[0];
maozi.style.cssText=`position:absolute;width:${width*ratio}px;height:${height*ratio}px;left:${positionX-width/3}px;top:${positionY-height/2}px`;
}
});
</script>
</body>
</html>
4.结果
原理:
1.利用计算机视觉(Computer Vision),识别出脸,给出坐标以及脸的大小等数据
2.设置圣诞帽的位置