diff --git a/src/main/java/com/qiniu/face/FaceCompareManager.java b/src/main/java/com/qiniu/face/FaceCompareManager.java new file mode 100644 index 000000000..4fb914794 --- /dev/null +++ b/src/main/java/com/qiniu/face/FaceCompareManager.java @@ -0,0 +1,80 @@ +package com.qiniu.face; + +import com.qiniu.common.Constants; +import com.qiniu.common.QiniuException; +import com.qiniu.http.Client; +import com.qiniu.http.Response; +import com.qiniu.util.Auth; +import com.qiniu.util.StringMap; + +/** + * Created by jemy on 2018/6/6. + */ +public class FaceCompareManager { + private final Auth auth; + private final String host; + private final Client client; + + public FaceCompareManager(Auth auth) { + this(auth, "http://argus.atlab.ai"); + } + + FaceCompareManager(Auth auth, String host) { + this.auth = auth; + this.host = host; + client = new Client(); + } + + /** + * + * @param id face_id + * @param data you can open url: https://developer.qiniu.com/dora/manual/4438/face-recognition + * @return + * @throws QiniuException + */ + public Response createFaceDB(String id, String data) throws QiniuException { + String url = String.format("%s%s%s%s", host, "/v1/face/group/", id, "/new"); + byte[] body = data.getBytes(Constants.UTF_8); + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); + return client.post(url, body, headers, Client.JsonMime); + } + + public Response deleteFaceDB(String id) throws QiniuException { + String url = String.format("%s%s%s%s", host, "/v1/face/group/", id, "/remove"); + StringMap headers = auth.authorizationV2(url, "POST", null, null); + return client.post(url, null, headers, Client.JsonMime); + } + + public Response listFaceDB() throws QiniuException { + String url = String.format("%s%s", host, "/v1/face/group"); + StringMap headers = auth.authorizationV2(url); + return client.get(url, headers); + } + + public Response createFace(String id, String data) throws QiniuException { + String url = String.format("%s%s%s%s", host, "/v1/face/group/", id, "/add"); + byte[] body = data.getBytes(Constants.UTF_8); + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); + return client.post(url, body, headers, Client.JsonMime); + } + + public Response getFace(String id) throws QiniuException { + String url = String.format("%s%s%s", host, "/v1/face/group/", id); + StringMap headers = auth.authorizationV2(url); + return client.get(url, headers); + } + + public Response deleteFace(String id, String data) throws QiniuException { + String url = String.format("%s%s%s%s", host, "/v1/face/group/", id, "/delete"); + byte[] body = data.getBytes(Constants.UTF_8); + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); + return client.post(url, body, headers, Client.JsonMime); + } + + public Response compareFace(String id, String data) throws QiniuException { + String url = String.format("%s%s%s%s", host, "/v1/face/group/", id, "/search"); + byte[] body = data.getBytes(Constants.UTF_8); + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); + return client.post(url, body, headers, Client.JsonMime); + } +} diff --git a/src/main/java/com/qiniu/face/FaceUploadManager.java b/src/main/java/com/qiniu/face/FaceUploadManager.java new file mode 100644 index 000000000..814488f57 --- /dev/null +++ b/src/main/java/com/qiniu/face/FaceUploadManager.java @@ -0,0 +1,7 @@ +package com.qiniu.face; + +/** + * Created by jemy on 2018/7/11. + */ +public class FaceUploadManager { +} diff --git a/src/main/java/com/qiniu/rtc/RtcAppManager.java b/src/main/java/com/qiniu/rtc/RtcAppManager.java index c2de0a896..599697758 100644 --- a/src/main/java/com/qiniu/rtc/RtcAppManager.java +++ b/src/main/java/com/qiniu/rtc/RtcAppManager.java @@ -51,11 +51,10 @@ public Response createApp(String hub, String title, int maxUsers, if (title != null) { params.put("title", title); } - if (hub != null) { + if (maxUsers > 0) { params.put("maxUsers", maxUsers); } params.put("noAutoKickUser", noAutoKickUser); - String url = String.format("%s%s", host, "/v3/apps"); byte[] body = Json.encode(params).getBytes(Constants.UTF_8); StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); diff --git a/src/main/java/com/qiniu/util/FaceUtil.java b/src/main/java/com/qiniu/util/FaceUtil.java new file mode 100644 index 000000000..9d433fd6f --- /dev/null +++ b/src/main/java/com/qiniu/util/FaceUtil.java @@ -0,0 +1,7 @@ +package com.qiniu.util; + +/** + * Created by jemy on 2018/6/6. + */ +public class FaceUtil { +} diff --git a/src/test/java/test/com/qiniu/face/FaceTest.java b/src/test/java/test/com/qiniu/face/FaceTest.java new file mode 100644 index 000000000..9321c2a68 --- /dev/null +++ b/src/test/java/test/com/qiniu/face/FaceTest.java @@ -0,0 +1,124 @@ +package test.com.qiniu.face; + +import com.qiniu.common.QiniuException; +import com.qiniu.face.FaceCompareManager; +import com.qiniu.http.Response; +import com.qiniu.util.Auth; +import org.junit.Test; +import test.com.qiniu.TestConfig; + +import java.util.Date; + +/** + * Created by jemy on 2018/6/6. + */ +public class FaceTest { + private String ak = "fae7Nl_OO2V6jKwIhZd"; //AccessKey you get from qiniu + private String sk = "-Dx41TwHW2lMJSO0cu2UHyr"; //SecretKey you get from qiniu + private Auth auth = null; + + { + try { + auth = Auth.create(ak, sk); + } catch (Exception e) { + auth = TestConfig.testAuth; + } + } + + private FaceCompareManager face = new FaceCompareManager(auth); + + //创建人脸库 + @Test + public void createFaceDb() { + try { + String data = "{\"data\": [{\"uri\": \"\", \"attribute\": {\"name\": \"zw001\"}}]}"; + Response response = face.createFaceDB("face01", data); + System.out.print(response.getInfo()); + } catch (QiniuException e) { + e.printStackTrace(); + } + } + + //查询人脸库 + @Test + public void listFaceDB() { + Response response = null; + try { + response = face.listFaceDB(); + } catch (QiniuException e) { + e.printStackTrace(); + } + System.out.print(getString(response)); + } + + //删除人脸库 + @Test + public void deleteFaceDB() { + try { + Response response = face.deleteFaceDB("zwdb06"); + System.out.print(getString(response)); + } catch (QiniuException e) { + e.printStackTrace(); + } + } + + //添加人脸 + @Test + public void createFace() { + try { + Long start = new Date().getTime(); + String data = "{\"data\": [{\"uri\": \"http://1.bkt.clouddn.com/FgoeFtPzutwUClK4jaR28BXGMA_D\", \"attribute\": {\"name\": \"zw004\"}}]}"; + Response response = face.createFace("zwdb02", data); + Long end = new Date().getTime() - start; + System.out.println(end); + System.out.print(getString(response)); + } catch (QiniuException e) { + e.printStackTrace(); + } + } + + + //查询所有人脸 + @Test + public void getFaceAll() { + try { + Response response = face.getFace("face01"); + System.out.print(getString(response)); + } catch (QiniuException e) { + e.printStackTrace(); + } + } + + //删除人脸 + @Test + public void deleteFace() { + try { + String str = "{\"faces\":[\"BwAAAJ5WNym22TUV\"]}"; + Response response = face.deleteFace("zwdb04", str); + System.out.print(getString(response)); + } catch (QiniuException e) { + e.printStackTrace(); + } + + } + + //人脸检索 + @Test + public void compareFace() { + try { + Long start = new Date().getTime(); + String str = "{\"data\":{\"uri\":\"http://p6c0z9zl1.bkt.clouddn.com/Fo9_5Ejmu9Wnu4yxTkFRxTTZqUGM\"}}"; + Response response = face.compareFace("zwdb01", str); + Long end = new Date().getTime() - start; + System.out.println(end); + System.out.print(getString(response)); + } catch (QiniuException e) { + e.printStackTrace(); + } + } + + private String getString(Response response) { + String[] resJson = response.getInfo().split("\n"); + return response.statusCode + "\n" + resJson[2]; + } +}