调用腾讯优图开放平台进行人脸识别-Java调用API实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用腾讯优图开放平台进行人脸识别-Java调用API实现相关的知识,希望对你有一定的参考价值。

ttp://open.youtu.qq.com官网

腾讯产品文档

技术分享

直接234.

第一步:鉴权服务技术方案

Java代码实现如下

  1. import java.util.Date;  
  2.   
  3. import com.baidu.aip.util.Base64Util;  
  4. /** 
  5.  * 获取Authorization 
  6.  * @author 小帅丶 
  7.  * @类名称  Sign 
  8.  * @remark  
  9.  * @date  2017-8-18 
  10.  */  
  11. public class Sign {  
  12.     /** 
  13.      * Authorization方法 
  14.      * @param userQQ 开发者创建应用时的QQ号 
  15.      * @param AppID 开发者创建应用后的AppID 
  16.      * @param SecretID 开发者创建应用后的SecretID 
  17.      * @param SecretKey 开发者创建应用后的SecretKey 
  18.      * @return sign 
  19.      * @throws Exception 
  20.      */  
  21.     public static String getSign(String userQQ,String AppID,String SecretID,String SecretKey) throws Exception{  
  22.         long tnowTimes = new Date().getTime()/1000;  
  23.         long enowTimes = tnowTimes+2592000;  
  24.         String rRandomNum = HMACSHA1.genRandomNum(10);  
  25.         String param = "u=" + userQQ + "&a=" + AppID + "&k=" + SecretID + "&e="  
  26.                 + enowTimes + "&t=" + tnowTimes + "&r=" + rRandomNum + "&f=";  
  27.         byte [] hmacSign = HMACSHA1.getSignature(param, SecretKey);  
  28.         byte[] all = new byte[hmacSign.length+param.getBytes().length];  
  29.         System.arraycopy(hmacSign, 0, all, 0, hmacSign.length);  
  30.         System.arraycopy(param.getBytes(), 0, all, hmacSign.length, param.getBytes().length);  
  31.         String sign = Base64Util.encode(all);  
  32.         return sign;  
  33.     }  
  34. }  

需要的HMACSHA1代码及随机数代码

  1. import java.util.Random;  
  2.   
  3. import javax.crypto.Mac;  
  4. import javax.crypto.spec.SecretKeySpec;  
  5.   
  6. /** 
  7.  * HMACSHA1算法 
  8.  *  
  9.  * @author 小帅丶 
  10.  * @类名称 HMACSHA1 
  11.  * @remark 
  12.  * @date 2017-8-18 
  13.  */  
  14. public class HMACSHA1 {  
  15.     /** 
  16.      * 算法标识 
  17.      */  
  18.     private static final String HMAC_SHA1 = "HmacSHA1";  
  19.     /** 
  20.      * 加密 
  21.      * @param data 要加密的数据 
  22.      * @param key 密钥  
  23.      * @return 
  24.      * @throws Exception 
  25.      */  
  26.     public static byte[] getSignature(String data, String key) throws Exception {  
  27.         Mac mac = Mac.getInstance(HMAC_SHA1);  
  28.         SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),  
  29.                 mac.getAlgorithm());  
  30.         mac.init(signingKey);  
  31.         return mac.doFinal(data.getBytes());  
  32.     }  
  33.     /** 
  34.      * 生成随机数字 
  35.      * @param length 
  36.      * @return 
  37.      */  
  38.     public static String genRandomNum(int length){    
  39.         int  maxNum = 62;    
  40.         int i;    
  41.         int count = 0;    
  42.         char[] str = {‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘};        
  43.         StringBuffer pwd = new StringBuffer("");    
  44.         Random r = new Random();    
  45.         while(count < length){    
  46.          i = Math.abs(r.nextInt(maxNum));       
  47.          if (i >= 0 && i < str.length) {    
  48.           pwd.append(str[i]);    
  49.           count ++;    
  50.          }    
  51.         }    
  52.         return pwd.toString();    
  53.       }   
  54. }  

第二步:准备相关代码 

保存SIGN 或者每次都生成一个也可以 方便测试就直接每次生成一个了

开始识别图片之前需要工具类Base64Util  FileUtil

