BWS 3 Face Recognition is in preview now!
Performs a one-to-one comparison of the uploaded face image with a stored biometric template in order to verify whether the individual is the person he or she claims to be.
The Verify
API is defined as a unary RPC:
rpc Verify (FaceVerificationRequest) returns (FaceVerificationResponse);
message FaceVerificationRequest {
int64 classId = 1;
ImageData image = 2;
}
message FaceVerificationResponse {
JobStatus status = 1;
repeated JobError errors = 2;
ImageProperties image_properties = 3;
bool verified = 4;
double score = 5;
}
The FaceVerificationRequest
message has the fields as follows:
classId
image
The maximum API request size is 50 MB
.
This API requires a valid JWT in the Authorization request header and accepts an optional reference number.
Authorization | Required Bearer authentication. Please refer to BWS API Authentication for a description of how to provide a valid JWT here. |
Reference-Number | Optional, client specific reference number, which will be added to the BWS bookkeeping as well as to the response header. You typically use this reference to link the resulting BWS bookkeeping entries with your logs. |
On success the API returns a FaceVerificationResponse
message:
status
errors
may have occurred, even if the job was completed successfully.
errors
image_properties
verified
true
, when the person specified by the given class ID could have been verified successfully with the given sample;
false
otherwise, or when errors occurred (see errors
for details).
score
The calculated verification score (a value between 0.0 and 1.0) that led to the verified
decision.
The higher the score, the more likely the face in the image and the template referenced by the class ID belong to the same person.
In case that the verified
field is set to false
, some errors
might have been reported:
Besides of the success return status code OK (0), this call might also return one of the following gRPC error status codes to indicate an error:
All successful BWS gRPC calls return a response header and a response trailer containing additional information about the request:
Response Header | |
jobid | The Job-ID (a GUID) that is assigned to this BWS call. |
bws-version | The version of the BWS gRPC service. |
reference-number | An optional reference number as provided in the request header. |
date | The timestamp when the request has been received at the server. |
... | Other headers that might have been added by the server (NGINX, Kestrel, ...) that was handling the request. |
Response Trailer | |
response-time-ms | The timespan in milliseconds the request spent at the BWS service. |
... | Other trailers, like exception trailers, which are added by the gRPC framework in case an RPC exception occurred. |
Here is a short example of how to call into the Verify gRPC API using a classId
and a image
.
Please refer to BWS API Authentication for a description of the methods related to authentication.
The tooling package Grpc.Tools
can be used to generate C# assets, such as classes and methods, from the facerecognition.proto
and bwsmessages.proto
files.
Refer to overview for gRPC on .NET to learn more about how to call into a gRPC service with a .NET client.
try
{
using GrpcChannel channel = CreateAuthenticatedChannel(new Uri(options.Host), GenerateToken(options.ClientId, options.Key));
var client = new BioIDWebService.BioIDWebServiceClient(channel);
var request = new FaceVerificationRequest
{
Image = new ImageData { Image = ByteString.CopyFrom(File.ReadAllBytes(options.files[0].FullName)) },
ClassId = options.classId
};
using var call = client.VerifyAsync(request);
FaceVerificationResponse response = await call.ResponseAsync.ConfigureAwait(false);
// ...
Console.WriteLine($"Verified: {response.Verified} - Score:{response.Score}");
}
catch (RpcException ex)
{
Console.Error.WriteLine($"gRPC error from calling service: {ex.Status.StatusCode} - '{ex.Status.Detail}'");
}
Assuming that the Protoc compiler and the Protoc gRPC Java plugin are already installed on your system.
To generate Protobuf Java classes and gRPC client stubs from the facerecognition.proto
and bwsmessages.proto
files.
Use the Protobuf compiler (protoc) with the following command in the cmd or terminal:
protoc -I=PATH_TO_PROTOS_DIR --java_out=OUTPUT_DIR --grpc-java_out=OUTPUT_DIR --plugin=protoc-gen-grpc-java=PATH_TO_PLUGIN facerecognition.proto bwsmessages.proto
For a detailed step by step guide on protobuf usage with Java, please refer to Java detailed guide
import java.util.concurrent.CompletableFuture;
import com.bioid.services.BioIDWebServiceGrpc;
import com.bioid.services.BioIDWebServiceGrpc.BioIDWebServiceStub;
import com.bioid.services.Bws.ImageData;
import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;
import com.bioid.services.Facerecognition.FaceVerificationRequest;
import com.bioid.services.Facerecognition.FaceVerificationResponse;
public void verifyAsync(byte[] image, long classId){
ManagedChannel grpcChannel = null;
try {
String jwtToken = generateToken(ptions.clientId, option.secretKey, options.expireMinutes);
grpcChannel = createAuthenticatedChannel(option.host, jwtToken);
var bwsClient = FaceRecognitionGrpc.newStub(grpcChannel);
var verifyRequest = FaceVerificationRequest.newBuilder()
// The unique class ID of the enrolled person
// You should set your ClassID
.setClassId(classId)
.setImage(ImageData.newBuilder().setImage(ByteString.copyFrom(image)).build())
.build();
CompletableFuture <FaceVerificationResponse> verifyResult = new CompletableFuture <>();
bwsClient.verify(verifyRequest, new StreamObserver <FaceVerificationResponse>() {
@Override
public void onNext(FaceVerificationResponse value) {
verifyResult.complete(value);
}
@Override
public void onError(Throwable t) {
verifyResult.completeExceptionally(t);
}
@Override
public void onCompleted() {
}
});
var verifyResponse = verifyResult.get();
System.out.printf("Verified: %s - Score: %f", verifyResponse.getVerified(), verifyResponse.getScore());
} catch (Exception ex) {
System.err.printf("Error processing images: " + ex.getMessage());
}finally {
if(grpcChannel != null) {
grpcChannel.shutdown();
}
}
}
You can generate the necessary Python client code from the facerecognition.proto
and bwsmessages.proto
files
using the protocol buffer compiler protoc (install: python -m pip install grpcio-tools
)
from the Phython gRPC tools.
These files can be taken from .proto service definitions.
Use the following commands to generate the necessary Python files:
python -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. facerecognition.proto bwsmessages.proto
import grpc
import facerecognition_pb2
import facerecognition_pb2_grpc
token = generate_token(args.clientid, args.key, arg.expireMinutes)
with create_authenticated_channel(args.host,token) as channel:
stub = facerecognition_pb2_grpc.FaceRecognitionStub(channel)
request = facerecognition_pb2.FaceVerificationRequest()
with open(args.image, 'rb') as f:
request.image.image= f.read()
try:
# The unique class ID of the enrolled person
# You should set your ClassID
request.classId = args.classs_id
response = stub.Verify(request)
print(response)
except grpc.RpcError as rpc_error:
print("Received error: ", rpc_error)