face++做人脸识别的过程

1.java代码

package test;
 

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.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import javax.net.ssl.SSLException;

import net.sf.json.JSONObject;

public class tt {
    
    public static void main(String[] args) throws Exception{
        
      //  File file = new File("src/test/zhu.jpg");/// src/text/c033.jpg
         //File file = new File("F:\\课程设计的文件\\1111.png");/// src/text/c033.jpgzhu.jpg
         File file = new File("D:/java\\workspace64/.metadata/.plugins/org.eclipse.wst.server.core/tmp2/wtpwebapps/vote/takephoto/1111.png");/// src/text/c033.jpgzhu.jpg
        byte[] buff = getBytesFromFile(file);
        String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "自己的");
        map.put("api_secret", "自己的");
        
        //map.put("return_landmark", "0");
       // map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
        map.put("return_attributes", "gender,age,smiling,facequality,eyestatus,beauty,skinstatus");
       
        
        byteMap.put("image_file", buff);
        StringBuffer sb=new StringBuffer();
        try{
            byte[] bacd = post(url, map, byteMap);
            System.out.println("返回的数据 \n"+new String(bacd));
           // String sss =" "image_id": "eie5oKbn8YYPewrF7raZCw==", "request_id": "1523453736,339de48d-0e7c-44fe-aa02-1d1eabc71a23", "time_used": 501, "faces": [{"attributes": {"beauty": {"female_score": 58.797, "male_score": 64.006}, "gender": {"value": "Male"}, "age": {"value": 22}, "eyestatus": {"left_eye_status": {"normal_glass_eye_open": 2.944, "no_glass_eye_close": 0.509, "occlusion": 5.098, "no_glass_eye_open": 0.148, "normal_glass_eye_close": 91.164, "dark_glasses": 0.137}, "right_eye_status": {"normal_glass_eye_open": 18.008, "no_glass_eye_close": 10.55, "occlusion": 1.408, "no_glass_eye_open": 2.528, "normal_glass_eye_close": 12.923, "dark_glasses": 54.582}}, "glass": {"value": "Normal"}, "skinstatus": {"dark_circle": 14.685, "stain": 1.854, "acne": 6.566, "health": 2.193}, "smile": {"threshold": 30.1, "value": 14.173}, "facequality": {"threshold": 70.1, "value": 74.604}}, "face_rectangle": {"width": 161, "top": 135, "left": 86, "height": 161}, "face_token": "6a84a349564c9c4644a69581b21009a0"}]";
//            
            JSONObject object = JSONObject.fromObject(new String(bacd));
            JSONObject resultObject=object.getJSONObject("");
            String e=object.get("image_id").toString();
            System.out.println(e);
//            
        }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;
    }


}

2.返回的数据

{"image_id":"eie5oKbn8YYPewrF7raZCw==", "request_id":"1523492606,1590d29f-2e8a-4540-aa43-95bb152f1151","time_used": 534, "faces": [{"attributes":{"beauty": {"female_score": 58.797, "male_score":64.006}, "gender": {"value": "Male"},"age": {"value": 22}, "eyestatus":{"left_eye_status": {"normal_glass_eye_open": 2.944,"no_glass_eye_close": 0.509, "occlusion": 5.098,"no_glass_eye_open": 0.148, "normal_glass_eye_close":91.164, "dark_glasses": 0.137}, "right_eye_status":{"normal_glass_eye_open": 18.008, "no_glass_eye_close":10.55, "occlusion": 1.408, "no_glass_eye_open": 2.528,"normal_glass_eye_close": 12.923, "dark_glasses": 54.582}},"glass": {"value": "Normal"}, "skinstatus":{"dark_circle": 14.685, "stain": 1.854, "acne":6.566, "health": 2.193}, "smile": {"threshold":30.1, "value": 14.173}, "facequality":{"threshold": 70.1, "value": 74.604}},"face_rectangle": {"width": 161, "top": 135,"left": 86, "height": 161}, "face_token":"4ced3bd272d2aa60e89ce0bbe18293e3"}]}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值