首先要在 face++ 注册一个账号,并且创建一个应用,拿到 api key 和 api secret;

下载 java 接入工具,一个 jar 包:https://github.com/FacePlusPlus/facepp-java-sdk

请求返回的数据是 json 格式,需要导入 json 解析包:

commons-beanutils-1.8.3.jar
commons-lang-2.5.jar
commons-collections-3.2.2.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar

图片上传类:FileUpLoad.java

 package com.krry;

 import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; /**
* FileUpLoad
* @author krry
*
*/
@WebServlet(name="fileUp",urlPatterns={"/show"})
public class FileUpLoad extends HttpServlet{ private static final long serialVersionUID = 1L; @Override
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
//判断是不是文件上传,根据请求包里面的内容是否为二进制提交的上传的文件 true/false
boolean isMultPart = ServletFileUpload.isMultipartContent(request);
if(isMultPart){
//创建文件上传工厂
FileOutputStream out = null;
InputStream in = null;
try {
FileItemFactory factory = new DiskFileItemFactory();
//创建容器文件类,负责处理上传的文件数据
ServletFileUpload upload = new ServletFileUpload(factory);
//解析上传文件,其实就是解析表单传过来的数据
List<FileItem> fileItems = upload.parseRequest(request);
//只需要拿一张图片
in = fileItems.get(0).getInputStream();
//获取源文件名
String Ofilename = fileItems.get(0).getName();
//拿到后缀名
String ext = Ofilename.substring(Ofilename.indexOf("."), Ofilename.length());
//拿到一个目录,绝对地址
String path =request.getSession().getServletContext().getRealPath("/");
//生成一个唯一的名字
String fileName = UUID.randomUUID().toString() + ext;
//定义上传目录
String dirPath = path+"/upload";
File dirFile = new File(dirPath);
//如果此文件夹不存在
if(!dirFile.exists()){
dirFile.mkdirs();//创建此文件夹
}
//配置文件路径
String filePath = path+"/upload/"+fileName;
//输出流
out = new FileOutputStream(filePath);
//边读取边编写 字节流
byte[] b = new byte[1024];
int length = 0;
while((length = in.read(b)) != -1){ //读到字节数组里面去
out.write(b,0,length); //从0开始写,写你读了多少
}
//放在request作用域里
request.setAttribute("imgSrc", filePath);
request.setAttribute("imgSrcPage", "upload/"+fileName);
//请求转发,转发到另一个servlet
request.getRequestDispatcher("showImage").forward(request, response);
} catch (Exception e) { e.printStackTrace();
}finally{
out.close();
in.close();
}
}
} }

人脸识别类:Analysis.java

