【问题标题】:Face++ Compare APIFace++ 比较 API
【发布时间】:2020-01-22 23:06:47
【问题描述】:

我需要比较 java 中的 2 张人脸图像和它们的置信度值。我正在努力学习阅读face++提供的官方文档,但我认为它有点差。我需要在这里使用人脸比较 API:https://console.faceplusplus.com/documents/5679308。我不明白如何构建 url 来发送请求。页面末尾有这些代码:

curl -X POST "https://api-us.faceplusplus.com/facepp/v3/compare" \
-F "api_key=<api_key>" \
-F "api_secret=<api_secret>" \
-F "face_token1=c2fc0ad7c8da3af5a34b9c70ff764da0" \
-F "face_token2=ad248a809408b6320485ab4de13fe6a9"

现在,我在他们的文档中看到的唯一代码是:

https://console.faceplusplus.com/documents/7078069

但它不起作用,或者至少,这不是用于“面部比较”。

我需要创建并获取面部比较 API 的请求(开头的第一个链接)。我找不到任何关于如何做到这一点的 java 示例。我是用java做的

【问题讨论】:

    标签: java api machine-learning computer-vision face-recognition


    【解决方案1】:

    使用文档中提供的相同sample code,您需要做的就是像这样更改 main 方法:

    public static void main(String[] args) throws Exception{
    
        // Create a new file object for the first file and get bytes from file
        File file = new File("C:\\Users\\ihene\\Desktop\\my-photo.jpg");
        byte[] buff1 = getBytesFromFile(file);
    
        // Create a new file object for the second file and get bytes from file
        File file2 = new File("C:\\Users\\ihene\\Desktop\\esan-caleb.jpg");
        byte[] buff2 = getBytesFromFile(file2);
    
        // Data needed to use the Face++ Compare API
        String url = "https://api-us.faceplusplus.com/facepp/v3/compare";
        HashMap<String, String> map = new HashMap<>();
        HashMap<String, byte[]> byteMap = new HashMap<>();
        map.put("api_key", "dam4ZdTkSsZOUAiR4oQpP3DRnjEz1fcD");
        map.put("api_secret", "0MOCfpum1Lec06EMOzuJPOEa_EhM4Ttg");
    
        byteMap.put("image_file1", buff1);
        byteMap.put("image_file2", buff2);
    
        try {
            // Connecting and retrieving the JSON results
            byte[] bacd = post(url, map, byteMap);
            String jsonStr = new String(bacd);
    
            // Parse the JSON and get the confidence value
            JSONObject obj = new JSONObject(jsonStr);
            double confidence = obj.getDouble("confidence");
            System.out.println(confidence);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    只需将PATH_TO_FILE_1PATH_TO_FILE_2YOUR_API_KEYYOUR_API_SECRET 替换为适当的值。它应该可以工作。

    我已经使用两张属于同一个人的图片和另外两张不同面孔的图片测试了 API。这是我为前者得到的输出:

    {"faces1": [{"face_rectangle": {"width": 156, "top": 210, "left": 142, "height": 156}, "face_token": "1ee9de6d362b0c8c1bf240a70fbf3eac"}], "faces2": [{"face_rectangle": {"width": 156, "top": 210, "left": 142, "height": 156}, "face_token": "0a7c539f3603aa744ee18c65acc224a8"}], "time_used": 531, "thresholds": {"1e-3": 62.327, "1e-5": 73.975, "1e-4": 69.101}, "confidence": 97.389, "image_id2": "Fc64vrBtETVmP2cS+BoW/Q==", "image_id1": "Fc64vrBtETVmP2cS+BoW/Q==", "request_id": "1569184566,0cc04f1a-1495-4316-928c-1efe7d6836dc"}
    

    请注意,此处的置信度为 97.389documentation 表示“置信度越高表明两张脸属于同一个人的可能性越高。”所以,这符合我们的预期。对于后面不同人脸的图像,输出如下:

    {"faces1": [{"face_rectangle": {"width": 156, "top": 210, "left": 142, "height": 156}, "face_token": "c7d8561f0235a99d9b060750c7a9c3c7"}], "faces2": [{"face_rectangle": {"width": 75, "top": 44, "left": 53, "height": 75}, "face_token": "ad87092cb593128c015c2e2221b962f2"}], "time_used": 533, "thresholds": {"1e-3": 62.327, "1e-5": 73.975, "1e-4": 69.101}, "confidence": 53.993, "image_id2": "MiZOf00hrq7OmAxc3+n7sg==", "image_id1": "Fc64vrBtETVmP2cS+BoW/Q==", "request_id": "1569185082,0bfd0a0f-a376-4dac-92bd-924c34ef76ed"}
    

    在这里,置信度为 53.933

    【讨论】:

    • 谢谢伙计。有效。现在我只需要解析这个 Jason 即可获得置信度值。我是初学者,你能帮我吗?
    • 我也在学习。我确实记得过去在玩 Android 时使用 org.json 来解析 JSON。你也可以在这里使用它。我编辑了答案以添加负责解析 JSON 并获取置信度值的代码。首先,您需要下载 *.jar 文件并将其添加到项目的外部库中,以便在编译期间可以将其添加到类路径中。之后将import org.json.JSONObject; 语句添加到源文件的顶部,上面的代码应该可以正常运行。
    • 您可能还会发现以下答案很有帮助:stackoverflow.com/questions/2591098/how-to-parse-json-in-javastackoverflow.com/questions/8390719/…stackoverflow.com/questions/5698900/…。我使用这个工具来更好地查看 JSON 数据:jsonformatter.org/json-pretty-print。最后,您还可以快速搜索 org.json 的 JSONObject 中的其他方法,例如 getString,如果置信度的值是字符串,我们会使用这些方法。
    猜你喜欢
    • 2017-04-15
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多