FileUtil代码 

  1. import java.io.*;  
  2.   
  3. /** 
  4.  * 文件读取工具类 
  5.  */  
  6. public class FileUtil {  
  7.   
  8.     /** 
  9.      * 读取文件内容,作为字符串返回 
  10.      */  
  11.     public static String readFileAsString(String filePath) throws IOException {  
  12.         File file = new File(filePath);  
  13.         if (!file.exists()) {  
  14.             throw new FileNotFoundException(filePath);  
  15.         }   
  16.   
  17.         if (file.length() > 1024 * 1024 * 1024) {  
  18.             throw new IOException("File is too large");  
  19.         }   
  20.   
  21.         StringBuilder sb = new StringBuilder((int) (file.length()));  
  22.         // 创建字节输入流    
  23.         FileInputStream fis = new FileInputStream(filePath);    
  24.         // 创建一个长度为10240的Buffer  
  25.         byte[] bbuf = new byte[10240];    
  26.         // 用于保存实际读取的字节数    
  27.         int hasRead = 0;    
  28.         while ( (hasRead = fis.read(bbuf)) > 0 ) {    
  29.             sb.append(new String(bbuf, 0, hasRead));    
  30.         }    
  31.         fis.close();    
  32.         return sb.toString();  
  33.     }  
  34.   
  35.     /** 
  36.      * 根据文件路径读取byte[] 数组 
  37.      */  
  38.     public static byte[] readFileByBytes(String filePath) throws IOException {  
  39.         File file = new File(filePath);  
  40.         if (!file.exists()) {  
  41.             throw new FileNotFoundException(filePath);  
  42.         } else {  
  43.             ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());  
  44.             BufferedInputStream in = null;  
  45.   
  46.             try {  
  47.                 in = new BufferedInputStream(new FileInputStream(file));  
  48.                 short bufSize = 1024;  
  49.                 byte[] buffer = new byte[bufSize];  
  50.                 int len1;  
  51.                 while (-1 != (len1 = in.read(buffer, 0, bufSize))) {  
  52.                     bos.write(buffer, 0, len1);  
  53.                 }  
  54.   
  55.                 byte[] var7 = bos.toByteArray();  
  56.                 return var7;  
  57.             } finally {  
  58.                 try {  
  59.                     if (in != null) {  
  60.                         in.close();  
  61.                     }  
  62.                 } catch (IOException var14) {  
  63.                     var14.printStackTrace();  
  64.                 }  
  65.   
  66.                 bos.close();  
  67.             }  
  68.         }  
  69.     }  
  70. }  


Base64Util  代码

  1. /** 
  2.  * Base64 工具类 
  3.  */  
  4. public class Base64Util {  
  5.     private static final char last2byte = (char) Integer.parseInt("00000011", 2);  
  6.     private static final char last4byte = (char) Integer.parseInt("00001111", 2);  
  7.     private static final char last6byte = (char) Integer.parseInt("00111111", 2);  
  8.     private static final char lead6byte = (char) Integer.parseInt("11111100", 2);  
  9.     private static final char lead4byte = (char) Integer.parseInt("11110000", 2);  
  10.     private static final char lead2byte = (char) Integer.parseInt("11000000", 2);  
  11.     private static final char[] encodeTable = new char[]{‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘+‘, ‘/‘};  
  12.   
  13.     public Base64Util() {  
  14.     }  
  15.   
  16.     public static String encode(byte[] from) {  
  17.         StringBuilder to = new StringBuilder((int) ((double) from.length * 1.34D) + 3);  
  18.         int num = 0;  
  19.         char currentByte = 0;  
  20.   
  21.         int i;  
  22.         for (i = 0; i < from.length; ++i) {  
  23.             for (num %= 8; num < 8; num += 6) {  
  24.                 switch (num) {  
  25.                     case 0:  
  26.                         currentByte = (char) (from[i] & lead6byte);  
  27.                         currentByte = (char) (currentByte >>> 2);  
  28.                     case 1:  
  29.                     case 3:  
  30.                     case 5:  
  31.                     default:  
  32.                         break;  
  33.                     case 2:  
  34.                         currentByte = (char) (from[i] & last6byte);  
  35.                         break;  
  36.                     case 4:  
  37.                         currentByte = (char) (from[i] & last4byte);  
  38.                         currentByte = (char) (currentByte << 2);  
  39.                         if (i + 1 < from.length) {  
  40.                             currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);  
  41.                         }  
  42.                         break;  
  43.                     case 6:  
  44.                         currentByte = (char) (from[i] & last2byte);  
  45.                         currentByte = (char) (currentByte << 4);  
  46.                         if (i + 1 < from.length) {  
  47.                             currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);  
  48.                         }  
  49.                 }  
  50.   
  51.                 to.append(encodeTable[currentByte]);  
  52.             }  
  53.         }  
  54.   
  55.         if (to.length() % 4 != 0) {  
  56.             for (i = 4 - to.length() % 4; i > 0; --i) {  
  57.                 to.append("=");  
  58.             }  
  59.         }  
  60.   
  61.         return to.toString();  
  62.     }  
  63. }  


创建应用得到的值放在一个常量类里面

 