这里返回一个 json 字符串,放到作用域,在前台获取

 package com.krry;

 import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap; import javax.net.ssl.SSLException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; /**
* analysis
* @author krry
* String 转json 需要的jar包:版本不能弄错
* commons-beanutils-1.8.3.jar
commons-lang-2.5.jar
commons-collections-3.2.2.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar
*/
@WebServlet(name="analysis",urlPatterns={"/showImage"})
public class Analysis extends HttpServlet{ private static final long serialVersionUID = 1L; @Override
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{ //获取上传的图片地址
String imgPath = request.getAttribute("imgSrc").toString();
File file = new File(imgPath);
byte[] buff = getBytesFromFile(file);
String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
HashMap<String, String> map = new HashMap<String, String>();
HashMap<String, byte[]> byteMap = new HashMap<String, byte[]>();
map.put("api_key", "15N9eBF_ieE-N2nvI7oK_A9anh2meuUS");
map.put("api_secret", "cBnvsIAo8VaOj53lXCWlpuOSd0NrsFif");
map.put("return_landmark", "1");
map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
byteMap.put("image_file", buff);
try{
byte[] bacd = post(url, map, byteMap);
String str = new String(bacd); JSONObject json = JSONObject.fromObject(str);
JSONArray faces = json.getJSONArray("faces"); //解析失败
if(str.indexOf("error_message") != -1 || faces.size() == 0){
request.getSession().setAttribute("error", "解析失败,请重新选择");
//重定向到首页
response.sendRedirect("index.jsp");
}else{
//将人脸信息字符串str信息放到作用域
request.setAttribute("face", str);
//请求转发,返回到页面
request.getRequestDispatcher("showImage.jsp").forward(request, response);
}
}catch (Exception e) {
e.printStackTrace();
} } private final static int CONNECT_TIME_OUT = 30000;
private final static int READ_OUT_TIME = 50000;
private static String boundaryString = getBoundary(); protected static byte[] post(String url, HashMap<String, String> map, HashMap<String, byte[]> fileMap) throws Exception {
HttpURLConnection conne;
URL url1 = new URL(url);
conne = (HttpURLConnection) url1.openConnection();
conne.setDoOutput(true);
conne.setUseCaches(false);
conne.setRequestMethod("POST");
conne.setConnectTimeout(CONNECT_TIME_OUT);
conne.setReadTimeout(READ_OUT_TIME);
conne.setRequestProperty("accept", "*/*");
conne.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);
conne.setRequestProperty("connection", "Keep-Alive");
conne.setRequestProperty("user-agent", "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)");
DataOutputStream obos = new DataOutputStream(conne.getOutputStream());
Iterator iter = map.entrySet().iterator();
while(iter.hasNext()){
Map.Entry<String, String> entry = (Map.Entry) iter.next();
String key = entry.getKey();
String value = entry.getValue();
obos.writeBytes("--" + boundaryString + "\r\n");
obos.writeBytes("Content-Disposition: form-data; name=\"" + key
+ "\"\r\n");
obos.writeBytes("\r\n");
obos.writeBytes(value + "\r\n");
}
if(fileMap != null && fileMap.size() > 0){
Iterator fileIter = fileMap.entrySet().iterator();
while(fileIter.hasNext()){
Map.Entry<String, byte[]> fileEntry = (Map.Entry<String, byte[]>) fileIter.next();
obos.writeBytes("--" + boundaryString + "\r\n");
obos.writeBytes("Content-Disposition: form-data; name=\"" + fileEntry.getKey()
+ "\"; filename=\"" + encode(" ") + "\"\r\n");
obos.writeBytes("\r\n");
obos.write(fileEntry.getValue());
obos.writeBytes("\r\n");
}
}
obos.writeBytes("--" + boundaryString + "--" + "\r\n");
obos.writeBytes("\r\n");
obos.flush();
obos.close();
InputStream ins = null;
int code = conne.getResponseCode();
try{
if(code == 200){
ins = conne.getInputStream();
}else{
ins = conne.getErrorStream();
}
}catch (SSLException e){
e.printStackTrace();
return new byte[0];
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buff = new byte[4096];
int len;
while((len = ins.read(buff)) != -1){
baos.write(buff, 0, len);
}
byte[] bytes = baos.toByteArray();
ins.close();
return bytes;
}
private static String getBoundary() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for(int i = 0; i < 32; ++i) {
sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".charAt(random.nextInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_".length())));
}
return sb.toString();
}
private static String encode(String value) throws Exception{
return URLEncoder.encode(value, "UTF-8");
} public static byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
}
return null;
} }

解析 json 字符串,先转换成 json 格式,json 数组,一个个获取人脸属性

 <%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%@ page language="java" import="java.io.File" pageEncoding="utf-8"%>
