基于apache httpclient 调用Face++ API

依赖包maven地址:

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.4</version> </dependency>

api_key、api_secret的获取,可以登录旷世科技官网注册获取免费的key和secret。

Detect API说明文档

java 测试代码: package buct.edu.cn; import java.net.URI; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientDemo { public static void main(String[] args) { HttpClient httpclient = HttpClients.createDefault(); //设置请求参数 String api_key = "<api_key>"; String api_secret = "<api_secret>"; String image_url = "http://gb.cri.cn/mmsource/images/2013/02/19/27/12679377093283173211.jpg"; String return_attributes = "gender,age"; try { URIBuilder builder = new URIBuilder("https://api-cn.faceplusplus.com/facepp/v3/detect"); URI uri = builder.build(); HttpPost request = new HttpPost(uri); StringEntity reqEntity = new StringEntity("api_key="+api_key+"&api_secret="+api_secret+"&image_url="+image_url+"&return_attributes="+return_attributes+""); reqEntity.setContentType("application/x-www-form-urlencoded"); request.setEntity(reqEntity); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } catch (Exception e) { System.out.println(e.getMessage()); } } } 测试结果: {"image_id": "qVohAsm0QmanSo9LBNtUcQ==", "request_id": "1514360502,7fc98900-59ca-4ac1-bbf0-95d5ada7588f", "time_used": 308, "faces": [{"attributes": {"gender": {"value": "Female"}, "age": {"value": 25}}, "face_rectangle": {"width": 173, "top": 160, "left": 215, "height": 173}, "face_token": "949f896eca7e645f27afc080971479f5"}]} 注意:

免费版key的QPS(每秒钟支持的调用次数):10,但是这个在后台是与其他用户共享的,所以经常会出现:

{"error_message":"CONCURRENCY_LIMIT_EXCEEDED"}

这个是操过了QPS限制所致,也可以理解,毕竟是免费的产品。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyfxfw.html