Implementing Liveness Detection (PAD)
Learn how to integrate Presentation Attack Detection using id3 Face SDK's PAD module.
About PAD: Presentation Attack Detection (PAD) helps prevent spoofing attempts by detecting presentation attacks like photos, videos, or masks.
1. Prerequisites
- id3 Face SDK (v4.0 or later)
- Visual Studio 2019 or later
- .NET 6.0 or later
- Valid id3 Face SDK license with PAD module enabled
2. Setting Up the Project
Add the required NuGet package to your .NET project:
Install-Package Id3.Face.SDK
Add the following using directives to your code:
using Id3.Face.SDK.PAD;
using Id3.Face.SDK.Core;
using Id3.Face.SDK.Utils;
3. Initializing the PAD Processor
Create and configure the PAD processor:
// Initialize the SDK
var sdk = new FaceSDK();
sdk.Initialize("YOUR_LICENSE_KEY");
// Create PAD processor configuration
var padConfig = new PADProcessorConfiguration
{
// Set the detection threshold (0.0 to 1.0)
Threshold = 0.5f,
// Enable specific detection types
EnablePhotoAttackDetection = true,
EnableScreenAttackDetection = true,
EnableMaskAttackDetection = true,
// Set maximum processing time (ms)
MaxProcessingTimeMs = 1000
};
// Create the PAD processor
var padProcessor = new PADProcessor(padConfig);
Note: Replace
YOUR_LICENSE_KEY
with your actual license key.
4. Processing Frames
Process video frames to detect presentation attacks:
public PADResult ProcessFrame(byte[] frameData, ImageFormat format)
{
try
{
// Load the image
using (var image = ImageUtils.LoadImage(frameData, format))
{
// Process the frame
var result = padProcessor.Process(image);
// Check if a face was detected
if (result.FaceDetected)
{
// Check for presentation attack
if (result.IsAttack)
{
Console.WriteLine($"Attack detected! Type: {result.AttackType}, " +
$"Confidence: {result.Confidence:P2}");
}
else
{
Console.WriteLine("Live face detected");
}
// Get detailed scores
Console.WriteLine($"Live score: {result.LiveScore:P2}");
Console.WriteLine($"Attack score: {result.AttackScore:P2}");
}
return result;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error processing frame: {ex.Message}");
return new PADResult { Error = ex };
}
}
5. Handling the Results
Process the PAD results in your application:
// Example of handling PAD results
void HandlePADResult(PADResult result)
{
if (!result.FaceDetected)
{
Console.WriteLine("No face detected");
return;
}
if (result.IsAttack)
{
switch (result.AttackType)
{
case AttackType.Photo:
Console.WriteLine("Photo attack detected!");
break;
case AttackType.Screen:
Console.WriteLine("Screen attack detected!");
break;
case AttackType.Mask:
Console.WriteLine("Mask attack detected!");
break;
default:
Console.WriteLine("Unknown attack type detected!");
break;
}
// Handle the attack (e.g., show warning, log event, etc.)
HandleAttackDetected(result);
}
else
{
// Proceed with face recognition or other operations
Console.WriteLine("Live face verified");
ProcessVerifiedFace(result);
}
}
6. Best Practices
Important: For production use, always implement additional security measures and server-side validation.
- Lighting: Ensure good, even lighting for accurate detection
- Frame Rate: Maintain 15-30 FPS for optimal performance
- Error Handling: Implement comprehensive error handling
- User Guidance: Provide clear instructions to users during the process
- Testing: Test with various devices and attack vectors
7. Troubleshooting
Common Issues:
- No face detected: Check camera permissions and ensure the face is clearly visible
- False positives/negatives: Adjust the threshold value based on your requirements
- Performance issues: Reduce frame resolution or processing frequency
Need Help? Visit our community forums or
contact support for assistance.