<%@page import="java.math.BigDecimal"%>
<%@ page import="java.util.*" %>
<% String path = request.getContextPath();
int port = request.getServerPort();
String basePath = null;
if(port==80){
basePath = request.getScheme()+"://"+request.getServerName()+path;
}else{
basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
}
pageContext.setAttribute("basePath", basePath); Object str = request.getAttribute("face");
JSONObject json = JSONObject.fromObject(str);
JSONArray faces = json.getJSONArray("faces");
StringBuffer strBuff = new StringBuffer();
//识别出人脸的个数
int length = faces.size(); //识别出多少个人脸,就是循环多少次
for(int i = 0;i < length;i++){ JSONObject face = faces.getJSONObject(i); //拿到第i+1个人脸部信息
JSONObject attribute = face.getJSONObject("attributes");//拿到脸部属性 //年龄
JSONObject age = attribute.getJSONObject("age");
int ageValue = age.getInt("value"); //性别
JSONObject gender = attribute.getJSONObject("gender");
String sex = gender.getString("value");
if(sex.equals("Male")) sex = "男";
else sex = "女"; //人种
JSONObject ethnicity = attribute.getJSONObject("ethnicity");
String races = ethnicity.getString("value");
if(races.equals("Asian")) races = "亚洲人";
else if(races.equals("White")) races = "白人";
else races = "黑人"; //微笑程度
JSONObject smile = attribute.getJSONObject("smile");
String smileRange = String.format("%.2f",smile.getDouble("value")); //表情
JSONObject emotion = attribute.getJSONObject("emotion");
Map<String,Double> mapp = new TreeMap<String,Double>();
//装配表情信息到map
double sadness = emotion.getDouble("sadness");
mapp.put("伤心", sadness);
double neutral = emotion.getDouble("neutral");
mapp.put("平静", neutral);
double disgust = emotion.getDouble("disgust");
mapp.put("厌恶", disgust);
double anger = emotion.getDouble("anger");
mapp.put("愤怒", anger);
double happiness = emotion.getDouble("happiness");
mapp.put("高兴", happiness);
double surprise = emotion.getDouble("surprise");
mapp.put("惊讶", surprise);
double fear = emotion.getDouble("fear");
mapp.put("恐惧", fear); //利用list取最大值
List<Double> listmap = new ArrayList<Double>();
for(String key:mapp.keySet()){
listmap.add(mapp.get(key));
}
//取到最大值
double valueMax = Collections.max(listmap);
//根据map的value获取map的key
String emotionMax = "";
for (Map.Entry<String, Double> entry : mapp.entrySet()) {
if(valueMax == entry.getValue()){
emotionMax = entry.getKey();
}
} //颜值分数
JSONObject beauty = attribute.getJSONObject("beauty");
String beautys = "";
if(sex.equals("男")) beautys = String.format("%.2f",beauty.getDouble("male_score"));
else beautys = String.format("%.2f",beauty.getDouble("female_score")); //面部状态
JSONObject skinstatus = attribute.getJSONObject("skinstatus");
Map<String,String> mapSkin = new TreeMap<String,String>();
//装配信息到map
double health = skinstatus.getDouble("health");
mapSkin.put("健康指数", String.format("%.2f",health));
double stain = skinstatus.getDouble("stain");
mapSkin.put("色斑指数", String.format("%.2f",stain));
double acne = skinstatus.getDouble("acne");
mapSkin.put("青春痘指数", String.format("%.2f",acne));
double dark_circle = skinstatus.getDouble("dark_circle");
mapSkin.put("黑眼圈指数", String.format("%.2f",dark_circle)); strBuff.append("<br>");
strBuff.append("<span>年龄:").append(ageValue).append("岁</span>");
strBuff.append("<span>性别:").append(sex).append("</span>");
strBuff.append("<br>").append("<br>");
strBuff.append("<span>人种:").append(races+"</span>");
strBuff.append("<span>微笑程度:").append(smileRange+"%</span>");
strBuff.append("<br>").append("<br>");
strBuff.append("<span>表情:").append(emotionMax+"</span>");
strBuff.append("<span>颜值分数:").append(beautys+"</span>");
strBuff.append("<br>").append("<br>");
int counts = 0;
for(String key:mapSkin.keySet()){
counts++;
strBuff.append("<span>"+key+":").append(mapSkin.get(key)+"</span>");
if(counts % 2 == 0) strBuff.append("<br>").append("<br>");
} strBuff.append("<br>"); } %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title> Java开发人脸特征识别系统 --krry</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <style type="text/css">
*{padding:0px;margin:0px;}
body{background: #FFF;background:url("images/78788.jpg");background-size:cover;}
.a_btn{text-decoration:none;font-family:"幼圆";background:#fff;transition:.6s;color:#f60;cursor:pointer;width:100px;height:40px;border-radius:10px;display: inline-block;text-align:center;line-height:40px;}
.aa{position: absolute;left: 48px;top: 31px;}
.a_btn:hover{background:red;color:#fff;transition:.6s;}
.clear{clear:both;}
.title{margin-top: 25px;font-size:48px;font-family:"幼圆";color: #ff7272;text-shadow: 1px 1px 3px #ff3737;text-align:center;line-height:45px;}
.content{margin:0 auto;width:900px;}
.content .img{float:left;margin-top:30px;}
.content .img img{border-radius:10px;}
.content .describe{margin-top:30px;width:500px;color:#000;font-family:"幼圆";font-size:18px;float:right;}
.content .describe span{width: 190px;display: inline-block;}
</style> </head> <body>
<div class="aa"><a class="a_btn" href="${basePath}">返回</a></div>
<div class="title">人脸识别结果</div>
<div class="content">
<div class = "img">
<img style="float:left" width="350" src="${imgSrcPage}" />
</div>
<div style="float:right" class="describe"><%=strBuff.toString() %></div>
<div class="clear"></div>
</div>
</body>
</html>

首页:

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

 <%

     Object ermsg = request.getSession().getAttribute("error");
StringBuffer strBuff = new StringBuffer();
if(ermsg != null){
//清除作用域
request.getSession().removeAttribute("error");
strBuff.append("loading('"+ermsg+"',4)");
}
%> <!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="keywords" content="关键词,关键词">
<meta name="description" content="">
<link rel="stylesheet" type="text/css" href="css/sg.css" />
<link rel="stylesheet" type="text/css" href="css/sg/css/animate.css" />
<link rel="stylesheet" type="text/css" href="css/sg/css/sg.css" /> <title> Java开发人脸特征识别系统 --krry</title>
<style type="text/css">
*{padding:0px;margin:0px;}
body{background:url("images/5.jpg");background-size:cover;font-size:18px;font-family:"微软雅黑";color:#666;}
img{border:none;}
h1{font-size:24px;}
.title{margin-top: 30px;font-size:48px;font-family:"幼圆";color: #ff7272;text-shadow: 1px 1px 3px #ff3737;text-align:center;line-height:45px;}
.box{margin:100px auto;text-align:center;}
.box .b_btn{width:100px;height:40px;background:#fff;transition:.6s;font-size:16px;font-family:"微软雅黑";display:block;margin:54px auto;text-align:center;
cursor:pointer;line-height:40px;text-decoration:none;color:#F60;border:1px;border-radius:12px}
.box .b_btn:hover{background:red;color:#fff;transition:.6s;}
.busbu{top:0;background:rgba(0, 0, 0, 0.2);display:none;width:100%;height:100%;position:absolute;z-index:1000;}
</style>
</head> <body>
<!--action 提交的路径 method:post(打包传输) get(明文传输) enctype(采用二进制)-->
<form action="show" method="post" enctype="multipart/form-data">
<div class="box">
<div class="title">人脸特征识别系统 --krry</div>
<input type="file" id="fileChoice" accept="image/jpeg,image/png" style="display:none" name="files"/>
<a class="b_btn" href="javascript:void(0);" onclick="choice()">选择照片</a>
<input type="button" value="提交解析" class="b_btn" id="but"/>
<input type="submit" class="sub_btn" style="display:none;"/>
</div>
</form>
<div class="busbu"></div>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/sg.js"></script>
<script src="js/sgutil.js"></script>
<script type="text/javascript" src="css/sg/tz_util.js"></script>
<script type="text/javascript" src="css/sg/sg.js"></script>
<script type="text/javascript">
<%=strBuff.toString() %>
//选择照片
function choice(){
//js 弱类型 快速定位
var choose = document.getElementById("fileChoice");
choose.click();
}
$("#but").click(function(){
if($("#fileChoice").val()){//如果选择了照片
$(".sub_btn").click();
$(".busbu").show();
loading("正在解析",900);//再展示
}else{
$.tmDialog.alert({open:"top",content:"请先选择一张照片",title:"Krry温馨提醒"});
}
}); </script>
</body> </html>

需要注意的点,json 字符串的转换和解析,获取 json 中人脸属性的方法

git:https://github.com/Krryxa/face

java 开发 face++ 人脸特征识别系统的更多相关文章

  1. 分享:Java 开发精美艺术二维码

    博客地址:https://ainyi.com/58 Java 开发精美艺术二维码 看到网络上各种各样的二维码层出不穷,好像很炫酷的样子,一时兴起,我也要制作这种炫酷二维码效果 例如: 根据以往例子 根 ...

  2. Java开发前期准备工作

    配置Java开发环境变量 在"系统变量"中设置3项属性,JAVA_HOME, PATH, CLASSPATH. 变量设置参数如下: 变量名:JAVA_HOME 变量值:C:\Pro ...

  3. Win7 32位系统下Java开发环境的安装及配置

    目录: Java JDK安装. Java JDK系统环境的配置. 配置常见问题的解决. Java JDK的安装 Java Jdk(Java Development Kit)Java开发不可缺少的环境, ...

  4. Java开发笔记(八十一)如何使用系统自带的注解

    之前介绍继承的时候,提到对于子类而言,父类的普通方法可以重写也可以不重写,但是父类的抽象方法是必须重写的,如果不重写,编译器就直接在子类名称那里显示红叉报错.例如,以前演示抽象类用法之时,曾经把Chi ...

  5. Nodejs开发人脸识别系统-教你实现高大上的人工智能

    Nodejs开发人脸识别系统-教你实现高大上的人工智能   一.缘起缘生 前段时间有个H5很火,上传个头像就可以显示自己穿军装的样子,无意中看到了一篇帖子叫 全民刷军装背后的AI技术及简单实现 ,里面 ...

  6. Ubuntu系统Java开发环境的搭建

    操作系统:Linux x64 / Ubuntu 14.04 Java JDK版本:jdk-8u65-linux-x64.tar.gz 声明:转载请注明出处及本文链接 本文原文地址:Ubuntu系统Ja ...

  7. 基于java开发的在线题库系统tamguo

    简介 探果网(简称tamguo)是基于java开发的在线题库系统,包括 在线访问 后台运营 会员中心 书籍中心 管理员账号:system 密码:123456 因为线上数据和测试数据没有做到隔离,作者已 ...

  8. Windows系统下JAVA开发环境搭建

    首先我们需要下载JDK(JAVA Development Kit),JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库. 下载地址:http://www.or ...

  9. SSM开发基于Java EE在线图书销售系统

           SSM(Spring+Spring MVC+MyBatis)开发基于Java EE在线图书销售系统  网站成功建立和运行很大部分取决于网站开发前的规划,因此为了在网站建立过程中避免一些不 ...

随机推荐

  1. Conemu, Msys2 工具整合,提升windows下控制台工作效率

    与windows cmd相比较git-bash这类的console工具好用很多,但是git-bash的命令和功能相对简单,功能扩展起来不方便,git-bash本身也是基于msys的. 昨天发现使用Ms ...

  2. win10+anaconda+cuda配置dlib,使用GPU对dlib的深度学习算法进行加速(以人脸检测为例)

    在计算机视觉和机器学习方向有一个特别好用但是比较低调的库,也就是dlib,与opencv相比其包含了很多最新的算法,尤其是深度学习方面的,因此很有必要学习一下.恰好最近换了一台笔记本,内含一块GTX1 ...

  3. Sphinx下载、安装、配置、Hello World、文档阅读

    sphinx下载.安装.配置.Hello World.查看文档

  4. Windows下使用Sublime text3快速编辑Linux文件,写Shell

    所需要配合的工具是WinSCP 添加完毕之后直接在目录下双击要编辑的shell脚本文件,即可弹出Sublime Text的编辑器 然后咱通过Putty看看Linux虚拟机上的文件有没有发生变化

  5. python语言基础语法笔记<note2--面向对象编程>

    Python面向对象编程(OOP) 一.面向对象过程的优点特征: 封装 模型的特征和能力打包在一起 模型的改变由模型自身完成 隐藏模型的细节,外界只能使用,不能改变 继承 符合自然界分类规律 快速实现 ...

  6. APP性能测试(CPU)

    获取数据 :adb shell dumpsys cpuinfo | grep packagename result = os.popen("adb shell dumpsys cpuinfo ...

  7. Davinci DM6446开发攻略——linux-2.6.18移植

     TI DAVINCI 使用最新的内核是montavista linux-2.6.18,之前说过,国内很多公司,包括开发板的软件包,一直在使用montavista linux-2.6.10,这个版本准 ...

  8. [javascript]一段焦点图的js代码

    <html> <head> <meta name="name" content="content"charset="ut ...

  9. C#数据缓存介绍及Caching通用帮助类整理

    C#缓存主要是为了提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容量数据时,使用缓存来直接为客户端服务,可以减少客户端与服务器端的数据交互,从而大大提高程序的性能. 以下为 ...

  10. R语言实现二分查找法

    二分查找时间复杂度O(h)=O(log2n),具备非常高的效率,用R处理数据时有时候需要用到二分查找法以便快速定位 Rbisect <- function(lst, value){ low=1 ...