|
@@ -0,0 +1,239 @@
|
|
|
|
+package cn.com.qmth.examcloud.web.facepp;
|
|
|
|
+
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.compress.utils.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.helpers.JsonHttpResponseHolder;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * face++ 客户端
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @date 2019年9月16日
|
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
|
+ */
|
|
|
|
+public class FaceppClient {
|
|
|
|
+
|
|
|
|
+ protected static ExamCloudLog log = ExamCloudLogFactory.getLog(FaceppClient.class);
|
|
|
|
+
|
|
|
|
+ private static CloseableHttpClient httpclient;
|
|
|
|
+
|
|
|
|
+ private static RequestConfig requestConfig;
|
|
|
|
+
|
|
|
|
+ private static FaceppClient faceppClient;
|
|
|
|
+
|
|
|
|
+ private static String apiKey;
|
|
|
|
+
|
|
|
|
+ private static String apiSecret;
|
|
|
|
+
|
|
|
|
+ private FaceppClient() {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取单例
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public FaceppClient getClient() {
|
|
|
|
+ if (null == faceppClient) {
|
|
|
|
+ synchronized (FaceppClient.class) {
|
|
|
|
+ if (null == faceppClient) {
|
|
|
|
+ faceppClient = new FaceppClient();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return faceppClient;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static {
|
|
|
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(60,
|
|
|
|
+ TimeUnit.SECONDS);
|
|
|
|
+ cm.setValidateAfterInactivity(1000);
|
|
|
|
+ cm.setMaxTotal(8000);
|
|
|
|
+ cm.setDefaultMaxPerRoute(800);
|
|
|
|
+ httpclient = HttpClients.custom().setConnectionManager(cm).disableAutomaticRetries()
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ requestConfig = RequestConfig.custom().setConnectionRequestTimeout(500)
|
|
|
|
+ .setSocketTimeout(10000).setConnectTimeout(10000).build();
|
|
|
|
+
|
|
|
|
+ apiKey = PropertyHolder.getString("$facepp.apiKey");
|
|
|
|
+ apiSecret = PropertyHolder.getString("$facepp.apiSecret");
|
|
|
|
+
|
|
|
|
+ if (StringUtils.isBlank(apiKey)) {
|
|
|
|
+ log.error("'facepp.apiKey' is not configured");
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isBlank(apiSecret)) {
|
|
|
|
+ log.error("'facepp.apiSecret' is not configured");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 人脸识别
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param faceToken
|
|
|
|
+ * @param imageUrl
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JsonHttpResponseHolder compareWithTokenAndImageUrl(String faceToken, String imageUrl) {
|
|
|
|
+
|
|
|
|
+ String url = "https://api-cn.faceplusplus.com/facepp/v3/compare";
|
|
|
|
+
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ httpPost.setConfig(FaceppClient.requestConfig);
|
|
|
|
+
|
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
+ builder.addTextBody("api_key", apiKey);
|
|
|
|
+ builder.addTextBody("api_secret", apiSecret);
|
|
|
|
+ builder.addTextBody("face_token1", faceToken);
|
|
|
|
+ builder.addTextBody("image_url2", imageUrl);
|
|
|
|
+ HttpEntity httpEntity = builder.build();
|
|
|
|
+
|
|
|
|
+ httpPost.setEntity(httpEntity);
|
|
|
|
+
|
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ response = httpclient.execute(httpPost);
|
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
+ String entityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
|
+ JSONObject obj = JSON.parseObject(entityStr);
|
|
|
|
+ responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
|
+
|
|
|
|
+ if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
|
+ log.error("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
|
+ + entityStr);
|
|
|
|
+ } else {
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
|
+ + entityStr);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++]. compare. faceToken=" + faceToken + "; imageUrl=" + imageUrl
|
|
|
|
+ + "; cost " + (System.currentTimeMillis() - s) + " ms.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 人脸识别
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param faceToken
|
|
|
|
+ * @param imageUrl
|
|
|
|
+ * 图片url
|
|
|
|
+ * @param backupImageUrl
|
|
|
|
+ * 备用图片url
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JsonHttpResponseHolder compareWithTokenAndImageUrl(String faceToken, String imageUrl,
|
|
|
|
+ String backupImageUrl) {
|
|
|
|
+
|
|
|
|
+ JsonHttpResponseHolder responseHolder = compareWithTokenAndImageUrl(faceToken, imageUrl);
|
|
|
|
+
|
|
|
|
+ if (HttpStatus.SC_OK == responseHolder.getStatusCode()) {
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ JSONObject respBody = responseHolder.getRespBody();
|
|
|
|
+ String errMsg = respBody.getString("error_message");
|
|
|
|
+ if (errMsg.startsWith("")) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 人脸识别
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param imageUrl1
|
|
|
|
+ * @param imageUrl2
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public JsonHttpResponseHolder compareWithImageUrl(String imageUrl1, String imageUrl2) {
|
|
|
|
+
|
|
|
|
+ String url = "https://api-cn.faceplusplus.com/facepp/v3/compare";
|
|
|
|
+
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ httpPost.setConfig(FaceppClient.requestConfig);
|
|
|
|
+
|
|
|
|
+ MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
+ builder.addTextBody("api_key", apiKey);
|
|
|
|
+ builder.addTextBody("api_secret", apiSecret);
|
|
|
|
+ builder.addTextBody("image_url1", imageUrl1);
|
|
|
|
+ builder.addTextBody("image_url2", imageUrl2);
|
|
|
|
+ HttpEntity httpEntity = builder.build();
|
|
|
|
+
|
|
|
|
+ httpPost.setEntity(httpEntity);
|
|
|
|
+
|
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
|
+ try {
|
|
|
|
+
|
|
|
|
+ response = httpclient.execute(httpPost);
|
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
+ String entityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
|
+ JSONObject obj = JSON.parseObject(entityStr);
|
|
|
|
+ responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
|
+
|
|
|
|
+ if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
|
+ log.error("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
|
+ + entityStr);
|
|
|
|
+ } else {
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
|
+ + entityStr);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
|
+ log.debug("[Face++]. compare. image_url1=" + imageUrl1 + "; imageUrl2=" + imageUrl2
|
|
|
|
+ + "; cost " + (System.currentTimeMillis() - s) + " ms.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return responseHolder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|