The First Face++ Java Program

Click here to download, unzip.

Set Up Eclipse Environment

Please make sure the Eclipse environment is configured correctly on your computer. If not, please refer to the instruction on the Eclipse official website.

New an Ecplise Java Project

1. In the menu bar, choose File-->New-->Java Project.

1

2. Fill the project name. Uncheck "Use default location" and specify your local directory of facepp-java-sdk/Demo/FaceppDemo.

2

3. Click "Next". You'll see the following page. Click "Finish".3

Run the first Java program

1. Under Package Explorer, expand "src-->(default package)-->Test.java". Double-click "Test.java" to view the Java program.

4

2. Click the green botton "Run Test" 5 to run your program. 3. Observe "Console" to view your program output.

6

Reading the Program

The program Test.java did the following things,
  1. First, detected a photo of a soccer team. 11 faces were detected.
  2. Then, 11 people were created with the corresponding faces.
  3. Finally, all people were added to a group.
  4. The program called "/train/verify" and "/recognition/verify" to verify the face.
Specified procedures are shown below. Initialize HttpRequests. Four parameters are API Key, API Secret, whether to use the Chinese Server, whether to use HTTP connection mode (instead of HTTPS).
// replace api_key and api_secret here (note)
HttpRequests httpRequests = new HttpRequests(
    "4480afa9b8b364e30ba03819f3e9eff5",
    "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M ", true, true);
Detect a photo of a soccer team.
// detection/detect
result = httpRequests
    .detectionDetect(new PostParameters()
        .setUrl("http://cn.faceplusplus.com/wp-content" +
            "/themes/faceplusplus.zh/assets/img/demo/9.jpg"));
Create people. Add face by calling "personAddFace". Add Chinese name by calling "setPersonName". And get the information of every person by calling "personGetInfo".
// -----------------Person-----------------
// person/create
System.out.println("\nperson/create");
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.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.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.getJSONArray("face").length(); ++i)
  System.out.println(httpRequests
      .personGetInfo(new PostParameters()
          .setPersonName("person_" + i)));
Create a Faceset, which is similar with the creation of people.
// -----------------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)));
Create a Group and add every Person to the Group.
// -----------------Group-----------------
// group/create
System.out.println("\ngroup/create");
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.getJSONArray("face").length(); ++i)
  personList.add("person_" + i);

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_0").setTag("group tag")));

// group/get_info
System.out.println("\ngroup/get_info");
System.out.println(httpRequests.groupGetInfo(new PostParameters()
    .setGroupName("group_0")));
Build face identification model by calling "trainIdentify". Build face verification model by calling "trainVerify".
// -----------------Recognition-----------------

// recognition/train
JSONObject syncRet = null;

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")));

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()));
}
Call "recognitionIdentify" with the input of a Chinese female singer. The console will show the success of identification, but the confidence is only 10%.
// 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.zh/assets/img/demo/5.jpg")));
Call "recognitionVerify" to verity two face images. One returns True and the other returns False.
// recognition/verify
System.out.println("\nrecognition/verify");
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"))));
Google+