Skip to content

Commit a74c538

Browse files
committed
Updated to simplified face comparison.
1 parent 2758679 commit a74c538

File tree

1 file changed

+26
-109
lines changed

1 file changed

+26
-109
lines changed

README.md

Lines changed: 26 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ public class MyActivity extends AppCompatActivity {
7777
boolean showGuide = true;
7878
// If you want to show the result of the scan to the user set this to true
7979
boolean showResult = true;
80+
// To extract a face that's suitable for face recognition
81+
boolean detectFaceForRecognition = true;
8082
// Set the region where the requested card was issued.
8183
// If you want the API to guess use the RegionUtil utility class.
8284
// The utility class will base the region on the device's SIM card provider
8385
// or on the device's locale. If you want the user to choose the region
8486
// set the variable to Region.GENERAL.
8587
Region region = RegionUtil.getUserRegion(this);
8688
// Construct ID capture settings
87-
VerIDIDCaptureSettings settings = new VerIDIDCaptureSettings(region, showGuide, showResult);
89+
VerIDIDCaptureSettings settings = new VerIDIDCaptureSettings(region, showGuide, showResult, detectFaceForRecognition);
8890
// Construct an intent
8991
VerIDIDCaptureIntent intent = new VerIDIDCaptureIntent(this, settings);
9092
// Start the ID capture activity
@@ -172,8 +174,12 @@ public class MyActivity extends AppCompatActivity {
172174
* (for example in response to a button click).
173175
*/
174176
void runLivenessDetection() {
177+
// Create liveness detection settings
178+
VerIDLivenessDetectionSessionSettings settings = new VerIDLivenessDetectionSessionSettings();
179+
// Ask Ver-ID to extract face templates needed for face recognition
180+
settings.includeFaceTemplatesInResult = true;
175181
// Construct the liveness detection intent
176-
VerIDLivenessDetectionIntent intent = new VerIDLivenessDetectionIntent(this);
182+
VerIDLivenessDetectionIntent intent = new VerIDLivenessDetectionIntent(this, settings);
177183
// Start the liveness detection activity
178184
startActivityForResult(intent, REQUEST_CODE_LIVENESS_DETECTION);
179185
}
@@ -220,136 +226,47 @@ public class MyActivity extends AppCompatActivity {
220226

221227
Building on example 1 and 2, you can use the results of the ID capture and liveness detection sessions and compare their faces. The activity below contains two `AsyncTaskLoader` classes that extract the bitmaps from the results and run the face comparison.
222228

223-
**Important:** When requesting the live face you must set `includeFaceTemplatesInResult` of the `VerIDLivenessDetectionSessionSettings` object to `true` to extract the captured face template and make it available for recognition.
229+
**Important:** When requesting the live face you must set `includeFaceTemplatesInResult` of the `VerIDLivenessDetectionSessionSettings` and `detectFaceForRecognition` of the `VerIDIDCaptureSettings` object to `true` to extract the captured face template and make it available for recognition.
224230

225231
~~~java
226-
public class CaptureResultActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks, FaceTemplateExtraction.FaceTemplateExtractionListener {
227-
228-
// Loader IDs
229-
private static final int LOADER_ID_SCORE = 458;
232+
public class CaptureResultActivity extends AppCompatActivity {
230233

231234
// Live face and ID capture results
232235
private VerIDSessionResult livenessDetectionResult;
233236
private IDCaptureResult idCaptureResult;
234237

235-
/**
236-
* Loader that compares the face from the card with the live face(s)
237-
*/
238-
private static class ScoreLoader extends AsyncTaskLoader<Float> {
239-
240-
private IFace cardFace;
241-
private IFace[] liveFaces;
242-
243-
public ScoreLoader(Context context, IFace cardFace, IFace[] liveFaces) {
244-
super(context);
245-
this.cardFace = cardFace;
246-
this.liveFaces = liveFaces;
247-
}
248-
249-
@Override
250-
public Float loadInBackground() {
251-
Float score = null;
252-
for (IFace face : liveFaces) {
253-
try {
254-
float faceScore = FaceUtil.compareFaces(cardFace, face);
255-
if (score == null) {
256-
score = faceScore;
257-
} else {
258-
score = Math.max(faceScore, score);
259-
}
260-
} catch (Exception e) {
261-
e.printStackTrace();
262-
}
263-
}
264-
return score;
265-
}
266-
}
267-
268238
@Override
269239
protected void onCreate(Bundle savedInstanceState) {
270240
super.onCreate(savedInstanceState);
271241
// Set up views
272242
// Obtain idCaptureResult and livenessDetectionResult, e.g. from intent parcelable
273243
// ...
274-
if (idCaptureResult != null && idCaptureResult.getFace() != null && livenessDetectionResult != null && livenessDetectionResult.getRecognitionFaces(VerID.Bearing.STRAIGHT).length > 0) {
275-
if (idCaptureResult.getFace().isSuitableForRecognition()) {
276-
// The card capture result has a suitable face
277-
compareFaceTemplates();
278-
return;
279-
} else if (idCaptureResult.getFace().isBackgroundProcessing()) {
280-
// The ID card face is being processed. Listen for face template extraction events
281-
VerID.shared.getFaceTemplateExtraction().addListener(idCaptureResult.getFace().getId(), this);
282-
return;
283-
}
244+
if (idCaptureResult != null && idCaptureResult.getFace() != null && idCaptureResult.getFace().isSuitableForRecognition() && livenessDetectionResult != null && livenessDetectionResult.getRecognitionFaces(VerID.Bearing.STRAIGHT).length > 0) {
245+
compareFaceTemplates();
246+
return;
284247
}
285248
updateScore(null);
286249
}
287250

288-
@Override
289-
protected void onDestroy() {
290-
super.onDestroy();
291-
getLoaderManager().destroyLoader(LOADER_ID_SCORE);
292-
if (idCaptureResult != null && idCaptureResult.getFace() != null && idCaptureResult.getFace().isBackgroundProcessing()) {
293-
VerID.shared.getFaceTemplateExtraction().removeListener(idCaptureResult.getFace().getId(), this);
294-
}
295-
}
296-
297251
private void compareFaceTemplates() {
298-
if (idCaptureResult.getFace().isSuitableForRecognition()) {
299-
// The face on the ID card is registered, calculate the similarity score
300-
Loader loader = getSupportLoaderManager().initLoader(LOADER_ID_SCORE, null, this);
301-
if (loader != null) {
302-
loader.forceLoad();
303-
}
304-
}
305-
}
306-
307-
@Override
308-
public void onFaceTemplateExtracted(long faceId, FBFace face) {
309-
VerID.shared.getFaceTemplateExtraction().removeListener(faceId, this);
310-
if (idCaptureResult.getFace() != null && idCaptureResult.getFace().isBackgroundProcessing() && idCaptureResult.getFace().getId() == faceId) {
311-
// Face template has been extracted from the face in the ID card. The face is now ready to be registered
312-
idCaptureResult.setFace(face);
313-
if (idCaptureResult.getFace().isSuitableForRecognition()) {
314-
compareFaceTemplates();
315-
} else {
316-
updateScore(null);
252+
Float score = null;
253+
IFace[] cardFace = idCaptureResult.getFace();
254+
IFace[] liveFaces = livenessDetectionResult.getRecognitionFaces(VerID.Bearing.STRAIGHT);
255+
for (IFace face : liveFaces) {
256+
try {
257+
float faceScore = FaceUtil.compareFaces(cardFace, face);
258+
if (score == null) {
259+
score = faceScore;
260+
} else {
261+
score = Math.max(faceScore, score);
262+
}
263+
} catch (Exception e) {
264+
e.printStackTrace();
317265
}
318266
}
319-
}
320-
321-
@Override
322-
public void onFaceTemplateExtractionProgress(long faceId, double progress) {
323-
324-
}
325-
326-
@Override
327-
public void onFaceTemplateExtractionFailed(long faceId, Exception exception) {
328-
VerID.shared.getFaceTemplateExtraction().removeListener(faceId, this);
329-
updateScore(null);
330-
}
331-
332-
@Override
333-
public Loader onCreateLoader(int id, Bundle args) {
334-
if (idCaptureResult.getFace() != null) {
335-
return new ScoreLoader(this, idCaptureResult.getFace(), livenessDetectionResult.getRecognitionFaces(VerID.Bearing.STRAIGHT));
336-
} else {
337-
updateScore(null);
338-
return null;
339-
}
340-
}
341-
342-
@Override
343-
public void onLoadFinished(Loader loader, Object data) {
344-
Float score = (Float) data;
345267
updateScore(score);
346268
}
347269

348-
@Override
349-
public void onLoaderReset(Loader loader) {
350-
351-
}
352-
353270
@UiThread
354271
private void updateScore(Float score) {
355272
if (score != null) {

0 commit comments

Comments
 (0)