DEMO地址:http://www.gbcphp.com/fly/myface/
FACE++的地址:http://www.faceplusplus.com.cn/
前记:前段时间,关于人脸识别的应用挺火的,比如检测年龄的啦,还有颜值计算的啦。
虽然这些东西都是昙花一现,但是在很多地方应用还是很广的,偶尔一些推广的活动可以结合这些趣味性的应用结合来做。
因为不知道是怎么实现的,这里涉及到太多的算法,自己开发一个人脸识别的算法不太理智,因为已经有人做出来的东西,如果不是非常必要,就不需要去自己写。
所以在网上找了一圈,这个face++算是比较理想的,有很多应用都是用了他们的SDK。
具体调用的方法face++上的文档已经有了,按照上面的步骤去做就行,这里不赘述。
使用方法:http://www.faceplusplus.com.cn/create-a-new-app/
使用API:http://www.faceplusplus.com.cn/api-overview/
主要写一下颜值计算这部分。
颜值计算这部分比较具有主观性,因为每个人的审美都不一样,所以只能采用一些标准,比如黄金比例,三庭五眼等标准。
我选择了三庭五眼这种普遍认为标准的计算方法。
具体如下:
/* 计算两点之间的 */
function distance(px1,py1,px2,py2){
return Math.sqrt(Math.abs(Math.pow(px2 - px1,2)) + Math.abs(Math.pow(py2 - py1,2)));
}
function comparePic(url2){
var face1,face2,results,smile = 0;
var api = new FacePP('ec7ca9298901a3725b050cd1c683e756', 'aG3Yo9DRXfys5Zh2a8yXoP8A3CqzyRE6');
api.request('detection/detect', {
url: url2
}, function(err, result) {
if (err) {
return;
}
face1 = result;
if(face1.face.length == 0){
yourscore.innerHTML = '不明物体';
return;
}
smile = parseInt(face1.face[0].attribute.smiling.value);
if(smile < 20){
smile = -10;
}else{
smile = parseInt(smile/10);
}
api.request('/detection/landmark', {
face_id: face1.face[0].face_id
}, function(err, result) {
if (err) {
return;
}
face2 = result;
//console.log(face2.result)
if(face2.result.length != 0){
var yourface = face2.result[0].landmark;
//计算两眉头间的距离
var c1 = distance(yourface.left_eyebrow_right_corner.x,yourface.left_eyebrow_right_corner.y,yourface.right_eyebrow_left_corner.x,yourface.right_eyebrow_left_corner.y);
//console.log('计算两眉头间的距离 = ' + c1);
//眉毛之间的中点坐标;
var c1_x = (yourface.right_eyebrow_left_corner.x - yourface.left_eyebrow_right_corner.x )/2 + yourface.left_eyebrow_right_corner.x,
c1_y = (yourface.right_eyebrow_left_corner.y - yourface.left_eyebrow_right_corner.y)/2 + yourface.left_eyebrow_right_corner.y;
//眉毛中点到鼻子最低处的距离
var c2 = distance(yourface.nose_contour_lower_middle.x,yourface.nose_contour_lower_middle.y,c1_x,c1_y);
//眼角之间的距离
//console.log('眼角之间的距离 = ' + c3);
var c3 = distance(yourface.left_eye_right_corner.x,yourface.left_eye_right_corner.y ,yourface.right_eye_left_corner.x,yourface.right_eye_left_corner.y);
//鼻子的宽度
var c4 = distance(yourface.nose_left.x,yourface.nose_left.y,yourface.nose_right.x,yourface.nose_right.y);
//脸的宽度
var c5 = distance(yourface.contour_left1.x,yourface.contour_left1.y,yourface.contour_right1.x,yourface.contour_right1.y);
//下巴到鼻子下方的高度
var c6 = distance(yourface.contour_chin.x,yourface.contour_chin.y,yourface.nose_contour_lower_middle.x,yourface.nose_contour_lower_middle.y);
//眼睛的大小
var c7_left = distance(yourface.left_eye_left_corner.x,yourface.left_eye_left_corner.y,yourface.left_eye_right_corner.x,yourface.left_eye_right_corner.y);
var c7_right = distance(yourface.right_eye_left_corner.x,yourface.right_eye_left_corner.y,yourface.right_eye_right_corner.x,yourface.right_eye_right_corner.y);
//嘴巴的大小
var c8 = distance(yourface.mouth_left_corner.x,yourface.mouth_left_corner.y,yourface.mouth_right_corner.x,yourface.mouth_right_corner.y);
//嘴巴处的face大小
var c9 = distance(yourface.contour_left6.x,yourface.contour_left6.y,yourface.contour_right6.x,yourface.contour_right6.y);
/* 开始计算步骤 */
var yourmark = 100,mustm = 0;
//眼角距离为脸宽的1/5,
mustm += Math.abs((c3/c5)*100 - 25);
//鼻子宽度为脸宽的1/5
mustm += Math.abs((c4/c5)*100 - 25);
//眼睛的宽度,应为同一水平脸部宽度的!/5
var eyepj = (c7_left+c7_right)/2;
mustm += Math.abs(eyepj/c5*100 - 25);
//理想嘴巴宽度应为同一脸部宽度的1/2
mustm += Math.abs((c8/c9)*100 - 50);
//下巴到鼻子下方的高度 == 眉毛中点到鼻子最低处的距离
mustm += Math.abs(c6 - c2);
yourscore.innerHTML = yourmark - parseInt(mustm) + smile;
}else{
yourscore.innerHTML = '不明物体';
}
});
});
}
上面这段代码就是按照标准去计算的颜值。
注意的地方就是,上传图片进行比较的时候,首先图片要处理一下,不能太大,太大了会失败。
其二,图片要使用服务器地址,如果是本地也会出错。