Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtc #392

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

rtc #392

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/main/java/com/qiniu/face/FaceCompareManager.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/qiniu/face/FaceUploadManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.qiniu.face;

/**
* Created by jemy on 2018/7/11.
*/
public class FaceUploadManager {
}
3 changes: 1 addition & 2 deletions src/main/java/com/qiniu/rtc/RtcAppManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/qiniu/util/FaceUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.qiniu.util;

/**
* Created by jemy on 2018/6/6.
*/
public class FaceUtil {
}
124 changes: 124 additions & 0 deletions src/test/java/test/com/qiniu/face/FaceTest.java
Original file line number Diff line number Diff line change
@@ -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];
}
}