Vision Sample (Android)
A sample project of implementing Liveness Detection and Identity OCR on Android app using Kredibel Vision SDK.
You can checkout the source code of this project.
git clone https://github.com/kredibel-id/VisionSample-Android.git
Then open this sample project with Android Studio or Intellij IDEA.
Vision SDK
Introduction
Vision SDK is a library that provides computer vision services such as Liveness Detection and Identity OCR with Kredibel VisionAI technology.
Features
Liveness Detection
- Examine the digital representation of the user's face from the camera preview in realtime.
- Out-of-frame face detection to prevent spoofing.
- Analyze multiple movements, including head movements, eye blinks, smiles and mouth opening to determine activity.
- Determine whether it is a living person or not.
Identity OCR
Identity OCR is an Optical Character Recognition (OCR) service that supports some types :
- National Identity (KTP)
- Driving License (SIM)
- Passport
- Handheld with Id Card selfie
Currently the Vision SDK can only be used on the Android platform.
Support API Level
Install / Setup
- Aar file size : 2,7 MB
Gradle
1. Add kredibel repository.
maven{url 'https://repo.repsy.io/mvn/kredibel/sdk'}
You can do this in two alternative ways.
- Latest way(Gradle 7+) : Add repository in dependencyResolutionManagement in setting.gradle.
dependencyResolutionManagement {
repositories {
...
...
maven{url 'https://repo.repsy.io/mvn/kredibel/sdk'} // <โ-- add this
}
}
- Old way : Add repository in build.gradle file at Project level.
allprojects {
repositories {
...
...
maven{url 'https://repo.repsy.io/mvn/kredibel/sdk'} // <โ-- add this
}
}
2. Add this dependency to gradle script on app module.
dependencies {
implementation 'io.kredibel:vision:0.0.1-beta-20220313085304' // Please check latest version
}
API-Key
Please read the instructions here to get the API-Key or contact our sales team.
Config AndroidManifest.xml
After getting API-Key then open your AndroidManifest.xml than add a this meta-data in the scope of
tag.
<meta-data android:name="io.kredibel.sdk.APIKEY" android:value="Your API-Key" />
Please name the attribute name with io.kredibel.sdk.APIKEY
.
Example :
xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="id.web.hangga.visionsample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.VisionSample">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
intent-filter>
activity>
<meta-data
android:name="io.kredibel.sdk.APIKEY"
android:value="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6OCw bla.. Bla.. bla.."/>
application>
manifest>
How to Use (Basic Implementation)
The Vision class is the main class in the Kredibel Vision SDK. This class contains methods or functions to handle Liveness Detection and OCR quickly. You don't need to create a layout/UI, because we have provided everything. You just use all the functions/methods in the Vision class.
Liveness Detection
1. Single Detection
Vision.with(this) // Context, required
.detection(Detection.SMILE) // required
.start()
Vision.with(this)
.detection(Detection.SMILE) // required
.start();
2. Multiple Detection
Vision.with(this)
.detection(arrayOf(Detection.SMILE, Detection.MOUTH_OPEN, Detection.BLINK_LEFT)) // required
.delay(2000) // milliseconds, optional. Default = 1000
.start()
Vision.with(this)
.detection(new String[]{Detection.SMILE, Detection.MOUTH_OPEN, Detection.BLINK_LEFT}) // required
.delay(2000) // milliseconds, optional. Default = 1000
.start();
The following are some of the head and facial movements supported by the Vision SDK.
Face and Head Movements | Parameters |
---|---|
Smile | Detection.SMILE |
Open mouth | Detection.MOUTH_OPEN |
Look Up | Detection.LOOK_UP |
Look to the right | Detection.RIGHT |
Looking down | Detection.LOOKING_DOWN |
Look to the left | Detection.LEFT |
Get random head and face movements | Detection.RANDOM_HEAD_ANGLE |
Left eye wink | Detection.BLINK_LEFT |
Right eye wink | Detection.BLINK_RIGHT |
Getting random winks | Detection.RANDOM_EYE_BLINK |
Identity OCR
Vision.with(this)
.identity(Identity.KTP) // required. Identity type.
.showOCRLastResult(true) // optional
.onSuccessPage(SuccessPageActivity::class.java) // optional
.start()
Vision.with(this)
.identity(Identity.KTP) // required. Identity type.
.showOCRLastResult(true) // optional
.onSuccessPage(SuccessPageActivity.class) // optional
.start();
The following are some of the supported document types and their parameter names.
Dosument Type | Parameters |
---|---|
Indonesian National Identity Card/ Kartu Tanda Penduduk(KTP) | Identity.KTP |
Driver's license | Identity.SIM |
Passport | Identity.PASSPORT |
Handheld with id card selfie | Identity.HANDHELD |
Get Result Data
You can use the onSuccessPage() method to select your activity that will receive the result data.
Vision.with(this) // Context, required
.detection(Detection.SMILE) // required
.onSuccessPage(SuccessPageActivity::class.java) // optional for passing result data
.start()
Vision.with(this)
.detection(Detection.SMILE) // required
.onSuccessPage(SuccessPageActivity.class) // optional for passing result data
.start();
Then you can get result data from intent in your SuccessPageActivity on activity created override method with this parameters.
getParcelableArrayListExtra(Vision.RESULT_LIVENESS)
getParcelableExtra(Vision.RESULT_OCR)
Example :
class SuccessPageActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_success_page)
// get result data
val livenessResults : List<LivenessResult> = intent.getParcelableArrayListExtra(Vision.RESULT_LIVENESS)!!
val ocrResult : OcrResult = intent.getParcelableExtra(Vision.RESULT_OCR)!!
}
}
public class SuccessPageActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
// get result data
List<LivenessResult> livenessResults = getIntent().getParcelableArrayListExtra(Vision.RESULT_LIVENESS);
OcrResult ocrResult = intent.getParcelableExtra(Vision.RESULT_OCR);
}
}
Using VisionListener
You can use VisionListener for capture all detection results and or add a custom action after process.
If you use a VisionListener, then you don't need to call the onSuccessPage() method, because it won't run.
Vision.with(this)
.detection(arrayOf(Detection.SMILE, Detection.MOUTH_OPEN)) // required
.listener(object : VisionListener{ // listener, optional on Liveness & OCR
override fun onSuccess(livenessResult: MutableList<LivenessResult>?, ocrResult: OcrResult?) {
// if you want to capture all detection results and or add a custom action.
}
override fun onError(s: String?) {
showMessage(s!!)
}
})
.delay(2000) // milliseconds, optional. Default = 1000
.start()
Vision.with(this)
.detection(new String[]{Detection.SMILE, Detection.MOUTH_OPEN}) // required
.listener(new VisionListener() { // listener, optional on Liveness & OCR
@Override
public void onSuccess(List<LivenessResult> list, OcrResult ocrResult) {
// if you want to capture all detection results and or add a custom action.
}
@Override
public void onError(String s) {
}
})
.delay(2000) // milliseconds, optional. Default = 1000
.start();
Optional Features
Some optional features that you can use.
Vision.with(this)
.detection(arrayOf(Detection.SMILE, Detection.MOUTH_OPEN)) // required
.delay(2000) // milliseconds, optional. Default = 1000
.onSuccessPage(SecondActivity::class.java) // optional
.finishOnSuccess(true) // optional, for auto destroy current activity/context after liveness/ocr process.
.showContour(true) // optional
.showLabel(true) // optional
.showBoundingBox(true) // optional
.start()
Vision.with(this)
.detection(new String[]{Detection.SMILE, Detection.MOUTH_OPEN}) // required
.delay(2000) // milliseconds, optional. Default = 1000
.onSuccessPage(SecondActivity.class) // optional
.finishOnSuccess(true) // optional, for auto destroy current activity/context after liveness/ocr process.
.showContour(true) // optional
.showLabel(true) // optional
.showBoundingBox(true) // optional
.start();
Customizing String
You can customize instructions and some text by adding the following string resource to your project. Add only the strings you need and make sure the string name is correct, don't be mistaken.
<string name="kv_title_close" translatable="false">Closestring>
<string name="kv_title_next" translatable="false">Nextstring>
<string name="kv_msg_loading_data" translatable="false">Loading...string>
<string name="kv_title_instruction" translatable="false">Follow instruction:string>
<string name="kv_title_liveness" translatable="false">Liveness Detectionstring>
<string name="kv_title_identity_type" translatable="false">Identity Typestring>
<string name="kv_msg_verification_complete" translatable="false">Verification Completestring>
<string name="kv_clue_yourface_inframe" translatable="false">Make sure your face is in the frame and in a well-lit place.
string>
<string name="kv_msg_yourface_out_circle" translatable="false">Oops! Your face should stay in circle during liveness. We will try again from the beginning.string>
<string name="kv_msg_liveness_oncomplete" translatable="false">You have successfully followed all instructions, congrats!
string>
<string name="kv_smile" translatable="false">Please Smilestring>
<string name="kv_left_eye_blink" translatable="false">Left Eye Blinkstring>
<string name="kv_right_eye_blink" translatable="false">Right Eye Blinkstring>
<string name="kv_look_up" translatable="false">Look Upstring>
<string name="kv_look_down" translatable="false">Look Downstring>
<string name="kv_look_left" translatable="false">Look Leftstring>
<string name="kv_look_right" translatable="false">Look Rightstring>
<string name="kv_open_mouth" translatable="false">Open your Mouthstring>
<string name="kv_title_ocr_last_result" translatable="false">See Last Resultstring>
<string name="kv_title_identity_result" translatable="false">Identity Resultstring>
<string name="kv_title_scan_identity" translatable="false">Scan Identitystring>
<string name="kv_title_scan_ktp" translatable="false">Scan Identity - KTPstring>
<string name="kv_title_scan_sim" translatable="false">Scan Identity - SIMstring>
<string name="kv_title_scan_passport" translatable="false">Scan Identity - PASSPORTstring>
<string name="kv_title_hand_held" translatable="false">Selfie holding Identity cardstring>
<string name="kv_title_ocr_start" translatable="false">Start Verificationstring>
<string name="kv_title_ocr_take_picture" translatable="false">Take Picturestring>
<string name="kv_title_ocr_uploading" translatable="false">Uploading...string>
<string name="kv_msg_upload_identity" translatable="false">Uploading Identity ...string>
<string name="kv_msg_ocr_succeded" translatable="false">Verification succeededstring>
<string name="kv_msg_ocr_see_result" translatable="false">Click the "See Last Result" button to see your verification livenessResult.string>
<string name="kv_msg_upload" translatable="false">Your identity is being uploaded and processed by our system, it may take some time.string>
<string name="kv_msg_ocr_verification_failed" translatable="false">Verification Failedstring>
<string name="kv_clue_card_inframe" translatable="false">Position your identity card in the frame and in a well-lit place.
string>