1. 程式人生 > >呼叫騰訊優圖開放平臺進行人臉識別-Java呼叫API實現

呼叫騰訊優圖開放平臺進行人臉識別-Java呼叫API實現

http://open.youtu.qq.com官網

直接234.

第一步:鑑權服務技術方案

Java程式碼實現如下

import java.util.Date;

import com.baidu.aip.util.Base64Util;
/**
 * 獲取Authorization
 * @author 小帥丶
 * @類名稱  Sign
 * @remark 
 * @date  2017-8-18
 */
public class Sign {
	/**
	 * Authorization方法
	 * @param userQQ 開發者建立應用時的QQ號
	 * @param AppID 開發者建立應用後的AppID
	 * @param SecretID 開發者建立應用後的SecretID
	 * @param SecretKey 開發者建立應用後的SecretKey
	 * @return sign
	 * @throws Exception
	 */
	public static String getSign(String userQQ,String AppID,String SecretID,String SecretKey) throws Exception{
		long tnowTimes = new Date().getTime()/1000;
		long enowTimes = tnowTimes+2592000;
		String rRandomNum = HMACSHA1.genRandomNum(10);
		String param = "u=" + userQQ + "&a=" + AppID + "&k=" + SecretID + "&e="
				+ enowTimes + "&t=" + tnowTimes + "&r=" + rRandomNum + "&f=";
		byte [] hmacSign = HMACSHA1.getSignature(param, SecretKey);
		byte[] all = new byte[hmacSign.length+param.getBytes().length];
		System.arraycopy(hmacSign, 0, all, 0, hmacSign.length);
		System.arraycopy(param.getBytes(), 0, all, hmacSign.length, param.getBytes().length);
		String sign = Base64Util.encode(all);
		return sign;
	}
}
需要的HMACSHA1程式碼及隨機數程式碼
import java.util.Random;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * HMACSHA1演算法
 * 
 * @author 小帥丶
 * @類名稱 HMACSHA1
 * @remark
 * @date 2017-8-18
 */
public class HMACSHA1 {
	/**
	 * 演算法標識
	 */
	private static final String HMAC_SHA1 = "HmacSHA1";
	/**
	 * 加密
	 * @param data 要加密的資料
	 * @param key 金鑰 
	 * @return
	 * @throws Exception
	 */
	public static byte[] getSignature(String data, String key) throws Exception {
		Mac mac = Mac.getInstance(HMAC_SHA1);
		SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
				mac.getAlgorithm());
		mac.init(signingKey);
		return mac.doFinal(data.getBytes());
	}
	/**
	 * 生成隨機數字
	 * @param length
	 * @return
	 */
	public static String genRandomNum(int length){  
        int  maxNum = 62;  
        int i;  
        int count = 0;  
        char[] str = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};      
        StringBuffer pwd = new StringBuffer("");  
        Random r = new Random();  
        while(count < length){  
         i = Math.abs(r.nextInt(maxNum));     
         if (i >= 0 && i < str.length) {  
          pwd.append(str[i]);  
          count ++;  
         }  
        }  
        return pwd.toString();  
      } 
}

第二步:準備相關程式碼

儲存SIGN 或者每次都生成一個也可以 方便測試就直接每次生成一個了

開始識別圖片之前需要工具類Base64Util  FileUtil

FileUtil程式碼

import java.io.*;

/**
 * 檔案讀取工具類
 */
public class FileUtil {

    /**
     * 讀取檔案內容,作為字串返回
     */
    public static String readFileAsString(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } 

        if (file.length() > 1024 * 1024 * 1024) {
            throw new IOException("File is too large");
        } 

        StringBuilder sb = new StringBuilder((int) (file.length()));
        // 建立位元組輸入流  
        FileInputStream fis = new FileInputStream(filePath);  
        // 建立一個長度為10240的Buffer
        byte[] bbuf = new byte[10240];  
        // 用於儲存實際讀取的位元組數  
        int hasRead = 0;  
        while ( (hasRead = fis.read(bbuf)) > 0 ) {  
            sb.append(new String(bbuf, 0, hasRead));  
        }  
        fis.close();  
        return sb.toString();
    }

    /**
     * 根據檔案路徑讀取byte[] 陣列
     */
    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new FileNotFoundException(filePath);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
            BufferedInputStream in = null;

            try {
                in = new BufferedInputStream(new FileInputStream(file));
                short bufSize = 1024;
                byte[] buffer = new byte[bufSize];
                int len1;
                while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
                    bos.write(buffer, 0, len1);
                }

                byte[] var7 = bos.toByteArray();
                return var7;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException var14) {
                    var14.printStackTrace();
                }

                bos.close();
            }
        }
    }
}

Base64Util  程式碼
/**
 * Base64 工具類
 */
public class Base64Util {
    private static final char last2byte = (char) Integer.parseInt("00000011", 2);
    private static final char last4byte = (char) Integer.parseInt("00001111", 2);
    private static final char last6byte = (char) Integer.parseInt("00111111", 2);
    private static final char lead6byte = (char) Integer.parseInt("11111100", 2);
    private static final char lead4byte = (char) Integer.parseInt("11110000", 2);
    private static final char lead2byte = (char) Integer.parseInt("11000000", 2);
    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', '+', '/'};

    public Base64Util() {
    }

