Face++与Java简单应用
2017-01-11 00:32
295 查看
第一次用face++的感受
前几天看视频学习,看见了这个好东西,之前是那种只知道人脸识别技术但是根本没去了解是怎么实现的…刚好趁着最近看看这是怎么个识别法。(其实只是用了他们的几个简单的方法,底层实现的真是厉害,采集数据不断学习不断分析…)
刚开始搜了一个网站大致了解了是个怎么的流程,这里我再自己梳理一下:
首先先去face++官网看,右上方点击这个返回旧站
点击开发者中心—>API可以看到许多接口方法,点击开发工具下载Java SDK —>下载下来是压缩包解压即可
解压后这个文件夹里面可都是好东西,Demo里面是他的一个实例,FaceppSDK里面有个output文件则是测试必须要用的jar包
废话不多说了大晚上的也挺困的…直接上图了,我用的是eclipse for j2ee,工具不同这不是问题都差不多 由于用的是SpringMVC,配置啊什么的我就挑主要的发了
配置文件如下:springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置注解的映射器--> <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>--> <!--配置注解的适配器--> <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>--> <!--注解扫描,代替注解的映射器和适配器--> <mvc:annotation-driven> </mvc:annotation-driven> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--配置视图的前缀和后缀--> <property name="prefix" value="/view/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 实际开发应用,扫描包下的controller--> <context:component-scan base-package="cn.fufujun.mvc.controller"> </context:component-scan> <mvc:interceptors> <!-- 多个拦截器,顺序执行 --> <mvc:interceptor> <mvc:mapping path="/user/*"/> <mvc:exclude-mapping path="/*"/> <bean class="cn.fufujun.mvc.interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> <!-- 配置文件上传 --> <bean id="multipartResolver" class="org.s 4000 pringframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="102400000"></property> </bean> </beans>
web.xml就是基本配置
Controller层java文件代码如下:
package cn.fufujun.mvc.controller; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.json.JSONObject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import cn.fufujun.mvc.pojo.Picture; import com.facepp.http.HttpRequests; import com.facepp.http.PostParameters; @Controller public class UploadController { @RequestMapping("/upfile.do") public static ModelAndView upfile(MultipartFile uploadfile) throws IllegalStateException, IOException{ Picture pic=null; StringBuffer sb=new StringBuffer(); //获得图片文件名 String filename=uploadfile.getOriginalFilename(); //防止文件夹名字重复 SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); //设置图片保存路径 保存在项目部署的路径 String path="C:"+File.separator+"Tomcat"+File.separator+"apache-tomcat-7.0.73"+ File.separator+"webapps"+File.separator+"spring-mvc"+File.separator+ "image"+File.separator+dateformat.format(new Date()); //System.out.println(filename); File file=new File(path,filename); //判断文件是否存在 if(!file.exists()){ file.mkdirs(); } //将上传的文件写到磁盘 uploadfile.transferTo(file); //储存在磁盘中的路径 String pp=path+File.separator+filename; //System.out.println("pp:"+pp); File fi=new File(pp); /** * 这里的两串字符可以自己注册face++然后获取,或者直接就用示例上的 反正是测试玩玩 * @author Folger */ HttpRequests hs = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M ", true, false); try{ /** * 具体方法可以对照着看API 都有详细讲解 * 这里只是简单的测试人脸识别 获取一些attribute信息 (比如:年龄 性别 人种..) * @author Folger */ PostParameters pps=new PostParameters(); JSONObject result = hs.detectionDetect(pps.setImg(fi).setAttribute("all")) .getJSONArray("face").getJSONObject(0).getJSONObject("attribute"); int age = result.getJSONObject("age").getInt("value"); int agerange=result.getJSONObject("age").getInt("range"); String sex=result.getJSONObject("gender").getString("value"); Double sexconfidence=result.getJSONObject("gender").getDouble("confidence"); String race=result.getJSONObject("race").getString("value"); Double raceconfidence=result.getJSONObject("race").getDouble("confidence"); Double smilingfidence=result.getJSONObject("smiling").getDouble("value"); //System.out.println(result); sb.append(" "+"\n"); sb.append("年龄"+age+"岁左右"+" "); sb.append("误差范围在"+agerange+"岁上下"+"\n"); sb.append("性别为"+sex+" "+"正确率:"+sexconfidence+"%\n"); sb.append("种族为"+race+" "+"正确率:"+raceconfidence+"%\n"); sb.append("正在笑的概率:"+smilingfidence+"%"); }catch(Exception e ){ e.printStackTrace(); } //在项目目录中的路径 根据自己的路径进行截取 例如我的是:/image/2017-01-11-09-29-01/1.jpg String ppp=pp.substring(49).replace("\\", "/"); //System.out.println(sb.toString()); //System.out.println("ppp:"+ppp); pic= new Picture(ppp,sb.toString()); /** * ModelAndView 传值并转发 * @author Folger */ ModelAndView mav=new ModelAndView(); mav.addObject("picture", pic); mav.setViewName("message"); // modelMap.addAttribute("picture", pic); // modelMap.addAttribute("filepic",ppp); // modelMap.addAttribute("message",sb.toString()); return mav; } }
jsp代码 一个是选择图片进行提交 另一个是转发后的jsp很简单只是一个展示:(以后可以换成类似ajax直接在同一页面输出)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> <c:set var="path" value="${pageContext.request.contextPath}"></c:set> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="../js/jquery-1.8.2.min.js"></script> </head> <body> <form action="${path}/upfile.do" method="post" enctype="multipart/form-data"> <input type="file" name="uploadfile" value="上传文件" c184 /> <input type="submit" value="提交" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> <c:set var="path" value="${pageContext.request.contextPath}"></c:set> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="../js/jquery-1.8.2.min.js"></script> <script type="text/javascript"> </script> </head> <body> <div align="center"> <!-- 设置图片的最小高度 高度随图片改变 --> <div align="center" style="height:400px;height:auto;min-height:200px;width:500px;margin:0 auto;"> <!-- 获取图片保存的绝对路径 --> <img style="margin: auto;" alt="" src="${path}${picture.filepic}"> </div> <div style="margin-top: 0px;margin-right: 20px; "> <textarea style="font-family:Arial,Verdana,Sans-serif;font-size:20px;text-align:center;" rows="6" cols="50">${picture.message}</textarea> </div> </div> </body> </html>
示例结果:
照片只是网上随便copy下来的 人脸识别确实厉害啊..
革命尚未成功,同志仍需努力啊!
相关文章推荐
- JSF(Java Server Face)与在Web UI实现托拽式快速开发的应用
- java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)——JAVA夜未眠
- java图像界面开发简单实例-JRadioButton、JCheckBox、JLabel的应用
- 把一个java web应用包装成桌面应用的简单做法
- 转:把一个java web应用包装成桌面应用的简单做法(个人比较喜欢,想有空研究一下,特意转载,以备后用)
- java(j2ee)应用-简单自定义标签开发全过程----之一(Inber)
- [零基础学JAVA]Java SE应用部分-31.Java IO操作(05)IO操作实例讲解之实现简单MIS 推荐
- java图像界面开发简单实例-JTextArea、JScrollPane、JPanel、JButton应用实例
- java图像界面开发简单实例-JButton及事件的简单应用
- 把一个java web应用包装成桌面应用的简单做法(网摘)
- 一个简单的定时器应用: VarTimer (java)
- JAVA 应用简单破解---类库提前加载
- JAVA中日志-log4j简单应用
- java图像界面开发简单实例-JCheckBox、JLabel的应用
- java图像界面开发简单实例-Graphics2D、Rectangle2D、Ellipse2D、Line2D的简单应用
- JSF(Java Server Face)与在Web UI实现托拽式快速开发的应用
- JAVA6新特性之---java桌面应用中生成托盘(tray)图标的简单方法
- java图像界面开发简单实例-JLabel,JFileChooser,JMenu应用
- 把一个java web应用包装成桌面应用的简单做法(转)
- java中的时间相关类简单应用