首先需要去face++官网注册账号
地址 https://www.faceplusplus.com.cn/
创建一个应用拿到api_key和api_secret
文档地址 https://console.faceplusplus.com.cn/documents/7776484
这一段我集成在微信中使用的image_url 可按需求结合上一篇自己改为客户端上传的
$img = $this->weObj->getValue('PicUrl');
$params['image_url'] = $img;
//查找face++的配置
$facecon = Db::name('face')->where('id',1)->find();
$api_key = $facecon['appkey'];
$api_secret = $facecon['appsecret'];
$server = "https://api-cn.faceplusplus.com/facepp/v3/detect";
$params['api_key'] = $api_key;
$params['api_secret'] = $api_secret;
$params['return_attributes'] = 'gender,age,headpose,blur,emotion,beauty,eyegaze,skinstatus';
$result = $this->http_post($server,$params);
// Db::name('log')->insert(['admin_name'=>$result]);
// 获取成功解json
if($result){
$info = json_decode($result,true);
//性别//颜值
if($info['faces']['0']['attributes']['gender'] == 'Male'){
$sex = '男';
$beauty = $info['faces']['0']['attributes']['beauty']['female_score'];
}else{
$sex = '女';
$beauty = $info['faces']['0']['attributes']['beauty']['male_score'];
}
//年龄
$age = $info['faces']['0']['attributes']['age']['value'];
//健康
$health = $info['faces']['0']['attributes']['skinstatus']['health'];
//色斑
$stain = $info['faces']['0']['attributes']['skinstatus']['stain'];
//青舂痘
$acne = $info['faces']['0']['attributes']['skinstatus']['acne'];
//黑眼圈
$dark_circle = $info['faces']['0']['attributes']['skinstatus']['dark_circle'];
//心情
$emotion['sadness'] = $info['faces']['0']['attributes']['emotion']['sadness'];
$emotion['neutral'] = $info['faces']['0']['attributes']['emotion']['neutral'];
$emotion['disgust'] = $info['faces']['0']['attributes']['emotion']['disgust'];
$emotion['anger'] = $info['faces']['0']['attributes']['emotion']['anger'];
$emotion['surprise'] = $info['faces']['0']['attributes']['emotion']['surprise'];
$emotion['fear'] = $info['faces']['0']['attributes']['emotion']['fear'];
$emotion['happiness'] = $info['faces']['0']['attributes']['emotion']['happiness'];
$key = array_search(max($emotion),$emotion);
//由于face++只给出了心情的英文自己转中文
if($key == 'anger'){
$xinqing = '愤怒';
}else if($key == 'disgust'){
$xinqing = 'disgust';
}else if($key == 'fear'){
$xinqing = '恐惧';
}else if($key == 'happiness'){
$xinqing = '高兴';
}else if($key == 'neutral'){
$xinqing = '平静';
}else if($key == 'sadness'){
$xinqing = '伤心';
}else if($key == 'surprise'){
$xinqing = '惊讶';
}
//拼接字符串
$aa = "性别:".$sex."\n"."年龄:".$age."\n"."颜值:".$beauty."\n"."健康:".$health."\n"."色斑:".$stain."\n"."青舂痘:".$acne."\n"."黑眼圈:".$dark_circle."\n"."心情:".$xinqing;
$this->weObj->text($aa);
}else{
$aa = "图片格式:JPG(JPEG),PNG;图片像素尺寸:最小48*48像素,最大4096*4096像素图片文件大小:2 MB;最小人脸像素尺寸:系统能够检测到的人脸框为一个正方形,正方形边长的最小值为图像短边长度的48分之一,最小值不低于48像素。例如图片为4096*3200像素,则最小人脸像素尺寸为 66*66 像素。";
$this->weObj->text($aa);
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
protected function http_post($url,$param,$post_file=false){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach($param as $key=>$val){
$aPOST[] = $key."=".urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST,true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
大神轻喷!!!