随着人工智能和大数据分析技术越来越广泛,众多的生活场景都存在着这些技术的身影,比如像现在比较流行的人脸识别技术,其底层的算法实现的支撑,为众多的业务场景铺垫了基础,像支付宝的刷脸支付,本文是百度的人脸识别的微信小程序,后续出百度人脸识别的专题,下面开始项目的开发。

1.新建springboot项目

1.1首先引入百度人工智能的jar:

        <!-- 百度人工智能 --><dependency><groupId>com.baidu.aip</groupId><artifactId>java-sdk</artifactId><version>4.4.1</version></dependency>

1.2案例使用fastjson,引入fastjson文件:

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId></exclusion></exclusions></dependency><!-- fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency>

1.3获取所需的AppID、APIKey、SecretKey

访问网址:https://ai.baidu.com/,百度AI主页去注册一个百度账号,如果你有了百度云账号,可以直接进入到控制台里面去,创建一个应用,创建成功后,会分配给你一个应用信息,包含AppID ,API Key,Secret Key,这些是你调用人脸识别API的凭证,妥善保管

1.4新建REST风格的接口

package com.example.demo.controller;import com.alibaba.fastjson.JSONObject;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import lombok.var;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;@RestController
public class UserController {static AipFace client = null;static {client = new AipFace(AppID, APIKey, SecretKey);// 可选:设置网络连接参数client.setConnectionTimeoutInMillis(2000);client.setSocketTimeoutInMillis(60000);}@PostMapping(value = "/face")public String faceDistinguish(HttpServletRequest request, HttpServletResponse response) throws Exception{MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;//对应前端的upload的name参数"file"MultipartFile multipartFile = req.getFile("file");try {return detectFace(multipartFile.getBytes(), "1");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}/*** 人脸检测** @return* @throws IOException*/public String detectFace(byte[] arg0, String max_face_num) {try {HashMap<String, String> options = new HashMap<String, String>();options.put("face_field", "age,beauty,expression,emotion,gender,glasses,race,qualities");options.put("max_face_num", "2");options.put("face_type", "LIVE");// 图片数据String imgStr = Base64Util.encode(arg0);String imageType = "BASE64";var res = client.detect(imgStr, imageType, options);JSONObject Jsonobject = new JSONObject();if(res.getInt("error_code")==0) {//  成功Jsonobject.put("code",0);var result = res.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);//年龄Jsonobject.put("age", result.getInt("age"));//颜值Jsonobject.put("beauty", result.getDouble("beauty"));//表情//    none:不笑;smile:微笑;laugh:大笑Jsonobject.put("expression", result.getJSONObject("expression").getString("type"));//性别//male:男性 female:女性Jsonobject.put("gender", result.getJSONObject("gender").getString("type"));//情绪//angry:愤怒 disgust:厌恶 fear:恐惧 happy:高兴 sad:伤心 surprise:惊讶 neutral:无表情 pouty: 撅嘴 grimace:鬼脸Jsonobject.put("emotion", result.getJSONObject("emotion").getString("type"));//眼镜//none:无眼镜,common:普通眼镜,sun:墨镜if(result.getJSONObject("glasses").getString("type").equals("none")){Jsonobject.put("glasses", "无眼镜");}else if(result.getJSONObject("glasses").getString("type").equals("common")){Jsonobject.put("glasses", "普通眼镜");}else{Jsonobject.put("glasses", "墨镜");}}else{//失败Jsonobject.put("code",1);Jsonobject.put("message",res.getString("error_msg"));}System.out.println(Jsonobject.toJSONString());return Jsonobject.toJSONString();} catch (Exception e) {e.printStackTrace();}return null;}private static byte[] FileToByte(File file) throws IOException {// 将数据转为流InputStream content = new FileInputStream(file);ByteArrayOutputStream swapStream = new ByteArrayOutputStream();byte[] buff = new byte[100];int rc = 0;while ((rc = content.read(buff, 0, 100)) > 0) {swapStream.write(buff, 0, rc);}// 获得二进制数组return swapStream.toByteArray();}}

1.5百度人脸识别返回的数据

检测失败:

{"result": null,"log_id": 594351565552,"error_msg": "pic not has face","cached": 0,"error_code": 222202,"timestamp": 1581855369
}

检测成功:

{"result": {"face_num": 1,"face_list": [{"glasses": {"probability": 1,"type": "none"},"expression": {"probability": 0.78,"type": "smile"},"emotion": {"probability": 0.9,"type": "neutral"},"beauty": 71.92,"gender": {"probability": 1,"type": "female"},"race": {"probability": 1,"type": "yellow"},"angle": {"roll": -2.42,"pitch": 9.89,"yaw": -17.84},"face_token": "22c27c473034f97d6c9fd1e1739e62e5","location": {"top": 208.17,"left": 144.01,"rotation": -2,"width": 188,"height": 176},"face_probability": 1,"age": 17}]},"log_id": 6520125451589,"error_msg": "SUCCESS","cached": 0,"error_code": 0,"timestamp": 1581912291
}

2.微信小程序开发

2.1修改app.js

修改其中的navigationBarTitleText,navigationBarBackgroundColor两个属性:

{"pages": ["pages/index/index"],"window": {"backgroundTextStyle": "light","navigationBarBackgroundColor": "#c9d0d8","navigationBarTitleText": "人脸检测","navigationBarTextStyle": "black"},"sitemapLocation": "sitemap.json"
}

2.2index.wxml

<!--动画-->
<view class='animation' wx:if='{{animation}}'>
<view class='animation-list'></view>
</view>
<!--图片区域-->
<view class='pages'><view class='face'><image src='{{images}}' mode='widthFix'></image></view>
</view>
<!--识别结果-->
<view class='result'>
<text class='result-text'>识别结果</text>
</view><view wx:if='{{faceend}}'>
<view class='from'>
<text>性别</text>
<text>{{gender}}</text>
</view>
<view class='from'>
<text>年龄</text>
<text>{{age}}</text>
</view>
<view class='from'>
<text>表情</text>
<text>{{expression}}</text>
</view>
<view class='from'>
<text>颜值</text>
<text>{{beauty}}</text>
</view>
<view class='from'>
<text>情绪</text>
<text>{{emotion}}</text>
</view>
<view class='from'>
<text>眼镜</text>
<text>{{glasses}}</text>
</view>
</view><view wx:else>没有检查到人脸
</view><view class='btn'>
<button type='primary' bindtap='faceImage'>选择图片</button>
</view>

2.3index.js

//index.js
//获取应用实例
const app = getApp()Page({data: {images:[],animation:false,//性别gender:'',//年龄age:'',//表情expression:'',//颜值beauty:'',//情绪emotion:'',//眼镜glasses:'',faceend:true},faceImage:function(){wx.chooseImage({count:1,sizeType:['original','compressed'],sourceType:['album','camera'],success: (res) => {var temFilePaths = res.tempFilePathsconsole.log(temFilePaths)this.setData({images: temFilePaths,animation:true})//像后台发送图片//延时返回数据setTimeout(()=>{wx.uploadFile({url: 'http://localhost:9900/face',filePath: temFilePaths[0],name: 'file',success: (res) => {var data = res.dataconsole.log(data)var datas = JSON.parse(data);if (datas.code == 1) {this.setData({faceend: false,animation:false})}else {this.setData({//性别gender: datas.gender,//年龄age: datas.age,//表情expression: datas.expression,//颜值beauty: datas.beauty,//情绪emotion: datas.emotion,//眼镜glasses: datas.glasses,faceend: true,animation:false});}}})},4000)},})}
})

2.4index.wxss

.pages {background: #c9d0d8}
.face {background: white;height: 500rpx;margin: 0 35rpx;text-align: center;overflow: hidden;}
.face image {width: 500rpx;height:500rpx;}.result {background: white;padding: 25rpx;}
.result-text {font-size: 30rpx;color: #00a4ff}.from{display:flex;padding:23rpx 25rpx;}
.from text{font-size: 30rpx;margin-right: 20rpx;}
.from:nth-child(odd){background: #f5f5f5;}.btn {position: fixed;bottom:5rpx;width: 100%;}
.btn button {width: 450rpx;border-radius: 70rpx;}/* 动画 */
.animation{position: fixed;top: 0;left: 0;right: 0;height: 500rpx;}
.animation-list{width: 100%;height: 450rpx;background: linear-gradient(to bottom,rgba(216,179,255,0),rgba(216,179,255,1));position: relative;top: -600rpx;animation: myfist 2s linear 1s infinite alternate;}
/* 开始执行动画 */
@keyframes myfist {0%{background: linear-gradient(to bottom,rgba(216,179,255,0),rgba(216.179,255,1));left: 0;top: -600rpx;}25%{left: 0;top: 50rpx;}50%{left: 0;top: 50rpx;}75%{left: 0;top: 50rpx;}100%{left: 0;top: -600rpx;}
}

2.5系统界面图

源码下载地址https://download.csdn.net/download/u013938578/12163827

人工智能微信小程序人脸识别之人脸属性检测(附源码)相关推荐

  1. SSM基于微信小程序的魔音影评交流平台-计算机毕设 附源码61758

    SSM基于微信小程序的魔音影评交流平台 摘 要 随着人类向信息社会的不断迈进,风起云涌的信息时代正掀起一次新的革命,同时计算机网络技术高速发展,网络管理运用也变得越来越广泛.因此,建立一个基于微信小程 ...

  2. python小程序贪吃蛇_微信小程序实现的贪吃蛇游戏【附源码下载】

    本文实例讲述了微信小程序实现的贪吃蛇游戏.分享给大家供大家参考,具体如下: 先来看看运行效果: 具体代码如下: 界面布局 pages/snake/snake/snake.wxml: snake 得分 ...

  3. 基于微信小程序的校园信息共享平台 毕业设计-附源码211615

    校园信息共享平台 摘 要 随着信息技术的发展,各大高校已经建立了各自的部门信息系统,但是由于这些应用系统开发时间迥异,开发平台不同,开发技术差异化的原因,各个系统间缺乏关联,使得信息不能有效共享,逐渐 ...

  4. JAVA+基于微信小程序的校园信息共享平台 毕业设计-附源码211615

    校园信息共享平台 摘 要 随着信息技术的发展,各大高校已经建立了各自的部门信息系统,但是由于这些应用系统开发时间迥异,开发平台不同,开发技术差异化的原因,各个系统间缺乏关联,使得信息不能有效共享,逐渐 ...

  5. JAVA基于微信小程序的校园信息共享平台毕业设计-附源码211615

    摘 要 随着信息技术的发展,各大高校已经建立了各自的部门信息系统,但是由于这些应用系统开发时间迥异,开发平台不同,开发技术差异化的原因,各个系统间缺乏关联,使得信息不能有效共享,逐渐形成了" ...

  6. SSM基于微信小程序的外卖点餐系统 毕业设计-附源码2711704

    基于微信小程序的外卖点餐系统的设计与实现 摘要 立足于当下餐饮行业现有的点餐模式,分析传统APP点餐的运作流程,结合微信小程序的特点设计新型的外卖点餐系统.近几年,人们生活水平日益提升,但工作强度和压 ...

  7. 使用高德地图微信小程序SDK开发案例-输入提示(附源码)

    闲来无事写一篇使用高德地图的微信小程序SDK开发应用的实例. 接下来先看需求: 我们要做的是,根据用户输入的关键词,给出相应的提示信息,列表中显示地方的名称,地方的详细地址以及对应的经纬度坐标. 当然 ...

  8. 微信小程序的校园求职招聘系统uniapp 附源码

    随着现代网络通信技术越来越深入而广泛的应用,国内的招聘网站如雨春笋,各种各样的招聘网站映入眼帘.网络上五花八门的企业使得应聘者无从下手.企业在选择应聘者时也只能了解基本情况.针对这些情况,本网站做出相 ...

  9. 微信小程序-订单页面——可左右滑动(附源码)

    说明:头部有一个三种预约状态的切换 wxml页面: <!-- 切换栏 --> <view class="swiper-tab"><block wx:f ...

  10. PyQt5 + Python3.7 + OpenCV人脸识别身份认证系统(附源码)

    基于PyQt5 + Python3.7 + OpenCV实现的人脸识别身份认证系统,附源码. 技术选型 PyQt5 + Python3.7 + OpenCV 功能概述 实现人员注册,信息修改,人脸识别 ...

最新文章

  1. 工艺仿真软件_【技术简讯】电解抛光仿真软件Elsyca EPOS技术简介
  2. C# Marshal类基本概念和入门示例程序
  3. iOS 分类思想(2)
  4. PowerShell字体颜色修改
  5. C#7.0之ref locals and returns (局部变量和引用返回)
  6. 全国计算机vb考试经典程序设计,全国计算机二级《VB语言程序设计》考试要点...
  7. lambda python_Python | Lambda和filter()与示例
  8. HTML5-Tab标签
  9. 【工业4.0】什么是工业4.0,这篇文章讲得明明白白!
  10. js常用内建对象之:Math
  11. easyui-validatebox验证 radio
  12. nginx location配置直接输出文本
  13. C# 正则表达式验证数据类型
  14. 单点登录 cas 设置回调地址_单点登录终极方案之 CAS 应用及原理
  15. python与ffmepg结合进行ts文件的下载和合并,密钥文件解决方法
  16. 盛夏光年 - 江湖一剑客
  17. 照片生成3D虚拟数字人,虚拟形象主播搭建(软件+教程)
  18. PHPCMSV9 企业黄页 黄页企业模板添加方法
  19. 装系统计算机丢失msi,msi电脑一键重装系统win10详细步骤
  20. WaitForSingleObject -- setevent 讲解与编程示例

热门文章

  1. 5.4 数值分析: 牛顿插值多项式及余项
  2. 水电一体化预付费云平台
  3. mysql设置定时备份
  4. 银行借ApplePay反攻 七方利益分润未解
  5. Arm A-profile feature names
  6. 苏格拉底让弟子拾麦穗的故事
  7. delphi5安装教程
  8. hdu 4466 Triangle (数学)
  9. 最近入手kinect,准备搞搞体感开发
  10. 微信小程序志愿者服务的设计与实现