技术分享

 
  1. /** 
  2.  * 常量 
  3.  * @author 小帅丶 
  4.  * @类名称  YouTuAppContants 
  5.  * @remark  
  6.  * @date  2017-8-18 
  7.  */  
  8. public class YouTuAppContants {  
  9.     public static String AppID = "你自己的AppID ";  
  10.     public static String SecretID = "你自己的SecretID";  
  11.     public static String SecretKey = "你自己的SecretKey";  
  12.     public static String userQQ = "你自己的userQQ";  
  13. }  

还需要一个HTTPUTIL工具类

 
  1. import java.io.BufferedReader;  
  2. import java.io.DataOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import java.security.KeyManagementException;  
  8. import java.security.NoSuchAlgorithmException;  
  9. import java.security.cert.CertificateException;  
  10. import java.security.cert.X509Certificate;  
  11.    
  12. import javax.net.ssl.HostnameVerifier;  
  13. import javax.net.ssl.SSLContext;  
  14. import javax.net.ssl.SSLSession;  
  15. import javax.net.ssl.TrustManager;  
  16. import javax.net.ssl.X509TrustManager;  
  17.   
  18. /** 
  19.  * http 工具类 
  20.  */  
  21. public class HttpUtilYoutu {  
  22.      private static class TrustAnyTrustManager implements X509TrustManager {  
  23.            
  24.             public void checkClientTrusted(X509Certificate[] chain, String authType)  
  25.                     throws CertificateException {  
  26.             }  
  27.        
  28.             public void checkServerTrusted(X509Certificate[] chain, String authType)  
  29.                     throws CertificateException {  
  30.             }  
  31.        
  32.             public X509Certificate[] getAcceptedIssuers() {  
  33.                 return new X509Certificate[] {};  
  34.             }  
  35.         }  
  36.        
  37.         private static class TrustAnyHostnameVerifier implements HostnameVerifier {  
  38.             public boolean verify(String hostname, SSLSession session) {  
  39.                 return true;  
  40.             }  
  41.         }  
  42.        
  43.         /** 
  44.          * post方式请求服务器(https协议) 
  45.          *  
  46.          * @param url 
  47.          *            请求地址 
  48.          * @param content 
  49.          *            参数 
  50.          * @param charset 
  51.          *            编码 
  52.          * @return 
  53.          * @throws NoSuchAlgorithmException 
  54.          * @throws KeyManagementException 
  55.          * @throws IOException 
  56.          */  
  57.         public static String post(String url, String content,String charset,String sign)  
  58.                 throws NoSuchAlgorithmException, KeyManagementException,  
  59.                 IOException {  
  60.             SSLContext sc = SSLContext.getInstance("SSL");  
  61.             sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },  
  62.                     new java.security.SecureRandom());  
  63.             URL console = new URL(url);  
  64.             Integer length = content.length();  
  65.             HttpURLConnection conn = (HttpURLConnection) console.openConnection();  
  66.             //文档要求填写的Header参数                  
  67.                 conn.setRequestProperty("Host", "api.youtu.qq.com");  
  68.             conn.setRequestProperty("Content-Length",length.toString());  
  69.             conn.setRequestProperty("Content-Type", "text/json");  
  70.             conn.setRequestProperty("Authorization", sign);  
  71.                 //文档要求填写的Header参数  
  72.                conn.setDoOutput(true);  
  73.             conn.connect();  
  74.             DataOutputStream out = new DataOutputStream(conn.getOutputStream());  
  75.             out.write(content.getBytes(charset));  
  76.             // 刷新、关闭  
  77.             out.flush();  
  78.             out.close();  
  79.             BufferedReader in = null;  
  80.             in = new BufferedReader(  
  81.                     new InputStreamReader(conn.getInputStream(), charset));  
  82.             String result = "";  
  83.             String getLine;  
  84.             while ((getLine = in.readLine()) != null) {  
  85.                 result += getLine;  
  86.             }  
  87.             in.close();  
  88.             System.err.println("result:" + result);  
  89.             return result;  
  90.         }  
  91.   
  92. }  


第三步:测试一下人脸检测接口

请求示例类:

 
  1. import com.xiaoshuai.test.Base64Util;  
  2. import com.xiaoshuai.test.FileUtil;  
  3.   
  4. public class DetectFace {  
  5.     //人脸检测接口  
  6.     public static String DETECTFACE_URL="http://api.youtu.qq.com/youtu/api/detectface";  
  7.     public static void main(String[] args) throws Exception {  
  8.         String imagePath = "G:/test2.jpg";  
  9.         System.out.println(getDetectFace(imagePath));  
  10.           
  11.     }  
  12.     /** 
  13.      * 检测图中人脸信息 
  14.      * @param imagePath 图片路径 
  15.      * @return 
  16.      * @throws Exception 
  17.      */  
  18.     public static String getDetectFace(String imagePath) throws Exception{  
  19.         //得到Authorization  
  20.         String sign = Sign.getSign(YouTuAppContants.userQQ,  
  21.                 YouTuAppContants.AppID, YouTuAppContants.SecretID,  
  22.                 YouTuAppContants.SecretKey);  
  23.         byte[] imgData = FileUtil.readFileByBytes(imagePath);  
  24.         String image =  Base64Util.encode(imgData);;  
  25.         String param = "{\"app_id\":\""+YouTuAppContants.AppID+"\",\"image\":\""+image+"\"}";  
  26.         String result = HttpUtilYoutu.post(DETECTFACE_URL, param, "UTF-8",sign);  
  27.         System.out.println(result);  
  28.         return result;  
  29.     }  
  30. }  


