whyclj
2020-04-27 48656a374afddd8cb2a758ed5eef376ae1d64302
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.fgkj.services;
 
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
 
import com.fgkj.actions.ActionUtil;
 
/**
* 人脸检测与属性分析
*/
public class FaceDetect {
 
    /**
    * 重要提示代码中所需工具类
    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
    * 下载
    */
    public static String faceDetect(String json) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
        try {
            //Map<String, Object> map = new HashMap<>();
            //map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
            //map.put("face_field", "faceshape,facetype");
            //map.put("image_type", "FACE_TOKEN");
            //map.put("accessToken", "************");
            Map<String, Object> map = ActionUtil.getGson().fromJson(json, HashMap.class);
            
            String param = GsonUtils.toJson(map);
 
            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            //String accessToken = "[调用鉴权接口获取的token]";
            String msg = HttpUtil.post(url, map.get("accessToken").toString(), "application/json", param);
            
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     *     人脸搜索
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
     public static String faceSearch(String json) {
         // 请求url
         String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
         try {
             /*Map<String, Object> map = new HashMap<>();
             map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
             map.put("liveness_control", "NORMAL");
             map.put("group_id_list", "group_repeat,group_233");
             map.put("image_type", "FACE_TOKEN");
             map.put("quality_control", "LOW");*/
 
             Map<String, Object> map = ActionUtil.getGson().fromJson(json, HashMap.class);
             
             String param = GsonUtils.toJson(map);
 
             // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
             //String accessToken = "[调用鉴权接口获取的token]";
 
             String result = HttpUtil.post(url, map.get("accessToken").toString(), "application/json", param);
             System.out.println(result);
             return result;
         } catch (Exception e) {
             e.printStackTrace();
         }
         return null;
     }
 
    public static void main(String[] args) {
        
        //map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
        //map.put("face_field", "faceshape,facetype");
        //map.put("image_type", "FACE_TOKEN");
        //map.put("accessToken", "************");
//        String json = "{'accessToken':'24.0363955052d87d6558e635ce1990a0ae.2592000.1590564753.282335-19585027',"
//                + "'image_type':'BASE64',"
//                + "'face_field':'age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type,mask,spoofing',"
//                + "}";
        String imgstr = "";
        File file = new File("src/img.txt");
        String courseFile = file.getAbsolutePath();
        System.out.println(courseFile);
        try {
            FileInputStream fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int len = 0;
            try {
                while((len = fis.read(b))!=-1){
                    imgstr += new String(b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } 
        Map map = new HashMap<String, String>();
        map.put("accessToken", "24.0363955052d87d6558e635ce1990a0ae.2592000.1590564753.282335-19585027");
        map.put("image_type", "BASE64");
        map.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type,mask,spoofing");
        map.put("image", imgstr);
        
        //System.out.println(json);
        System.out.println( FaceDetect.faceDetect(ActionUtil.tojson(map)));
    }
}