diff --git a/Demo/FaceppAndroidDemo/.classpath b/Demo/FaceppAndroidDemo/.classpath index a4763d1..7bc01d9 100644 --- a/Demo/FaceppAndroidDemo/.classpath +++ b/Demo/FaceppAndroidDemo/.classpath @@ -3,6 +3,7 @@ - + + diff --git a/Demo/FaceppAndroidDemo/.gitignore b/Demo/FaceppAndroidDemo/.gitignore new file mode 100644 index 0000000..49b89c8 --- /dev/null +++ b/Demo/FaceppAndroidDemo/.gitignore @@ -0,0 +1,166 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store +*.class + +bin \ No newline at end of file diff --git a/Demo/FaceppAndroidDemo/libs/facepp_without_source_code.jar b/Demo/FaceppAndroidDemo/libs/faceppsdk_min.jar similarity index 64% rename from Demo/FaceppAndroidDemo/libs/facepp_without_source_code.jar rename to Demo/FaceppAndroidDemo/libs/faceppsdk_min.jar index d89aaac..d84b255 100644 Binary files a/Demo/FaceppAndroidDemo/libs/facepp_without_source_code.jar and b/Demo/FaceppAndroidDemo/libs/faceppsdk_min.jar differ diff --git a/Demo/FaceppAndroidDemo/src/com/facepp/demo/MainActivity.java b/Demo/FaceppAndroidDemo/src/com/facepp/demo/MainActivity.java index 93b6ae4..0547ba3 100644 --- a/Demo/FaceppAndroidDemo/src/com/facepp/demo/MainActivity.java +++ b/Demo/FaceppAndroidDemo/src/com/facepp/demo/MainActivity.java @@ -1,18 +1,18 @@ package com.facepp.demo; -import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; -import com.facepp.error.FaceppParseException; -import com.facepp.http.HttpRequests; -import com.facepp.http.PostParameters; -import com.facepp.result.FaceppResult; +import org.json.JSONException; +import org.json.JSONObject; -import android.os.Bundle; import android.app.Activity; +import android.os.Bundle; import android.view.Menu; -import android.view.MenuItem; -import android.support.v4.app.NavUtils; + +import com.facepp.error.FaceppParseException; +import com.facepp.http.HttpRequests; +import com.facepp.http.PostParameters; /** * Facepp SDK Android test @@ -29,84 +29,128 @@ public void onCreate(Bundle savedInstanceState) { new Thread(new Runnable() { public void run() { - HttpRequests httpRequests = new HttpRequests("api_key", "api_secret"); + //replace api_key and api_secret here (note) + HttpRequests httpRequests = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M", true, true); - FaceppResult result = null; + JSONObject result = null; try { + + System.out.println(Charset.forName("UTF-8").name()); System.out.println("FacePlusPlus API Test:"); //detection/detect - result = httpRequests.detectionDetect(new PostParameters().setUrl("http://faceplusplus.com/static/img/demo/20.jpg")); + result = httpRequests.detectionDetect(new PostParameters().setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/9.jpg")); System.out.println(result); + System.out.println(result.getJSONArray("face").getJSONObject(0).getJSONObject("position").getJSONObject("center")); + //-----------------Person----------------- //person/create System.out.println("\nperson/create"); - for (int i = 0; i < result.get("face").getCount(); ++i) + for (int i = 0; i < result.getJSONArray("face").length(); ++i) System.out.println(httpRequests.personCreate(new PostParameters().setPersonName("person_"+i))); + new PostParameters().setPersonName("person_"+0).setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")).getMultiPart().writeTo(System.out); + //person/add_face System.out.println("\nperson/add_face"); - for (int i = 0; i < result.get("face").getCount(); ++i) - System.out.println(httpRequests.personAddFace(new PostParameters().setPersonName("person_"+i).setFaceId(result.get("face").get(i).get("face_id").toString()))); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.personAddFace(new PostParameters().setPersonName("person_"+i).setFaceId( + result.getJSONArray("face").getJSONObject(i).getString("face_id")))); //person/set_info System.out.println("\nperson/set_info"); - for (int i = 0; i < result.get("face").getCount(); ++i) - System.out.println(httpRequests.personSetInfo(new PostParameters().setPersonName("person_"+i).setTag("tag_"+i))); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) { + new PostParameters().setPersonName("person_"+i).setTag("���� tag_"+i).getMultiPart().writeTo(System.out); + System.out.println(httpRequests.personSetInfo(new PostParameters().setPersonName("person_"+i).setTag("���� tag_"+i))); + } //person/get_info System.out.println("\nperson/get_info"); - for (int i = 0; i < result.get("face").getCount(); ++i) + for (int i = 0; i < result.getJSONArray("face").length(); ++i) System.out.println(httpRequests.personGetInfo(new PostParameters().setPersonName("person_"+i))); + //-----------------Faceset----------------- + //faceset/create + System.out.println("\nfaceset/create"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.facesetCreate(new PostParameters().setFacesetName("faceset_"+i))); + + //faceset/add_face + System.out.println("\nfaceset/add_face"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.facesetAddFace(new PostParameters().setFacesetName("faceset_"+i).setFaceId( + result.getJSONArray("face").getJSONObject(i).getString("face_id")))); + + //faceset/set_info + System.out.println("\nfaceset/set_info"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) { + new PostParameters().setFacesetName("faceset_"+i).setTag("���� tag_"+i).getMultiPart().writeTo(System.out); + System.out.println(httpRequests.facesetSetInfo(new PostParameters().setFacesetName("faceset_"+i).setTag("���� tag_"+i))); + } + + //faceset/get_info + System.out.println("\nfaceset/get_info"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.facesetGetInfo(new PostParameters().setFacesetName("faceset_"+i))); + //-----------------Group----------------- //group/create System.out.println("\ngroup/create"); - System.out.println(httpRequests.groupCreate(new PostParameters().setGroupName("group_test"))); + System.out.println(httpRequests.groupCreate(new PostParameters().setGroupName("group_0"))); //group/add_person System.out.println("\ngroup/add_person"); ArrayList personList = new ArrayList(); - for (int i = 0; i < result.get("face").getCount(); ++i) + for (int i = 0; i < result.getJSONArray("face").length(); ++i) personList.add("person_"+i); - System.out.println(httpRequests.groupAddPerson(new PostParameters().setGroupName("group_test").setPersonName(personList))); + + new PostParameters().setGroupName("group_0").setPersonName(personList).getMultiPart().writeTo(System.out); + System.out.println(httpRequests.groupAddPerson(new PostParameters().setGroupName("group_0").setPersonName(personList))); //group/set_info System.out.println("\ngroup/set_info"); - System.out.println(httpRequests.groupSetInfo(new PostParameters().setGroupName("group_test").setTag("group tag"))); + System.out.println(httpRequests.groupSetInfo(new PostParameters().setGroupName("group_0").setTag("group tag"))); //group/get_info System.out.println("\ngroup/get_info"); - System.out.println(httpRequests.groupGetInfo(new PostParameters().setGroupName("group_test"))); + System.out.println(httpRequests.groupGetInfo(new PostParameters().setGroupName("group_0"))); //-----------------Recognition----------------- - //recognition/compare - System.out.println("\nrecognition/compare"); - System.out.println(httpRequests.recognitionCompare( - new PostParameters().setFaceId1(result.get("face").get(0).get("face_id").toString()).setFaceId2(result.get("face").get(1).get("face_id").toString()))); //recognition/train - System.out.println("\nrecognition/train"); - System.out.println(httpRequests.train(new PostParameters().setGroupName("group_test").setType("all"))); + JSONObject syncRet = null; - //recognition/recognize - System.out.println("\nrecognition/recognize"); - System.out.println(httpRequests.recognitionRecognize(new PostParameters().setGroupName("group_test").setUrl("http://faceplusplus.com/static/img/demo/20.jpg"))); + System.out.println("\ntrain/Identify"); + syncRet = httpRequests.trainIdentify(new PostParameters().setGroupName("group_0")); + System.out.println(syncRet); + System.out.println(httpRequests.getSessionSync(syncRet.getString("session_id"))); - //recognition/search - System.out.println("\nrecognition/search"); - System.out.println(httpRequests.recognitionSearch(new PostParameters().setGroupName("group_test").setKeyFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println("\ntrain/verify"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) { + syncRet = httpRequests.trainVerify(new PostParameters().setPersonName("person_" + i)); + System.out.println(httpRequests.getSessionSync(syncRet.get("session_id").toString())); + } + + //recognition/recognize + System.out.println("\nrecognition/identify"); + System.out.println(httpRequests.recognitionIdentify( + new PostParameters().setGroupName("group_0").setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/5.jpg"))); //recognition/verify System.out.println("\nrecognition/verify"); - System.out.println(httpRequests.recognitionVerify(new PostParameters().setPersonName("person_0").setFaceId(result.get("face").get(0).get("face_id").toString()))); - System.out.println(httpRequests.recognitionVerify(new PostParameters().setPersonName("person_1").setFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println( + httpRequests.recognitionVerify(new PostParameters().setPersonName("person_0").setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); + System.out.println( + httpRequests.recognitionVerify(new PostParameters().setPersonName("person_1").setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); //-----------------Info----------------- //info/get_app @@ -115,7 +159,8 @@ public void run() { //info/get_face System.out.println("\ninfo/get_app"); - System.out.println(httpRequests.infoGetFace(new PostParameters().setFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println(httpRequests.infoGetFace(new PostParameters().setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); //info/get_group_list System.out.println("\ninfo/get_group_list"); @@ -123,7 +168,8 @@ public void run() { //info/get_image System.out.println("\ninfo/get_image"); - System.out.println(httpRequests.infoGetImage(new PostParameters().setImgId(result.get("img_id").toString()))); + System.out.println(httpRequests.infoGetImage(new PostParameters().setImgId( + result.getString("img_id")))); //info/get_person_list System.out.println("\ninfo/get_person_list"); @@ -135,55 +181,44 @@ public void run() { //info/get_session System.out.println("\ninfo/get_session"); - System.out.println(httpRequests.infoGetSession(new PostParameters().setSessionId(result.get("session_id").toString()))); + System.out.println(httpRequests.infoGetSession(new PostParameters().setSessionId( + result.getString("session_id")))); //-----At last---- //person/remove_face System.out.println("\nperson/remove_face"); - System.out.println(httpRequests.personRemoveFace(new PostParameters().setPersonName("person_0").setFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println(httpRequests.personRemoveFace( + new PostParameters().setPersonName("person_0").setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); //group/delete System.out.println("\ngroup/delete"); - System.out.println(httpRequests.groupDelete(new PostParameters().setGroupName("group_test"))); + System.out.println(httpRequests.groupDelete(new PostParameters().setGroupName("group_0"))); //person/delete System.out.println("\nperson/delete"); System.out.println(httpRequests.personDelete(new PostParameters().setPersonName("person_0"))); + //faceset/delete + System.out.println("\nfaceset/delete"); + System.out.println(httpRequests.facesetDelete(new PostParameters().setFacesetName("faceset_0"))); + } catch(FaceppParseException e) { e.printStackTrace(); } catch (Exception e) { } finally { - - //delete all person try { - for (int i = 1; i < result.get("face").getCount(); ++i) { - try { - httpRequests.personDelete(new PostParameters().setPersonName("person_"+i)); - } catch (FaceppParseException e) { - e.printStackTrace(); - } + for (int i = 1; i < result.getJSONArray("face").length(); ++i) { + httpRequests.personDelete(new PostParameters().setPersonName("person_"+i)); + httpRequests.facesetDelete(new PostParameters().setFacesetName("faceset_"+i)); } + } catch (JSONException e) { + e.printStackTrace(); } catch (FaceppParseException e) { e.printStackTrace(); - } catch (Exception e2) { - e2.printStackTrace(); } } - - try { - //-----Debug---- - //How to watch what's the http post sent? - //If we have a PostParameters object, we can do .getMultiPart().wrtieTo(outputStream) to watch the message. - System.out.println("\nDebug"); - System.out.println("=========message========="); - PostParameters postParameters = new PostParameters().setPersonName("a person").setGroupName("a group"); - postParameters.getMultiPart().writeTo(System.out); - System.out.println("=========message========="); - } catch (IOException e) { - e.printStackTrace(); - } } }).start(); } diff --git a/Demo/FaceppDemo/.classpath b/Demo/FaceppDemo/.classpath index 0870a20..d0fbdbb 100644 --- a/Demo/FaceppDemo/.classpath +++ b/Demo/FaceppDemo/.classpath @@ -2,6 +2,6 @@ - + diff --git a/Demo/FaceppDemo/.gitignore b/Demo/FaceppDemo/.gitignore new file mode 100644 index 0000000..49b89c8 --- /dev/null +++ b/Demo/FaceppDemo/.gitignore @@ -0,0 +1,166 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store +*.class + +bin \ No newline at end of file diff --git a/Demo/FaceppDemo/libs/facepp_without_source_code.jar b/Demo/FaceppDemo/libs/faceppsdk_min.jar similarity index 64% rename from Demo/FaceppDemo/libs/facepp_without_source_code.jar rename to Demo/FaceppDemo/libs/faceppsdk_min.jar index d89aaac..d84b255 100644 Binary files a/Demo/FaceppDemo/libs/facepp_without_source_code.jar and b/Demo/FaceppDemo/libs/faceppsdk_min.jar differ diff --git a/Demo/FaceppDemo/src/Test.java b/Demo/FaceppDemo/src/Test.java index 02ea2f8..7403f37 100644 --- a/Demo/FaceppDemo/src/Test.java +++ b/Demo/FaceppDemo/src/Test.java @@ -1,98 +1,139 @@ -import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; +import org.json.JSONException; +import org.json.JSONObject; + import com.facepp.error.FaceppParseException; import com.facepp.http.HttpRequests; import com.facepp.http.PostParameters; -import com.facepp.result.FaceppResult; -/** - * test facepp java sdk - * FaceppDemo/Test.java - * @author moon5ckq - */ + public class Test { -public static void main(String[] args) { - + + public static void main(String[] args) { //replace api_key and api_secret here (note) - HttpRequests httpRequests = new HttpRequests("api_key", "api_secret"); + HttpRequests httpRequests = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M ", true, true); - FaceppResult result = null; + JSONObject result = null; try { + + System.out.println(Charset.forName("UTF-8").name()); System.out.println("FacePlusPlus API Test:"); //detection/detect - result = httpRequests.detectionDetect(new PostParameters().setUrl("http://faceplusplus.com/static/img/demo/20.jpg")); + result = httpRequests.detectionDetect(new PostParameters().setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/9.jpg")); System.out.println(result); + System.out.println(result.getJSONArray("face").getJSONObject(0).getJSONObject("position").getJSONObject("center")); + //-----------------Person----------------- //person/create System.out.println("\nperson/create"); - for (int i = 0; i < result.get("face").getCount(); ++i) + for (int i = 0; i < result.getJSONArray("face").length(); ++i) System.out.println(httpRequests.personCreate(new PostParameters().setPersonName("person_"+i))); + new PostParameters().setPersonName("person_"+0).setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")).getMultiPart().writeTo(System.out); + //person/add_face System.out.println("\nperson/add_face"); - for (int i = 0; i < result.get("face").getCount(); ++i) - System.out.println(httpRequests.personAddFace(new PostParameters().setPersonName("person_"+i).setFaceId(result.get("face").get(i).get("face_id").toString()))); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.personAddFace(new PostParameters().setPersonName("person_"+i).setFaceId( + result.getJSONArray("face").getJSONObject(i).getString("face_id")))); //person/set_info System.out.println("\nperson/set_info"); - for (int i = 0; i < result.get("face").getCount(); ++i) - System.out.println(httpRequests.personSetInfo(new PostParameters().setPersonName("person_"+i).setTag("tag_"+i))); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) { + new PostParameters().setPersonName("person_"+i).setTag("中文 tag_"+i).getMultiPart().writeTo(System.out); + System.out.println(httpRequests.personSetInfo(new PostParameters().setPersonName("person_"+i).setTag("中文 tag_"+i))); + } //person/get_info System.out.println("\nperson/get_info"); - for (int i = 0; i < result.get("face").getCount(); ++i) + for (int i = 0; i < result.getJSONArray("face").length(); ++i) System.out.println(httpRequests.personGetInfo(new PostParameters().setPersonName("person_"+i))); + //-----------------Faceset----------------- + //faceset/create + System.out.println("\nfaceset/create"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.facesetCreate(new PostParameters().setFacesetName("faceset_"+i))); + + //faceset/add_face + System.out.println("\nfaceset/add_face"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.facesetAddFace(new PostParameters().setFacesetName("faceset_"+i).setFaceId( + result.getJSONArray("face").getJSONObject(i).getString("face_id")))); + + //faceset/set_info + System.out.println("\nfaceset/set_info"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) { + new PostParameters().setFacesetName("faceset_"+i).setTag("中文 tag_"+i).getMultiPart().writeTo(System.out); + System.out.println(httpRequests.facesetSetInfo(new PostParameters().setFacesetName("faceset_"+i).setTag("中文 tag_"+i))); + } + + //faceset/get_info + System.out.println("\nfaceset/get_info"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) + System.out.println(httpRequests.facesetGetInfo(new PostParameters().setFacesetName("faceset_"+i))); + //-----------------Group----------------- //group/create System.out.println("\ngroup/create"); - System.out.println(httpRequests.groupCreate(new PostParameters().setGroupName("group_test"))); + System.out.println(httpRequests.groupCreate(new PostParameters().setGroupName("group_0"))); //group/add_person System.out.println("\ngroup/add_person"); ArrayList personList = new ArrayList(); - for (int i = 0; i < result.get("face").getCount(); ++i) + for (int i = 0; i < result.getJSONArray("face").length(); ++i) personList.add("person_"+i); - System.out.println(httpRequests.groupAddPerson(new PostParameters().setGroupName("group_test").setPersonName(personList))); + + new PostParameters().setGroupName("group_0").setPersonName(personList).getMultiPart().writeTo(System.out); + System.out.println(httpRequests.groupAddPerson(new PostParameters().setGroupName("group_0").setPersonName(personList))); //group/set_info System.out.println("\ngroup/set_info"); - System.out.println(httpRequests.groupSetInfo(new PostParameters().setGroupName("group_test").setTag("group tag"))); + System.out.println(httpRequests.groupSetInfo(new PostParameters().setGroupName("group_0").setTag("group tag"))); //group/get_info System.out.println("\ngroup/get_info"); - System.out.println(httpRequests.groupGetInfo(new PostParameters().setGroupName("group_test"))); + System.out.println(httpRequests.groupGetInfo(new PostParameters().setGroupName("group_0"))); //-----------------Recognition----------------- - //recognition/compare - System.out.println("\nrecognition/compare"); - System.out.println(httpRequests.recognitionCompare( - new PostParameters().setFaceId1(result.get("face").get(0).get("face_id").toString()).setFaceId2(result.get("face").get(1).get("face_id").toString()))); //recognition/train - System.out.println("\nrecognition/train"); - System.out.println(httpRequests.train(new PostParameters().setGroupName("group_test").setType("all"))); + JSONObject syncRet = null; - //recognition/recognize - System.out.println("\nrecognition/recognize"); - System.out.println(httpRequests.recognitionRecognize(new PostParameters().setGroupName("group_test").setUrl("http://faceplusplus.com/static/img/demo/20.jpg"))); + System.out.println("\ntrain/Identify"); + syncRet = httpRequests.trainIdentify(new PostParameters().setGroupName("group_0")); + System.out.println(syncRet); + System.out.println(httpRequests.getSessionSync(syncRet.getString("session_id"))); - //recognition/search - System.out.println("\nrecognition/search"); - System.out.println(httpRequests.recognitionSearch(new PostParameters().setGroupName("group_test").setKeyFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println("\ntrain/verify"); + for (int i = 0; i < result.getJSONArray("face").length(); ++i) { + syncRet = httpRequests.trainVerify(new PostParameters().setPersonName("person_" + i)); + System.out.println(httpRequests.getSessionSync(syncRet.get("session_id").toString())); + } + + //recognition/recognize + System.out.println("\nrecognition/identify"); + System.out.println(httpRequests.recognitionIdentify( + new PostParameters().setGroupName("group_0").setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus/assets/img/demo/5.jpg"))); //recognition/verify System.out.println("\nrecognition/verify"); - System.out.println(httpRequests.recognitionVerify(new PostParameters().setPersonName("person_0").setFaceId(result.get("face").get(0).get("face_id").toString()))); - System.out.println(httpRequests.recognitionVerify(new PostParameters().setPersonName("person_1").setFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println( + httpRequests.recognitionVerify(new PostParameters().setPersonName("person_0").setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); + System.out.println( + httpRequests.recognitionVerify(new PostParameters().setPersonName("person_1").setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); //-----------------Info----------------- //info/get_app @@ -101,7 +142,8 @@ public static void main(String[] args) { //info/get_face System.out.println("\ninfo/get_app"); - System.out.println(httpRequests.infoGetFace(new PostParameters().setFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println(httpRequests.infoGetFace(new PostParameters().setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); //info/get_group_list System.out.println("\ninfo/get_group_list"); @@ -109,7 +151,8 @@ public static void main(String[] args) { //info/get_image System.out.println("\ninfo/get_image"); - System.out.println(httpRequests.infoGetImage(new PostParameters().setImgId(result.get("img_id").toString()))); + System.out.println(httpRequests.infoGetImage(new PostParameters().setImgId( + result.getString("img_id")))); //info/get_person_list System.out.println("\ninfo/get_person_list"); @@ -121,55 +164,43 @@ public static void main(String[] args) { //info/get_session System.out.println("\ninfo/get_session"); - System.out.println(httpRequests.infoGetSession(new PostParameters().setSessionId(result.get("session_id").toString()))); + System.out.println(httpRequests.infoGetSession(new PostParameters().setSessionId( + result.getString("session_id")))); //-----At last---- //person/remove_face System.out.println("\nperson/remove_face"); - System.out.println(httpRequests.personRemoveFace(new PostParameters().setPersonName("person_0").setFaceId(result.get("face").get(0).get("face_id").toString()))); + System.out.println(httpRequests.personRemoveFace( + new PostParameters().setPersonName("person_0").setFaceId( + result.getJSONArray("face").getJSONObject(0).getString("face_id")))); //group/delete System.out.println("\ngroup/delete"); - System.out.println(httpRequests.groupDelete(new PostParameters().setGroupName("group_test"))); + System.out.println(httpRequests.groupDelete(new PostParameters().setGroupName("group_0"))); //person/delete System.out.println("\nperson/delete"); System.out.println(httpRequests.personDelete(new PostParameters().setPersonName("person_0"))); + //faceset/delete + System.out.println("\nfaceset/delete"); + System.out.println(httpRequests.facesetDelete(new PostParameters().setFacesetName("faceset_0"))); + } catch(FaceppParseException e) { e.printStackTrace(); } catch (Exception e) { } finally { - - //delete all person try { - for (int i = 1; i < result.get("face").getCount(); ++i) { - try { - httpRequests.personDelete(new PostParameters().setPersonName("person_"+i)); - } catch (FaceppParseException e) { - e.printStackTrace(); - } + for (int i = 1; i < result.getJSONArray("face").length(); ++i) { + httpRequests.personDelete(new PostParameters().setPersonName("person_"+i)); + httpRequests.facesetDelete(new PostParameters().setFacesetName("faceset_"+i)); } + } catch (JSONException e) { + e.printStackTrace(); } catch (FaceppParseException e) { e.printStackTrace(); } } - - - try { - //-----Debug---- - //How to watch what's the http post sent? - //If we have a PostParameters object, we can do .getMultiPart().wrtieTo(outputStream) to watch the message. - System.out.println("\nDebug"); - System.out.println("=========message========="); - PostParameters postParameters = new PostParameters().setPersonName("a person").setGroupName("a group"); - postParameters.getMultiPart().writeTo(System.out); - System.out.println("=========message========="); - } catch (IOException e) { - e.printStackTrace(); - } - } - } diff --git a/Demo/PictureDetect/.classpath b/Demo/PictureDetect/.classpath index a4763d1..7bc01d9 100644 --- a/Demo/PictureDetect/.classpath +++ b/Demo/PictureDetect/.classpath @@ -3,6 +3,7 @@ - + + diff --git a/Demo/PictureDetect/.gitignore b/Demo/PictureDetect/.gitignore new file mode 100644 index 0000000..49b89c8 --- /dev/null +++ b/Demo/PictureDetect/.gitignore @@ -0,0 +1,166 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store +*.class + +bin \ No newline at end of file diff --git a/Demo/PictureDetect/gen/com/facpp/picturedetect/R.java b/Demo/PictureDetect/gen/com/facpp/picturedetect/R.java index 82fd362..6009973 100644 --- a/Demo/PictureDetect/gen/com/facpp/picturedetect/R.java +++ b/Demo/PictureDetect/gen/com/facpp/picturedetect/R.java @@ -17,8 +17,6 @@ public static final class dimen { } public static final class drawable { public static final int facepp_icon=0x7f020000; - public static final int ic_action_search=0x7f020001; - public static final int ic_launcher=0x7f020002; } public static final class id { public static final int button1=0x7f080000; diff --git a/Demo/PictureDetect/libs/facepp_without_source_code.jar b/Demo/PictureDetect/libs/faceppsdk_min.jar similarity index 64% rename from Demo/PictureDetect/libs/facepp_without_source_code.jar rename to Demo/PictureDetect/libs/faceppsdk_min.jar index d89aaac..d84b255 100644 Binary files a/Demo/PictureDetect/libs/facepp_without_source_code.jar and b/Demo/PictureDetect/libs/faceppsdk_min.jar differ diff --git a/Demo/PictureDetect/src/com/facpp/picturedetect/MainActivity.java b/Demo/PictureDetect/src/com/facpp/picturedetect/MainActivity.java index 390484b..298f03a 100644 --- a/Demo/PictureDetect/src/com/facpp/picturedetect/MainActivity.java +++ b/Demo/PictureDetect/src/com/facpp/picturedetect/MainActivity.java @@ -2,6 +2,9 @@ import java.io.ByteArrayOutputStream; +import org.json.JSONException; +import org.json.JSONObject; + import android.app.Activity; import android.content.Intent; import android.database.Cursor; @@ -25,7 +28,6 @@ import com.facepp.error.FaceppParseException; import com.facepp.http.HttpRequests; import com.facepp.http.PostParameters; -import com.facepp.result.FaceppResult; /** * A simple demo, get a picture form your phone
@@ -72,7 +74,7 @@ public void onClick(View arg0) { FaceppDetect faceppDetect = new FaceppDetect(); faceppDetect.setDetectCallback(new DetectCallback() { - public void detectResult(FaceppResult rst) { + public void detectResult(JSONObject rst) { //Log.v(TAG, rst.toString()); //use the red paint @@ -88,16 +90,20 @@ public void detectResult(FaceppResult rst) { try { //find out all faces - final int count = rst.get("face").getCount(); + final int count = rst.getJSONArray("face").length(); for (int i = 0; i < count; ++i) { float x, y, w, h; //get the center point - x = (float)rst.get("face").get(i).get("center").get("x").toDouble().doubleValue(); - y = (float)rst.get("face").get(i).get("center").get("y").toDouble().doubleValue(); + x = (float)rst.getJSONArray("face").getJSONObject(i) + .getJSONObject("position").getJSONObject("center").getDouble("x"); + y = (float)rst.getJSONArray("face").getJSONObject(i) + .getJSONObject("position").getJSONObject("center").getDouble("y"); //get face size - w = (float)rst.get("face").get(i).get("width").toDouble().doubleValue(); - h = (float)rst.get("face").get(i).get("height").toDouble().doubleValue(); + w = (float)rst.getJSONArray("face").getJSONObject(i) + .getJSONObject("position").getDouble("width"); + h = (float)rst.getJSONArray("face").getJSONObject(i) + .getJSONObject("position").getDouble("height"); //change percent value to the real size x = x / 100 * img.getWidth(); @@ -124,9 +130,8 @@ public void run() { } }); - } catch (FaceppParseException e) { + } catch (JSONException e) { e.printStackTrace(); - MainActivity.this.runOnUiThread(new Runnable() { public void run() { textView.setText("Error."); @@ -198,7 +203,7 @@ public void detect(final Bitmap image) { new Thread(new Runnable() { public void run() { - HttpRequests httpRequests = new HttpRequests("api_key", "api_secret"); + HttpRequests httpRequests = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M", true, false); //Log.v(TAG, "image size : " + img.getWidth() + " " + img.getHeight()); ByteArrayOutputStream stream = new ByteArrayOutputStream(); @@ -214,7 +219,7 @@ public void run() { try { //detect - FaceppResult result = httpRequests.detectionDetect(new PostParameters().setImg(array)); + JSONObject result = httpRequests.detectionDetect(new PostParameters().setImg(array)); //finished , then call the callback function if (callback != null) { callback.detectResult(result); @@ -234,6 +239,6 @@ public void run() { } interface DetectCallback { - void detectResult(FaceppResult rst); + void detectResult(JSONObject rst); } } diff --git a/FaceppSDK/.gitignore b/FaceppSDK/.gitignore new file mode 100644 index 0000000..93a4f26 --- /dev/null +++ b/FaceppSDK/.gitignore @@ -0,0 +1,168 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +[Dd]ebug/ +[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store +*.class + +bin +src/org +src/Test.java \ No newline at end of file diff --git a/FaceppSDK/bin/facepp.jar b/FaceppSDK/output/faceppsdk.jar similarity index 77% rename from FaceppSDK/bin/facepp.jar rename to FaceppSDK/output/faceppsdk.jar index af39894..70f51bf 100644 Binary files a/FaceppSDK/bin/facepp.jar and b/FaceppSDK/output/faceppsdk.jar differ diff --git a/FaceppSDK/bin/facepp_without_source_code.jar b/FaceppSDK/output/faceppsdk_min.jar similarity index 64% rename from FaceppSDK/bin/facepp_without_source_code.jar rename to FaceppSDK/output/faceppsdk_min.jar index d89aaac..d84b255 100644 Binary files a/FaceppSDK/bin/facepp_without_source_code.jar and b/FaceppSDK/output/faceppsdk_min.jar differ diff --git a/FaceppSDK/src/com/facepp/error/FaceppParseException.java b/FaceppSDK/src/com/facepp/error/FaceppParseException.java index 8fce843..3a14a75 100644 --- a/FaceppSDK/src/com/facepp/error/FaceppParseException.java +++ b/FaceppSDK/src/com/facepp/error/FaceppParseException.java @@ -1,5 +1,11 @@ package com.facepp.error; +/** + * exception about faceplusplus sdk + * @author moon5ckq + * @since 1.0.0 + * @version 1.0.0 + */ public class FaceppParseException extends Exception { private String errorMessage = null; diff --git a/FaceppSDK/src/com/facepp/http/HttpRequests.java b/FaceppSDK/src/com/facepp/http/HttpRequests.java index c758300..a6b5889 100644 --- a/FaceppSDK/src/com/facepp/http/HttpRequests.java +++ b/FaceppSDK/src/com/facepp/http/HttpRequests.java @@ -12,7 +12,6 @@ import org.json.JSONObject; import com.facepp.error.FaceppParseException; -import com.facepp.result.FaceppResult; /** * request to faceplusplus.com
@@ -21,15 +20,20 @@ * {@code new HttpRequests(apiKey, apiSecret).train()} * @author moon5ckq * @since 1.0.0 - * @version 1.2.0 + * @version 1.3.0 */ public class HttpRequests { - static private String WEBSITE = "https://api.faceplusplus.com/"; + static final private String WEBSITE_CN = "https://apicn.faceplusplus.com/v2/"; + static final private String DWEBSITE_CN = "http://apicn.faceplusplus.com/v2/"; + static final private String WEBSITE_US = "https://apius.faceplusplus.com/v2/"; + static final private String DWEBSITE_US = "http://apius.faceplusplus.com/v2/"; + static final private int BUFFERSIZE = 1048576; static final private int TIMEOUT = 30000; - static final private int TRAINTIMEOUT = 60000; + static final private int SYNC_TIMEOUT = 60000; + private String webSite; private String apiKey, apiSecret; private PostParameters params; private int httpTimeOut = TIMEOUT; @@ -79,6 +83,26 @@ public String getApiSecret() { public void setApiSecret(String apiSecret) { this.apiSecret = apiSecret; } + + /** + * if isCN is true, then use AliCloud, false to Amazon
+ * if isDebug is true, then use http, otherwise https + * @param isCN + * @param isDebug + */ + public void setWebSite(boolean isCN, boolean isDebug) { + if (isCN && isDebug) webSite = DWEBSITE_CN; + else if (isCN && !isDebug) webSite = WEBSITE_CN; + else if (!isCN && isDebug) webSite = DWEBSITE_US; + else if (!isCN && !isDebug) webSite = WEBSITE_US; + } + + /** + * @return a webSite clone + */ + public String getWebSite() { + return new String(webSite); + } /** * {@link #request(String, String, PostParameters)}
@@ -88,103 +112,155 @@ public void setApiSecret(String apiSecret) { * @param action * @return a result object */ - public FaceppResult request(String control, String action) throws FaceppParseException { + public JSONObject request(String control, String action) throws FaceppParseException { return request(control, action, getParams()); } +// /** +// * {@link #train(PostParameters, long)}
+// * default timeout time is 1 min, use parameters which {@link #getParams} +// * @return a result object +// * @throws FaceppParseException +// */ +// public FaceppResult train() throws FaceppParseException { +// return train(getParams(), TRAINTIMEOUT); +// } +// /** +// * {@link #train(PostParameters, long)}
+// * timeout time is [timeOut]ms, use parameters which {@link #getParams} +// * @param timeOut +// * @return a result object +// * @throws FaceppParseException +// */ +// public FaceppResult train(long timeOut) throws FaceppParseException { +// return train(getParams(), timeOut); +// } +// /** +// * {@link #train(PostParameters, long)}
+// * default timeout time is 5000ms +// * @param params +// * @return a result object +// * @throws FaceppParseException +// */ +// public FaceppResult train(PostParameters params) throws FaceppParseException { +// return train(params, TRAINTIMEOUT); +// } +// +// /** +// * timeout time is [timeOut]ms, the method is synchronized.
+// * If training is success, return the result about "/info/get_session?session_id=..".
+// * If timeout, then return a json object which "session_id = ...". +// * @param params +// * @param timeOut +// * @return a result object +// * @throws FaceppParseException +// */ +// public FaceppResult train(PostParameters params, long timeOut) throws FaceppParseException { +// +// final long timeOutFinal = timeOut; +// String sessionIdTmp = null; +// FaceppResult fj = null; +// try { +// fj = this.request("recognition", "train", params); +// sessionIdTmp = fj.get("session_id").toString(); +// } catch (FaceppParseException e) { +// if (fj.isError()) return fj; +// throw new FaceppParseException("Train error."); +// } +// final StringBuilder sb = new StringBuilder(); +// final String sessionId = sessionIdTmp; +// +// long t = new Date().getTime() + timeOutFinal; +// while (true) { +// FaceppResult rst = HttpRequests.this.request("info", "get_session", new PostParameters().setSessionId(sessionId)); +// try { +// if (rst.get("status").toString().equals("SUCC")) { +// sb.append(rst.toString()); +// break; +// } else if (rst.get("status").toString().equals("INVALID_SESSION")) { +// sb.append("INVALID_SESSION"); +// break; +// } +// } catch (FaceppParseException e) { +// sb.append("Unknow error."); +// break; +// } +// try { +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// sb.append("Thread.sleep error."); +// break; +// } +// if (new Date().getTime() >= t) { +// sb.append("Time Out"); +// break; +// } +// } +// +// String rst = sb.toString(); +// if (rst.equals("INVALID_SESSION")) { +// throw new FaceppParseException("Invaild session, unknow error."); +// } else if (rst.equals("Unknow error.")) { +// throw new FaceppParseException("Unknow error."); +// } else if (rst.equals("Thread.sleep error.")) { +// throw new FaceppParseException("Thread.sleep error."); +// } else if (rst.equals("Time Out")) { +// return fj; +// } else { +// try { +// return new FaceppResult(new JSONObject(rst), 200); +// } catch (JSONException e) { +// } +// } +// +// return null; +// } + /** - * {@link #train(PostParameters, long)}
- * default timeout time is 1 min, use parameters which {@link #getParams} - * @return a result object - * @throws FaceppParseException - */ - public FaceppResult train() throws FaceppParseException { - return train(getParams(), TRAINTIMEOUT); - } - /** - * {@link #train(PostParameters, long)}
- * timeout time is [timeOut]ms, use parameters which {@link #getParams} - * @param timeOut - * @return a result object - * @throws FaceppParseException - */ - public FaceppResult train(long timeOut) throws FaceppParseException { - return train(getParams(), timeOut); - } - /** - * {@link #train(PostParameters, long)}
- * default timeout time is 5000ms - * @param params - * @return a result object - * @throws FaceppParseException + * default timeout time is 1 minute + * @param sessionId + * @return the getSession Result + * @throws FaceppParseException */ - public FaceppResult train(PostParameters params) throws FaceppParseException { - return train(params, TRAINTIMEOUT); + public JSONObject getSessionSync(String sessionId) throws FaceppParseException { + return getSessionSync(sessionId, SYNC_TIMEOUT); } /** - * timeout time is [timeOut]ms, the method is synchronized.
- * If training is success, return the result about "/info/get_session?session_id=..".
- * If timeout, then return a json object which "session_id = ...". - * @param params + * timeout time is [timeOut]ms, the method is synchronized. + * @param sessionId * @param timeOut - * @return a result object + * @return the getSession Result * @throws FaceppParseException */ - public FaceppResult train(PostParameters params, long timeOut) throws FaceppParseException { - - final long timeOutFinal = timeOut; - String sessionIdTmp = null; - FaceppResult fj = null; - try { - fj = this.request("recognition", "train", params); - sessionIdTmp = fj.get("session_id").toString(); - } catch (FaceppParseException e) { - if (fj.isError()) return fj; - throw new FaceppParseException("Train error."); - } + public JSONObject getSessionSync(String sessionId, long timeOut) throws FaceppParseException { final StringBuilder sb = new StringBuilder(); - final String sessionId = sessionIdTmp; - -// Thread thread = new Thread(new Runnable() { -// -// @Override -// public void run() { - long t = new Date().getTime() + timeOutFinal; - while (true) { - FaceppResult rst = HttpRequests.this.request("info", "get_session", new PostParameters().setSessionId(sessionId)); - try { - if (rst.get("status").toString().equals("SUCC")) { - sb.append(rst.toString()); - break; - } else if (rst.get("status").toString().equals("INVALID_SESSION")) { - sb.append("INVALID_SESSION"); - break; - } - } catch (FaceppParseException e) { - sb.append("Unknow error."); - break; - } - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - sb.append("Thread.sleep error."); - break; - } - if (new Date().getTime() >= t) { - sb.append("Time Out"); - break; - } + long t = new Date().getTime() + timeOut; + while (true) { + JSONObject rst = HttpRequests.this.request("info", "get_session", new PostParameters().setSessionId(sessionId)); + try { + if (rst.getString("status").equals("SUCC")) { + sb.append(rst.toString()); + break; + } else if (rst.getString("status").equals("INVALID_SESSION")) { + sb.append("INVALID_SESSION"); + break; } -// } -// }); - -// thread.start(); -// try { -// thread.join(); -// } catch (InterruptedException e) { -// throw new FaceppParseException("Thread error."); -// } + } catch (JSONException e) { + sb.append("Unknow error."); + break; + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + sb.append("Thread.sleep error."); + break; + } + if (new Date().getTime() >= t) { + sb.append("Time Out"); + break; + } + } String rst = sb.toString(); if (rst.equals("INVALID_SESSION")) { @@ -194,10 +270,12 @@ public FaceppResult train(PostParameters params, long timeOut) throws FaceppPars } else if (rst.equals("Thread.sleep error.")) { throw new FaceppParseException("Thread.sleep error."); } else if (rst.equals("Time Out")) { - return fj; + throw new FaceppParseException("Get session time out."); } else { try { - return new FaceppResult(new JSONObject(rst), 200); + JSONObject result = new JSONObject(rst); + result.put("response_code", 200); + return result; } catch (JSONException e) { } } @@ -214,11 +292,11 @@ public FaceppResult train(PostParameters params, long timeOut) throws FaceppPars * @return a result object * @throws FaceppParseException */ - public FaceppResult request(String control, String action, PostParameters params) throws FaceppParseException { + public JSONObject request(String control, String action, PostParameters params) throws FaceppParseException { URL url; HttpURLConnection urlConn = null; try { - url = new URL(WEBSITE+control+"/"+action); + url = new URL(webSite+control+"/"+action); urlConn = (HttpURLConnection) url.openConnection(); urlConn.setRequestMethod("POST"); urlConn.setConnectTimeout(httpTimeOut); @@ -241,16 +319,26 @@ public FaceppResult request(String control, String action, PostParameters params else resultString = readString(urlConn.getErrorStream()); - FaceppResult result = new FaceppResult( new JSONObject(resultString), urlConn.getResponseCode()); + //FaceppResult result = new FaceppResult( new JSONObject(resultString), urlConn.getResponseCode()); + JSONObject result = new JSONObject(resultString); - if (result.isError()) - throw new FaceppParseException("API error.", result.getErrorCode(), result.getErrorMessage(), result.getHttpResponseCode()); + if (result.has("error")) { + + if (result.getString("error").equals("API not found")) + throw new FaceppParseException("API not found"); + + throw new FaceppParseException("API error.", result.getInt("error_code"), + result.getString("error"), urlConn.getResponseCode()); + } + result.put("response_code", urlConn.getResponseCode()); + urlConn.getInputStream().close(); return result; } catch (Exception e) { - throw new FaceppParseException("error : " + e.toString()); - //return new FaceppResult( new JSONObject("{\"error\":"+e.toString()+"}")); + throw new FaceppParseException("error :" + e.toString()); + } finally { + if (urlConn != null) + urlConn.disconnect(); } - } private static String readString(InputStream is) { @@ -273,7 +361,7 @@ private static String readString(InputStream is) { /** * create {@link HttpRequests}
* api_key=...,api_secret=...
- * use https default + * use https and AliCloud default * @param apiKey * @param apiSecret */ @@ -281,6 +369,7 @@ public HttpRequests(String apiKey, String apiSecret) { super(); this.apiKey = apiKey; this.apiSecret = apiSecret; + this.webSite = WEBSITE_CN; } /** @@ -294,24 +383,17 @@ public HttpRequests() { /** * create {@link HttpRequests}
* api_key=...,api_secret=...
- * if debug is `true', use http instead of https + * the isCN and isDebug use like {@link setWebSite}} * @param apiKey * @param apiSecret + * @param isCN + * @param isDebug */ - public HttpRequests(String apiKey, String apiSecret, boolean debug) { + public HttpRequests(String apiKey, String apiSecret, boolean isCN, boolean isDebug) { super(); this.apiKey = apiKey; this.apiSecret = apiSecret; - if (debug) WEBSITE = "http://api.faceplusplus.com/"; - } - - /** - * if debug is `true', use http instead of https - * create a empty {@link HttpRequests} object - */ - public HttpRequests(boolean debug) { - super(); - if (debug) WEBSITE = "http://api.faceplusplus.com/"; + setWebSite(isCN, isDebug); } /** @@ -331,494 +413,294 @@ public void setParams(PostParameters params) { } /** - * {@link #request(String, String)}
- * detection/detect - * @throws FaceppParseException - */ - public FaceppResult detectionDetect() throws FaceppParseException { - return request("detection", "detect"); + * used by offline detect + * example: request.offlineDetect(detecter.getImageByteArray(), detecter.getResultJsonString()); + * @param image + * @param jsonResult + * @return + * @throws FaceppParseException + */ + public JSONObject offlineDetect(byte[] image, String jsonResult) throws FaceppParseException { + return offlineDetect(image, jsonResult, this.params); } /** - * {@link #request(String, String, PostParameters)}
- * detection/detect - * @throws FaceppParseException - */ - public FaceppResult detectionDetect(PostParameters params) throws FaceppParseException{ + * used by offline detect + * example: request.offlineDetect(detecter.getImageByteArray(), detecter.getResultJsonString(), params); + * @param image + * @param jsonResult + * @param params + * @return + * @throws FaceppParseException + */ + public JSONObject offlineDetect(byte[] image, String jsonResult, PostParameters params) throws FaceppParseException{ + if (params == null) params = new PostParameters(); + params.setImg(image); + params.setMode("offline"); + params.addAttribute("offline_result", jsonResult); return request("detection", "detect", params); } - - - - /** - * {@link #request(String, String)}
- * info/get_image - * @throws FaceppParseException - */ - public FaceppResult infoGetImage() throws FaceppParseException { - return request("info", "get_image"); + + //all api here + public JSONObject detectionDetect() throws FaceppParseException { + return request("detection", "detect"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_image - * @throws FaceppParseException - */ - public FaceppResult infoGetImage(PostParameters params) throws FaceppParseException{ - return request("info", "get_image", params); + public JSONObject detectionDetect(PostParameters params) throws FaceppParseException{ + return request("detection", "detect", params); } - - - - /** - * {@link #request(String, String)}
- * info/get_face - * @throws FaceppParseException - */ - public FaceppResult infoGetFace() throws FaceppParseException { - return request("info", "get_face"); + + public JSONObject detectionLandmark() throws FaceppParseException { + return request("detection", "landmark"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_face - * @throws FaceppParseException - */ - public FaceppResult infoGetFace(PostParameters params) throws FaceppParseException{ - return request("info", "get_face", params); + public JSONObject detectionLandmark(PostParameters params) throws FaceppParseException{ + return request("detection", "landmark", params); } - - - /** - * {@link #request(String, String)}
- * info/get_session - * @throws FaceppParseException - */ - public FaceppResult infoGetSession() throws FaceppParseException { - return request("info", "get_session"); + public JSONObject trainVerify() throws FaceppParseException { + return request("train", "verify"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_session - * @throws FaceppParseException - */ - public FaceppResult infoGetSession(PostParameters params) throws FaceppParseException{ - return request("info", "get_session", params); + public JSONObject trainVerify(PostParameters params) throws FaceppParseException{ + return request("train", "verify", params); } - - - /** - * {@link #request(String, String)}
- * info/get_quota - * @throws FaceppParseException - */ - public FaceppResult infoGetQuota() throws FaceppParseException { - return request("info", "get_quota"); + public JSONObject trainSearch() throws FaceppParseException { + return request("train", "search"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_quota - * @throws FaceppParseException - */ - public FaceppResult infoGetQuota(PostParameters params) throws FaceppParseException{ - return request("info", "get_quota", params); + public JSONObject trainSearch(PostParameters params) throws FaceppParseException{ + return request("train", "search", params); } - - - /** - * {@link #request(String, String)}
- * info/get_person_list - * @throws FaceppParseException - */ - public FaceppResult infoGetPersonList() throws FaceppParseException { - return request("info", "get_person_list"); + public JSONObject trainIdentify() throws FaceppParseException { + return request("train", "identify"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_person_list - * @throws FaceppParseException - */ - public FaceppResult infoGetPersonList(PostParameters params) throws FaceppParseException{ - return request("info", "get_person_list", params); + public JSONObject trainIdentify(PostParameters params) throws FaceppParseException{ + return request("train", "identify", params); } - - - /** - * {@link #request(String, String)}
- * info/get_group_list - * @throws FaceppParseException - */ - public FaceppResult infoGetGroupList() throws FaceppParseException { - return request("info", "get_group_list"); + public JSONObject recognitionCompare() throws FaceppParseException { + return request("recognition", "compare"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_group_list - * @throws FaceppParseException - */ - public FaceppResult infoGetGroupList(PostParameters params) throws FaceppParseException{ - return request("info", "get_group_list", params); + public JSONObject recognitionCompare(PostParameters params) throws FaceppParseException{ + return request("recognition", "compare", params); } + public JSONObject recognitionVerify() throws FaceppParseException { + return request("recognition", "verify"); + } + public JSONObject recognitionVerify(PostParameters params) throws FaceppParseException{ + return request("recognition", "verify", params); + } - - /** - * {@link #request(String, String)}
- * info/get_app - * @throws FaceppParseException - */ - public FaceppResult infoGetApp() throws FaceppParseException { - return request("info", "get_app"); + public JSONObject recognitionSearch() throws FaceppParseException { + return request("recognition", "search"); } - /** - * {@link #request(String, String, PostParameters)}
- * info/get_app - * @throws FaceppParseException - */ - public FaceppResult infoGetApp(PostParameters params) throws FaceppParseException{ - return request("info", "get_app", params); + public JSONObject recognitionSearch(PostParameters params) throws FaceppParseException{ + return request("recognition", "search", params); } + public JSONObject recognitionIdentify() throws FaceppParseException { + return request("recognition", "identify"); + } + public JSONObject recognitionIdentify(PostParameters params) throws FaceppParseException{ + return request("recognition", "identify", params); + } + public JSONObject groupingGrouping() throws FaceppParseException { + return request("grouping", "grouping"); + } + public JSONObject groupingGrouping(PostParameters params) throws FaceppParseException{ + return request("grouping", "grouping", params); + } - /** - * {@link #request(String, String)}
- * person/create - * @throws FaceppParseException - */ - public FaceppResult personCreate() throws FaceppParseException { + public JSONObject personCreate() throws FaceppParseException { return request("person", "create"); } - /** - * {@link #request(String, String, PostParameters)}
- * person/create - * @throws FaceppParseException - */ - public FaceppResult personCreate(PostParameters params) throws FaceppParseException{ + public JSONObject personCreate(PostParameters params) throws FaceppParseException{ return request("person", "create", params); } - - - /** - * {@link #request(String, String)}
- * person/delete - * @throws FaceppParseException - */ - public FaceppResult personDelete() throws FaceppParseException { + public JSONObject personDelete() throws FaceppParseException { return request("person", "delete"); } - /** - * {@link #request(String, String, PostParameters)}
- * person/delete - * @throws FaceppParseException - */ - public FaceppResult personDelete(PostParameters params) throws FaceppParseException{ + public JSONObject personDelete(PostParameters params) throws FaceppParseException{ return request("person", "delete", params); } - - - /** - * {@link #request(String, String)}
- * person/add_face - * @throws FaceppParseException - */ - public FaceppResult personAddFace() throws FaceppParseException { + public JSONObject personAddFace() throws FaceppParseException { return request("person", "add_face"); } - /** - * {@link #request(String, String, PostParameters)}
- * person/add_face - * @throws FaceppParseException - */ - public FaceppResult personAddFace(PostParameters params) throws FaceppParseException{ + public JSONObject personAddFace(PostParameters params) throws FaceppParseException{ return request("person", "add_face", params); } - - - /** - * {@link #request(String, String)}
- * person/remove_face - * @throws FaceppParseException - */ - public FaceppResult personRemoveFace() throws FaceppParseException { + public JSONObject personRemoveFace() throws FaceppParseException { return request("person", "remove_face"); } - /** - * {@link #request(String, String, PostParameters)}
- * person/remove_face - * @throws FaceppParseException - */ - public FaceppResult personRemoveFace(PostParameters params) throws FaceppParseException{ + public JSONObject personRemoveFace(PostParameters params) throws FaceppParseException{ return request("person", "remove_face", params); } + public JSONObject personSetInfo() throws FaceppParseException { + return request("person", "set_info"); + } + public JSONObject personSetInfo(PostParameters params) throws FaceppParseException{ + return request("person", "set_info", params); + } - - /** - * {@link #request(String, String)}
- * person/get_info - * @throws FaceppParseException - */ - public FaceppResult personGetInfo() throws FaceppParseException { + public JSONObject personGetInfo() throws FaceppParseException { return request("person", "get_info"); } - /** - * {@link #request(String, String, PostParameters)}
- * person/get_info - * @throws FaceppParseException - */ - public FaceppResult personGetInfo(PostParameters params) throws FaceppParseException{ + public JSONObject personGetInfo(PostParameters params) throws FaceppParseException{ return request("person", "get_info", params); } + public JSONObject facesetCreate() throws FaceppParseException { + return request("faceset", "create"); + } + public JSONObject facesetCreate(PostParameters params) throws FaceppParseException{ + return request("faceset", "create", params); + } + public JSONObject facesetDelete() throws FaceppParseException { + return request("faceset", "delete"); + } + public JSONObject facesetDelete(PostParameters params) throws FaceppParseException{ + return request("faceset", "delete", params); + } - /** - * {@link #request(String, String)}
- * person/set_info - * @throws FaceppParseException - */ - public FaceppResult personSetInfo() throws FaceppParseException { - return request("person", "set_info"); + public JSONObject facesetAddFace() throws FaceppParseException { + return request("faceset", "add_face"); } - /** - * {@link #request(String, String, PostParameters)}
- * person/set_info - * @throws FaceppParseException - */ - public FaceppResult personSetInfo(PostParameters params) throws FaceppParseException{ - return request("person", "set_info", params); + public JSONObject facesetAddFace(PostParameters params) throws FaceppParseException{ + return request("faceset", "add_face", params); } + public JSONObject facesetRemoveFace() throws FaceppParseException { + return request("faceset", "remove_face"); + } + public JSONObject facesetRemoveFace(PostParameters params) throws FaceppParseException{ + return request("faceset", "remove_face", params); + } + public JSONObject facesetSetInfo() throws FaceppParseException { + return request("faceset", "set_info"); + } + public JSONObject facesetSetInfo(PostParameters params) throws FaceppParseException{ + return request("faceset", "set_info", params); + } - /** - * {@link #request(String, String)}
- * group/create - * @throws FaceppParseException - */ - public FaceppResult groupCreate() throws FaceppParseException { + public JSONObject facesetGetInfo() throws FaceppParseException { + return request("faceset", "get_info"); + } + public JSONObject facesetGetInfo(PostParameters params) throws FaceppParseException{ + return request("faceset", "get_info", params); + } + + public JSONObject groupCreate() throws FaceppParseException { return request("group", "create"); } - /** - * {@link #request(String, String, PostParameters)}
- * group/create - * @throws FaceppParseException - */ - public FaceppResult groupCreate(PostParameters params) throws FaceppParseException{ + public JSONObject groupCreate(PostParameters params) throws FaceppParseException{ return request("group", "create", params); } - - - /** - * {@link #request(String, String)}
- * group/delete - * @throws FaceppParseException - */ - public FaceppResult groupDelete() throws FaceppParseException { + public JSONObject groupDelete() throws FaceppParseException { return request("group", "delete"); } - /** - * {@link #request(String, String, PostParameters)}
- * group/delete - * @throws FaceppParseException - */ - public FaceppResult groupDelete(PostParameters params) throws FaceppParseException{ + public JSONObject groupDelete(PostParameters params) throws FaceppParseException{ return request("group", "delete", params); } - - - /** - * {@link #request(String, String)}
- * group/add_person - * @throws FaceppParseException - */ - public FaceppResult groupAddPerson() throws FaceppParseException { + public JSONObject groupAddPerson() throws FaceppParseException { return request("group", "add_person"); } - /** - * {@link #request(String, String, PostParameters)}
- * group/add_person - * @throws FaceppParseException - */ - public FaceppResult groupAddPerson(PostParameters params) throws FaceppParseException{ + public JSONObject groupAddPerson(PostParameters params) throws FaceppParseException{ return request("group", "add_person", params); } - - - /** - * {@link #request(String, String)}
- * group/remove_person - * @throws FaceppParseException - */ - public FaceppResult groupRemovePerson() throws FaceppParseException { + public JSONObject groupRemovePerson() throws FaceppParseException { return request("group", "remove_person"); } - /** - * {@link #request(String, String, PostParameters)}
- * group/remove_person - * @throws FaceppParseException - */ - public FaceppResult groupRemovePerson(PostParameters params) throws FaceppParseException{ + public JSONObject groupRemovePerson(PostParameters params) throws FaceppParseException{ return request("group", "remove_person", params); } + public JSONObject groupSetInfo() throws FaceppParseException { + return request("group", "set_info"); + } + public JSONObject groupSetInfo(PostParameters params) throws FaceppParseException{ + return request("group", "set_info", params); + } - - /** - * {@link #request(String, String)}
- * group/get_info - * @throws FaceppParseException - */ - public FaceppResult groupGetInfo() throws FaceppParseException { + public JSONObject groupGetInfo() throws FaceppParseException { return request("group", "get_info"); } - /** - * {@link #request(String, String, PostParameters)}
- * group/get_info - * @throws FaceppParseException - */ - public FaceppResult groupGetInfo(PostParameters params) throws FaceppParseException{ + public JSONObject groupGetInfo(PostParameters params) throws FaceppParseException{ return request("group", "get_info", params); } - - - /** - * {@link #request(String, String)}
- * group/set_info - * @throws FaceppParseException - */ - public FaceppResult groupSetInfo() throws FaceppParseException { - return request("group", "set_info"); + public JSONObject infoGetImage() throws FaceppParseException { + return request("info", "get_image"); } - /** - * {@link #request(String, String, PostParameters)}
- * group/set_info - * @throws FaceppParseException - */ - public FaceppResult groupSetInfo(PostParameters params) throws FaceppParseException{ - return request("group", "set_info", params); + public JSONObject infoGetImage(PostParameters params) throws FaceppParseException{ + return request("info", "get_image", params); } - - - /** - * {@link #request(String, String)}
- * recognition/compare - * @throws FaceppParseException - */ - public FaceppResult recognitionCompare() throws FaceppParseException { - return request("recognition", "compare"); + public JSONObject infoGetFace() throws FaceppParseException { + return request("info", "get_face"); } - /** - * {@link #request(String, String, PostParameters)}
- * recognition/compare - * @throws FaceppParseException - */ - public FaceppResult recognitionCompare(PostParameters params) throws FaceppParseException{ - return request("recognition", "compare", params); + public JSONObject infoGetFace(PostParameters params) throws FaceppParseException{ + return request("info", "get_face", params); } - - - /** - * {@link #request(String, String)}
- * recognition/train - * @throws FaceppParseException - */ - public FaceppResult recognitionTrain() throws FaceppParseException { - return request("recognition", "train"); + public JSONObject infoGetPersonList() throws FaceppParseException { + return request("info", "get_person_list"); } - /** - * {@link #request(String, String, PostParameters)}
- * recognition/train - * @throws FaceppParseException - */ - public FaceppResult recognitionTrain(PostParameters params) throws FaceppParseException{ - return request("recognition", "train", params); + public JSONObject infoGetPersonList(PostParameters params) throws FaceppParseException{ + return request("info", "get_person_list", params); } - - - /** - * {@link #request(String, String)}
- * recognition/verify - * @throws FaceppParseException - */ - public FaceppResult recognitionVerify() throws FaceppParseException { - return request("recognition", "verify"); + public JSONObject infoGetFacesetList() throws FaceppParseException { + return request("info", "get_faceset_list"); } - /** - * {@link #request(String, String, PostParameters)}
- * recognition/verify - * @throws FaceppParseException - */ - public FaceppResult recognitionVerify(PostParameters params) throws FaceppParseException{ - return request("recognition", "verify", params); + public JSONObject infoGetFacesetList(PostParameters params) throws FaceppParseException{ + return request("info", "get_faceset_list", params); } - - - /** - * {@link #request(String, String)}
- * recognition/recognize - * @throws FaceppParseException - */ - public FaceppResult recognitionRecognize() throws FaceppParseException { - return request("recognition", "recognize"); + public JSONObject infoGetGroupList() throws FaceppParseException { + return request("info", "get_group_list"); } - /** - * {@link #request(String, String, PostParameters)}
- * recognition/recognize - * @throws FaceppParseException - */ - public FaceppResult recognitionRecognize(PostParameters params) throws FaceppParseException{ - return request("recognition", "recognize", params); + public JSONObject infoGetGroupList(PostParameters params) throws FaceppParseException{ + return request("info", "get_group_list", params); } - + public JSONObject infoGetSession() throws FaceppParseException { + return request("info", "get_session"); + } + public JSONObject infoGetSession(PostParameters params) throws FaceppParseException{ + return request("info", "get_session", params); + } /** - * {@link #request(String, String)}
- * recognition/search - * @throws FaceppParseException - */ - public FaceppResult recognitionSearch() throws FaceppParseException { - return request("recognition", "search"); + * @deprecated this api is deprecated + * @return + * @throws FaceppParseException + */ + public JSONObject infoGetQuota() throws FaceppParseException { + return request("info", "get_quota"); } /** - * {@link #request(String, String, PostParameters)}
- * recognition/search - * @throws FaceppParseException - */ - public FaceppResult recognitionSearch(PostParameters params) throws FaceppParseException{ - return request("recognition", "search", params); + * @deprecated this api is deprecated + * @return + * @throws FaceppParseException + */ + public JSONObject infoGetQuota(PostParameters params) throws FaceppParseException{ + return request("info", "get_quota", params); } - - - /** - * {@link #request(String, String)}
- * grouping/grouping - * @throws FaceppParseException - */ - public FaceppResult groupingGrouping() throws FaceppParseException { - return request("grouping", "grouping"); + + public JSONObject infoGetApp() throws FaceppParseException { + return request("info", "get_app"); } - /** - * {@link #request(String, String, PostParameters)}
- * grouping/grouping - * @throws FaceppParseException - */ - public FaceppResult groupingGrouping(PostParameters params) throws FaceppParseException{ - return request("grouping", "grouping", params); + public JSONObject infoGetApp(PostParameters params) throws FaceppParseException{ + return request("info", "get_app", params); } } diff --git a/FaceppSDK/src/com/facepp/http/PostParameters.java b/FaceppSDK/src/com/facepp/http/PostParameters.java index 53ee01a..8424565 100644 --- a/FaceppSDK/src/com/facepp/http/PostParameters.java +++ b/FaceppSDK/src/com/facepp/http/PostParameters.java @@ -17,7 +17,7 @@ * {@code new PostParameters().setMode("oneface").setImg(new File("...")).setTag("some message")} * @author moon5kcq * @since 1.0.0 - * @version 1.1.0 + * @version 1.3.0 */ public class PostParameters { private MultipartEntity multiPart = null; @@ -43,7 +43,6 @@ private String getBoundary() { public MultipartEntity getMultiPart() { return multiPart; } - /** * default boundary is auto generate {@link #getBoundary()} @@ -212,6 +211,36 @@ public PostParameters setFaceId(String faceId) { return this; } + /** + * faceset_id=... + * @param facesetId + * @return this + */ + public PostParameters setFacesetId(String facesetId) { + addString("faceset_id", facesetId); + return this; + } + + /** + * faceset_id=..., ..., ... + * @param facesetIds + * @return this + */ + public PostParameters setFacesetId(String[] facesetId) { + setFacesetId(toStringList(facesetId)); + return this; + } + + /** + * faceset_id=... + * @param facesetIds + * @return this + */ + public PostParameters setFacesetId(ArrayList facesetId) { + setFacesetId(toStringList(facesetId)); + return this; + } + /** * person_id=... * @param personId @@ -231,7 +260,7 @@ public PostParameters setPersonName(String personName) { addString("person_name", personName); return this; } - + /** * name=... * @param name @@ -355,7 +384,42 @@ public PostParameters setImgId(String imgId) { return this; } + /** + * faceset_name=... + * @param facesetName + * @return this + */ + public PostParameters setFacesetName(String facesetName) { + addString("faceset_name", facesetName); + return this; + } + + /** + * faceset_name=... , ... , ... + * @param facesetNames + * @return this + */ + public PostParameters setFacesetName(ArrayList facesetNames) { + return setFacesetName(toStringList(facesetNames)); + } + /** + * faceset_name=... , ... , ... + * @param facesetNames + * @return this + */ + public PostParameters setFacesetName(String[] facesetNames) { + return setFacesetName(toStringList(facesetNames)); + } + /** + * `attr`=`value` + * @param attr value + * @return this + */ + public PostParameters addAttribute(String attr, String value) { + addString(attr, value); + return this; + } private void addString(String id, String str) { try { diff --git a/FaceppSDK/src/com/facepp/result/FaceppResult.java b/FaceppSDK/src/com/facepp/result/FaceppResult.java deleted file mode 100644 index 45efd04..0000000 --- a/FaceppSDK/src/com/facepp/result/FaceppResult.java +++ /dev/null @@ -1,394 +0,0 @@ -package com.facepp.result; - -import java.util.Dictionary; -import java.util.Hashtable; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import com.facepp.error.FaceppParseException; - -/** - * the request result type
- * Example
- * {@code FaceppResult faceppResult = request-result.}
- * {@code faceppResult.get("face").get(0).get("attribute").get("gender").get("value").toString()} - * @author moon5ckq - * @since 1.0.0 - * @version 1.1.0 - */ -public class FaceppResult { - static public enum JsonType { - INT, BOOL, DOUBLE, STRING, JSON, ARRAY - } - - static private Dictionary keyWordDict; - - static { - keyWordDict = new Hashtable(); - keyWordDict.put("session_id", JsonType.STRING); - keyWordDict.put("url", JsonType.STRING); - keyWordDict.put("img_id", JsonType.STRING); - keyWordDict.put("face_id", JsonType.STRING); - keyWordDict.put("person_id", JsonType.STRING); - keyWordDict.put("group_id", JsonType.STRING); - keyWordDict.put("img_width", JsonType.INT); - keyWordDict.put("img_height", JsonType.INT); - keyWordDict.put("face", JsonType.ARRAY); - keyWordDict.put("width", JsonType.DOUBLE); - keyWordDict.put("height", JsonType.DOUBLE); - keyWordDict.put("eye_left", JsonType.JSON); - keyWordDict.put("eye_right", JsonType.JSON); - keyWordDict.put("mouth_left", JsonType.JSON); - keyWordDict.put("mouth_right", JsonType.JSON); - keyWordDict.put("attribute", JsonType.JSON); - keyWordDict.put("gender", JsonType.JSON); - keyWordDict.put("age", JsonType.JSON); - keyWordDict.put("race", JsonType.JSON); - keyWordDict.put("similarity", JsonType.DOUBLE); - keyWordDict.put("component_similarity", JsonType.JSON); - keyWordDict.put("is_same_person", JsonType.BOOL); - keyWordDict.put("candidate", JsonType.ARRAY); - keyWordDict.put("results", JsonType.ARRAY); - keyWordDict.put("status", JsonType.STRING); - keyWordDict.put("response", JsonType.STRING); - keyWordDict.put("x", JsonType.DOUBLE); - keyWordDict.put("y", JsonType.DOUBLE); - - keyWordDict.put("has_untrained_face", JsonType.BOOL); - keyWordDict.put("has_untrained_person", JsonType.BOOL); - keyWordDict.put("tag", JsonType.STRING); - keyWordDict.put("person_name", JsonType.STRING); - keyWordDict.put("group_name", JsonType.STRING); - keyWordDict.put("eyebrow", JsonType.DOUBLE); - keyWordDict.put("nose", JsonType.DOUBLE); - keyWordDict.put("eye", JsonType.DOUBLE); - keyWordDict.put("mouth", JsonType.DOUBLE); - keyWordDict.put("confidence", JsonType.DOUBLE); - keyWordDict.put("success", JsonType.BOOL); - keyWordDict.put("name", JsonType.STRING); - keyWordDict.put("description", JsonType.STRING); - keyWordDict.put("position", JsonType.JSON); - keyWordDict.put("person", JsonType.ARRAY); - keyWordDict.put("group", JsonType.ARRAY); - keyWordDict.put("QUOTA_ALL", JsonType.INT); - keyWordDict.put("QUOTA_SEARCH", JsonType.INT); - keyWordDict.put("center", JsonType.JSON); - - keyWordDict.put("ungrouped", JsonType.JSON); - keyWordDict.put("result", JsonType.JSON); - - } - - private Object json; - private JsonType type; - private int httpResponseCode = 0; - - /** - * @param json - */ - public FaceppResult(Object json) { - this.json = json; - this.type = JsonType.JSON; - } - - /** - * @param json - * @param httpResponseCode - */ - public FaceppResult(Object json, int httpResponseCode) { - this.json = json; - this.type = JsonType.JSON; - this.httpResponseCode = httpResponseCode; - } - - /** - * @param json - * @param type - * @param httpResponseCode - */ - public FaceppResult(Object json, JsonType type, int httpResponseCode) { - this.json = json; - this.type = type; - this.httpResponseCode = httpResponseCode; - } - - private JSONArray a(String key) { - try { - return ((JSONObject)json).getJSONArray(key); - } catch (JSONException e) { - } - return null; - } - - private JSONObject j(String key) { - try { - return ((JSONObject)json).getJSONObject(key); - } catch (JSONException e) { - } - return null; - } - - private String s(String key) { - try { - return ((JSONObject)json).getString(key); - } catch (JSONException e) { - } - return null; - } - - private Double d(String key) { - try { - return ((JSONObject)json).getDouble(key); - } catch (JSONException e) { - } - return null; - } - - private Boolean b(String key) { - try { - return ((JSONObject)json).getBoolean(key); - } catch (JSONException e) { - } - return null; - } - - private Integer i(String key) { - try { - return ((JSONObject)json).getInt(key); - } catch (JSONException e) { - } - return null; - } - - - - private JSONArray getArray(String key) { - return a(key); - } - - private JSONObject getJson(String key) { - return j(key); - } - - private String getString(String key) { - return s(key); - } - - private Double getDouble(String key) { - return d(key); - } - - private Boolean getBoolean(String key) { - return b(key); - } - - private Integer getInt(String key) { - return i(key); - } - - ///--------------------------------------------------- - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - public String toString() { - return json.toString(); - } - /** - * change to a integer value, if no a integer type , throw the exception - * @return a Integer value - * @throws FaceppParseException - */ - public Integer toInteger() throws FaceppParseException { - if (this.type != JsonType.INT) { - throw new FaceppParseException("( " + json.toString() + " ) is not an integer."); - } - return (Integer)json; - } - /** - * change to a double value, if no a double type , throw the exception - * @return a Double value - * @throws FaceppParseException - */ - public Double toDouble() throws FaceppParseException { - if (this.type != JsonType.DOUBLE) { - throw new FaceppParseException("( " + json.toString() + " ) is not a float number."); - } - return (Double)json; - } - /** - * change to a boolean value, if no a boolean type , throw the exception - * @return a Boolean value - * @throws FaceppParseException - */ - public Boolean toBoolean() throws FaceppParseException { - if (this.type != JsonType.BOOL) { - throw new FaceppParseException("( " + json.toString() + " ) is not a boolean value."); - } - return (Boolean)json; - } - - - /** - * if not an array, throw the exception - * @return the array length - * @throws FaceppParseException - */ - public int getCount() throws FaceppParseException { - if (this.type != JsonType.ARRAY) { - throw new FaceppParseException("( " + json.toString() + " ) is not an array."); - } - - return ((JSONArray)json).length(); - } - - /** - * if not an array, throw the exception - * @param index - * @return the index-th element in array - * @throws FaceppParseException - */ - public FaceppResult get(int index) throws FaceppParseException { - - if (this.type != JsonType.ARRAY) { - throw new FaceppParseException("( " + json.toString() + " ) is not an array."); - } - - try { - return new FaceppResult( ((JSONArray)json).getJSONObject(index), JsonType.JSON, httpResponseCode); - } catch (JSONException e) { - throw new FaceppParseException("Json string can not be parsed."); - } - } - - - /** - * if don't have the key or not a json object, throw the exception - * @param key - * @return get the value about key - * @throws FaceppParseException - */ - public FaceppResult get(String key) throws FaceppParseException { - if (this.type != JsonType.JSON) { - throw new FaceppParseException("( " + json.toString() + " ) is not json string."); - } - - JsonType returnType = keyWordDict.get(key); - if (returnType == null) { - throw new FaceppParseException("invalid key word : " + key + "."); - } - - if (! ((JSONObject)json).has(key)) { - throw new FaceppParseException("( " + json.toString() + " ) do not have the key "+ key); - } - - switch (returnType) { - case INT: - return new FaceppResult( getInt(key), JsonType.INT, httpResponseCode); - case DOUBLE: - return new FaceppResult( getDouble(key), JsonType.DOUBLE, httpResponseCode); - case BOOL: - return new FaceppResult( getBoolean(key), JsonType.BOOL, httpResponseCode); - case STRING: - return new FaceppResult( getString(key), JsonType.STRING, httpResponseCode); - case ARRAY: - return new FaceppResult( getArray(key), JsonType.ARRAY, httpResponseCode); - case JSON: - return new FaceppResult( getJson(key), JsonType.JSON, httpResponseCode); - default: - - } - - return null; - } - - /** - * if not a json object, throw the exception
- * used when api is update but sdk not - * @param key - * @param type - * @return - * @throws FaceppParseException - */ - public FaceppResult get(String key, JsonType type) throws FaceppParseException { - if (this.type != JsonType.JSON) { - throw new FaceppParseException("( " + json.toString() + " ) is not json string."); - } - - switch (type) { - case INT: - return new FaceppResult( getInt(key), JsonType.INT, httpResponseCode); - case DOUBLE: - return new FaceppResult( getDouble(key), JsonType.DOUBLE, httpResponseCode); - case BOOL: - return new FaceppResult( getBoolean(key), JsonType.BOOL, httpResponseCode); - case STRING: - return new FaceppResult( getString(key), JsonType.STRING, httpResponseCode); - case ARRAY: - return new FaceppResult( getArray(key), JsonType.ARRAY, httpResponseCode); - case JSON: - return new FaceppResult( getJson(key), JsonType.JSON, httpResponseCode); - default: - - } - - return null; - } - - - /** - * the request result like
- * {
- * "error": "...",
- * "error_code": ...
- * } - * @return is a error message - */ - public boolean isError() { - if (this.type != JsonType.JSON) { - return false; - } - return ((JSONObject)json).has("error"); - } - - /** - * where error : ... - * @return the error message - * @throws FaceppParseException - */ - public String getErrorMessage() throws FaceppParseException { - if (this.type != JsonType.JSON || !isError()) { - throw new FaceppParseException("( " + json.toString() + " ) is not an error message."); - } - try { - return ((JSONObject)json).getString("error"); - } catch (JSONException e) { - throw new FaceppParseException("Json string can not be parsed."); - } - } - - /** - * where error_code : ... - * @return the error code - * @throws FaceppParseException - */ - public int getErrorCode() throws FaceppParseException { - if (this.type != JsonType.JSON || !isError()) { - throw new FaceppParseException("( " + json.toString() + " ) is not an error message."); - } - try { - return ((JSONObject)json).getInt("error_code"); - } catch (JSONException e) { - throw new FaceppParseException("Json string can not be parsed."); - } - } - - /** - * @return the http response code - */ - public int getHttpResponseCode() { - return httpResponseCode; - } -} diff --git a/Readme.md b/Readme.md index bc22d31..e6d15b7 100644 --- a/Readme.md +++ b/Readme.md @@ -1,9 +1,10 @@ # FacePlusPlus Java SDK This SDK uses apache.http lib and json lib. +The new FacePP API has been online in 17 Oct. 2013. ## 1. Install -1. download [facepp.jar](http://faceplusplus.com/static/sdk/java/facepp.jar); +1. download [faceppsdk.jar](https://raw.github.com/FacePlusPlus/facepp-java-sdk/facepp-v3/FaceppSDK/output/faceppsdk.jar); 2. add it to java project. ## 2. Import package @@ -17,10 +18,19 @@ This SDK uses apache.http lib and json lib. 1. create a com.facepp.http.HttpRequests object `HttpRequests httpRequests = new HttpRequests(you_api_key_string, you_api_secret_string);` + + * use server in china and https: + + `HttpRequests httpRequests = new HttpRequests(you_api_key_string, you_api_secret_string, true, false);` 2. create a com.facepp.http.PostParameters object with all parameters - `PostParameters postParameters = new PostParameters().setUrl("http://faceplusplus.com/static/img/demo/8.jpg").setAttribute("all");` + ``` + PostParameters postParameters = + new PostParameters() + .setUrl("http://cn.faceplusplus.com/wp-content/themes/faceplusplus.zh/assets/img/demo/9.jpg") + .setAttribute("all"); + ``` 3. request @@ -33,16 +43,19 @@ This SDK uses apache.http lib and json lib. 4. get the result use the com.facepp.json.FaceppJson object - `FaceppResult result = httpRequests.detectionDetect(postParameters);` + `JSONObejct result = httpRequests.detectionDetect(postParameters);` ## 4. Get value from result * Example: - `result.get("face").getCount()` + `result.getJSONArray("face").length()` - `result.get("face").get(0).get("face_id").toString()` + `result.getJSONArray("face").getJSONObject(0).getString("face_id")` - `result.get("face").get(0).get("attribute").get("gender").get("confidence").toDouble()` + ``` + result.getJSONArray("face").getJSONObject(0).getJSONObject("attribute") + .getJSONObject("gender").getDouble("confidence") + ``` ## 5. Debug 1. View the post request @@ -105,3 +118,16 @@ This SDK uses apache.http lib and json lib. * 1.2.0 Add https request method, and set it to default. We can use `debug = true` to open http to instead of https. + +* 2.0.0 + + Face-Plus-Plus 2.0 has released. This SDK can use the new API now. + +* 2.1.0 + + Face-Plus-Plus 2.0 was free right now, and add more new function. This SDK can use the new API now. + And, removing FaceppResult class, using JSONObject to instead of it to record the result. + +* 2.1.1 + + Fix bugs. \ No newline at end of file