看看返回的内容:

 
  1. {"session_id":"","image_height":280,"image_width":359,"face":[{"face_id":"2188093443753939350","x":128,"y":127,"height":106.0,"width":106.0,"pitch":6,"roll":3,"yaw":-2,"age":23,"gender":94,"glass":true,"expression":34,"beauty":80,"face_shape":{"face_profile":[{"x":139,"y":160},{"x":139,"y":170},{"x":140,"y":180},{"x":143,"y":189},{"x":146,"y":199},{"x":150,"y":208},{"x":156,"y":217},{"x":162,"y":225},{"x":170,"y":232},{"x":179,"y":236},{"x":189,"y":238},{"x":199,"y":235},{"x":207,"y":230},{"x":214,"y":222},{"x":221,"y":214},{"x":225,"y":205},{"x":229,"y":195},{"x":231,"y":185},{"x":232,"y":174},{"x":233,"y":164},{"x":232,"y":155}],"left_eye":[{"x":152,"y":159},{"x":156,"y":161},{"x":161,"y":163},{"x":166,"y":162},{"x":171,"y":160},{"x":167,"y":157},{"x":162,"y":156},{"x":157,"y":157}],"right_eye":[{"x":216,"y":156},{"x":212,"y":159},{"x":208,"y":160},{"x":203,"y":160},{"x":198,"y":158},{"x":202,"y":155},{"x":206,"y":154},{"x":211,"y":154}],"left_eyebrow":[{"x":143,"y":148},{"x":151,"y":148},{"x":159,"y":148},{"x":166,"y":148},{"x":174,"y":147},{"x":167,"y":142},{"x":158,"y":141},{"x":150,"y":142}],"right_eyebrow":[{"x":224,"y":145},{"x":216,"y":145},{"x":208,"y":145},{"x":200,"y":146},{"x":192,"y":146},{"x":199,"y":141},{"x":208,"y":139},{"x":217,"y":139}],"mouth":[{"x":170,"y":209},{"x":175,"y":214},{"x":180,"y":218},{"x":187,"y":219},{"x":194,"y":217},{"x":200,"y":213},{"x":204,"y":207},{"x":199,"y":203},{"x":192,"y":201},{"x":186,"y":203},{"x":180,"y":202},{"x":174,"y":205},{"x":175,"y":210},{"x":181,"y":211},{"x":187,"y":211},{"x":193,"y":210},{"x":198,"y":209},{"x":198,"y":207},{"x":193,"y":208},{"x":187,"y":209},{"x":180,"y":207},{"x":175,"y":207}],"nose":[{"x":184,"y":184},{"x":183,"y":160},{"x":180,"y":166},{"x":177,"y":173},{"x":174,"y":180},{"x":170,"y":188},{"x":178,"y":191},{"x":185,"y":192},{"x":192,"y":190},{"x":199,"y":186},{"x":194,"y":179},{"x":191,"y":172},{"x":187,"y":166}]}}],"errorcode":0,"errormsg":"OK"}  
 
    1. "face_id": "2188093443753939350",  
    2. "x": 128,  
    3. "y": 127,  
    4. "height": 106,  
    5. "width": 106,  
    6. "pitch": 6,  
    7. "roll": 3,  
    8. "yaw": -2,  
    9. "age": 23,//年龄 [0~100]  
    10. "gender": 94,//性别 [0/(female)~100(male)]  
    11. "glass": true,//是否有眼镜 [true,false]  
    12. "expression": 34,//微笑[0(normal)~50(smile)~100(laugh)]  
    13. "beauty": 80,//魅力 [0~100]  

以上是关于调用腾讯优图开放平台进行人脸识别-Java调用API实现的主要内容,如果未能解决你的问题,请参考以下文章

腾讯优图人脸检测

基于百度AI开放平台的人脸识别及语音合成

腾讯优图及知脸(ZKface)人脸比对接口测试(python)

腾讯技术工程 |腾讯AI Lab刷新人脸识别与检测两大测评国际记录,技术日调用超六亿

腾讯优图开放手机端深度学习框架,无第三方库依赖,可跨平台使用

多维活体检测,让人脸识别更安全