Face recognition technology is widely used across various industries, such as for login, attendance, and security systems. Baidu AI offers a powerful face recognition API, which can help developers quickly implement this functionality. This article will explain how to integrate Baidu's Face Recognition API with PHP to perform face detection and comparison operations.
Before you can integrate Baidu's Face Recognition API, there are a few preparations to make:
First, you need to register an account on the Baidu AI platform and create an application to obtain your API Key and Secret Key. Once your application is created, you can find these credentials in your Baidu AI console.
To make using Baidu AI's API easier, you can download the official PHP SDK from the following link: Baidu AI SDK. After downloading, include the AipFace.php file from the SDK into your PHP code.
Baidu's Face Recognition API provides a powerful face detection feature that can identify the number, position, angle, and other characteristics of faces in an image. Below is an example of how to call the Face Detection API in PHP:
<?php require_once 'AipFace.php'; // Initialize AipFace object $app_id = 'Your App ID'; $api_key = 'Your API Key'; $secret_key = 'Your Secret Key'; $client = new AipFace($app_id, $api_key, $secret_key); // Read image and convert to BASE64 encoding $image = file_get_contents('test.jpg'); $image_type = 'BASE64'; // Set options for returned parameters $options = array(); $options["face_field"] = "age,beauty,gender,expression"; // Call the face detection API $result = $client->detect($image, $image_type, $options); // Output the result echo json_encode($result); ?>
In the code above, we initialize the AipFace object using the API Key and Secret Key. Then we read the local image and convert it into a BASE64 encoded string, which is passed as a parameter to the Face Detection API. You can also specify which face attributes to return, such as age, beauty, gender, and expression, using the $options array.
The Face Comparison API allows you to compare the faces in two images and return the similarity between them. Below is an example of how to call the Face Comparison API in PHP:
<?php require_once 'AipFace.php'; // Initialize AipFace object $app_id = 'Your App ID'; $api_key = 'Your API Key'; $secret_key = 'Your Secret Key'; $client = new AipFace($app_id, $api_key, $secret_key); // Read two images and convert them to BASE64 encoding $image1 = file_get_contents('1.jpg'); $image1_type = 'BASE64'; $image2 = file_get_contents('2.jpg'); $image2_type = 'BASE64'; // Set options for returned parameters $options = array(); $options["ext_fields"] = "qualities"; $options["image_liveness"] = "faceliveness,faceliveness"; // Call the face comparison API $result = $client->match([ [ "image" => base64_encode($image1), "image_type" => $image1_type ], [ "image" => base64_encode($image2), "image_type" => $image2_type ] ], $options); // Output the result echo json_encode($result); ?>
In the code above, we initialize the AipFace object and read two images, converting them into BASE64 encoded strings to pass as parameters to the Face Comparison API. You can also set additional options in the $options array, such as face quality and liveness detection results.
Through this article, you have learned how to integrate Baidu Face Recognition API with PHP, including how to use the Face Detection and Face Comparison APIs. Now, you can expand the functionality further and integrate face recognition into your own applications. Baidu AI's powerful technology makes face recognition simple and efficient.