    public static String encode(byte[] from) {
        StringBuilder to = new StringBuilder((int) ((double) from.length * 1.34D) + 3);
        int num = 0;
        char currentByte = 0;

        int i;
        for (i = 0; i < from.length; ++i) {
            for (num %= 8; num < 8; num += 6) {
                switch (num) {
                    case 0:
                        currentByte = (char) (from[i] & lead6byte);
                        currentByte = (char) (currentByte >>> 2);
                    case 1:
                    case 3:
                    case 5:
                    default:
                        break;
                    case 2:
                        currentByte = (char) (from[i] & last6byte);
                        break;
                    case 4:
                        currentByte = (char) (from[i] & last4byte);
                        currentByte = (char) (currentByte << 2);
                        if (i + 1 < from.length) {
                            currentByte = (char) (currentByte | (from[i + 1] & lead2byte) >>> 6);
                        }
                        break;
                    case 6:
                        currentByte = (char) (from[i] & last2byte);
                        currentByte = (char) (currentByte << 4);
                        if (i + 1 < from.length) {
                            currentByte = (char) (currentByte | (from[i + 1] & lead4byte) >>> 4);
                        }
                }

                to.append(encodeTable[currentByte]);
            }
        }

        if (to.length() % 4 != 0) {
            for (i = 4 - to.length() % 4; i > 0; --i) {
                to.append("=");
            }
        }

        return to.toString();
    }
}

建立應用得到的值放在一個常量類裡面


/**
 * 常量
 * @author 小帥丶
 * @類名稱  YouTuAppContants
 * @remark 
 * @date  2017-8-18
 */
public class YouTuAppContants {
	public static String AppID = "你自己的AppID ";
	public static String SecretID = "你自己的SecretID";
	public static String SecretKey = "你自己的SecretKey";
	public static String userQQ = "你自己的userQQ";
}
還需要一個HTTPUTIL工具類
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * http 工具類
 */
public class HttpUtilYoutu {
	 private static class TrustAnyTrustManager implements X509TrustManager {
		 
	        public void checkClientTrusted(X509Certificate[] chain, String authType)
	                throws CertificateException {
	        }
	 
	        public void checkServerTrusted(X509Certificate[] chain, String authType)
	                throws CertificateException {
	        }
	 
	        public X509Certificate[] getAcceptedIssuers() {
	            return new X509Certificate[] {};
	        }
	    }
	 
	    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
	        public boolean verify(String hostname, SSLSession session) {
	            return true;
	        }
	    }
	 
	    /**
	     * post方式請求伺服器(https協議)
	     * 
	     * @param url
	     *            請求地址
	     * @param content
	     *            引數
	     * @param charset
	     *            編碼
	     * @return
	     * @throws NoSuchAlgorithmException
	     * @throws KeyManagementException
	     * @throws IOException
	     */
	    public static String post(String url, String content,String charset,String sign)
	            throws NoSuchAlgorithmException, KeyManagementException,
	            IOException {
	        SSLContext sc = SSLContext.getInstance("SSL");
	        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
	                new java.security.SecureRandom());
	        URL console = new URL(url);
	        Integer length = content.length();
	        HttpURLConnection conn = (HttpURLConnection) console.openConnection();
	        //文件要求填寫的Header引數                
                conn.setRequestProperty("Host", "api.youtu.qq.com");
	        conn.setRequestProperty("Content-Length",length.toString());
	        conn.setRequestProperty("Content-Type", "text/json");
	        conn.setRequestProperty("Authorization", sign);
                //文件要求填寫的Header引數
               conn.setDoOutput(true);
	        conn.connect();
	        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
	        out.write(content.getBytes(charset));
	        // 重新整理、關閉
	        out.flush();
	        out.close();
	        BufferedReader in = null;
	        in = new BufferedReader(
	                new InputStreamReader(conn.getInputStream(), charset));
	        String result = "";
	        String getLine;
	        while ((getLine = in.readLine()) != null) {
	            result += getLine;
	        }
	        in.close();
	        System.err.println("result:" + result);
	        return result;
	    }

}

第三步:測試一下人臉檢測介面

請求示例類:

import com.xiaoshuai.test.Base64Util;
import com.xiaoshuai.test.FileUtil;

public class DetectFace {
	//人臉檢測介面
	public static String DETECTFACE_URL="http://api.youtu.qq.com/youtu/api/detectface";
	public static void main(String[] args) throws Exception {
		String imagePath = "G:/test2.jpg";
		System.out.println(getDetectFace(imagePath));
		
	}
	/**
	 * 檢測圖中人臉資訊
	 * @param imagePath 圖片路徑
	 * @return
	 * @throws Exception
	 */
	public static String getDetectFace(String imagePath) throws Exception{
		//得到Authorization
		String sign = Sign.getSign(YouTuAppContants.userQQ,
				YouTuAppContants.AppID, YouTuAppContants.SecretID,
				YouTuAppContants.SecretKey);
		byte[] imgData = FileUtil.readFileByBytes(imagePath);
		String image =  Base64Util.encode(imgData);;
		String param = "{\"app_id\":\""+YouTuAppContants.AppID+"\",\"image\":\""+image+"\"}";
		String result = HttpUtilYoutu.post(DETECTFACE_URL, param, "UTF-8",sign);
		System.out.println(result);
		return result;
	}
}

看看返回的內容:
{"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"}

 忽略掉檢測五官位置的資料:

            "face_id": "2188093443753939350",
            "x": 128,
            "y": 127,
            "height": 106,
            "width": 106,
            "pitch": 6,
            "roll": 3,
            "yaw": -2,
            "age": 23,//年齡 [0~100]
            "gender": 94,//性別 [0/(female)~100(male)]
            "glass": true,//是否有眼鏡 [true,false]
            "expression": 34,//微笑[0(normal)~50(smile)~100(laugh)]
            "beauty": 80,//魅力 [0~100]


想知道自己的魅力嗎?那就掃描下面的二維碼吧(微信小程式碼)

想體驗一下?那就掃描一下我的微信個人小程式吧。