The Baidu Crowd Attribute Recognition API is a powerful image recognition service that analyzes user demographics such as age, gender, interests, and occupation by processing uploaded images. This API enables developers to gain deeper insights into their user base and implement targeted marketing and personalized recommendations. This article explains how to integrate the API using PHP.
First, visit the Baidu AI Open Platform to apply for the service. Log in and navigate to the "Crowd Attribute Recognition" service in the console. Create a new application to obtain the API Key and Secret Key needed for API requests.
Use Composer to install Baidu's PHP SDK by running the following command:
composer require baidu/aip-sdk
After installation, include the SDK in your project as follows:
require_once 'vendor/autoload.php';
Below is a complete PHP code example for calling Baidu's crowd attribute recognition API:
<?php
require_once 'vendor/autoload.php';
use BaiduAipClient;
use BaiduAipAipImageCensor;
const APP_ID = 'your_app_id';
const API_KEY = 'your_api_key';
const SECRET_KEY = 'your_secret_key';
$client = new Client(APP_ID, API_KEY, SECRET_KEY); // Create Client object
$imageCensor = new AipImageCensor($client); // Create AipImageCensor object
// Path to the image file
$image = file_get_contents('your_image_path');
// Call the crowd attribute recognition API
$result = $imageCensor->user()->crowd($image);
// Handle the API response
if (!empty($result['error_code'])) {
echo 'Error: ' . $result['error_msg'];
} else {
// Extract crowd attribute information
$attributes = $result['result']['user_attributes'][0]['attributes'];
// Output crowd attribute details
echo 'Age: ' . $attributes['age'] . '<br>';
echo 'Gender: ' . $attributes['gender'] . '<br>';
echo 'Occupation: ' . $attributes['occupation'] . '<br>';
echo 'Interest: ' . $attributes['interest'];
}
?>
Please replace your_app_id, your_api_key, and your_secret_key with your own credentials obtained from the Baidu AI platform, and replace your_image_path with the local path of the image you want to analyze.
Save the PHP code to a file and execute it via the command line. The program will upload the image to Baidu AI and return the recognized crowd attribute information including age, gender, interests, and occupation, which can be used for further processing or analysis.
This article demonstrated how to integrate Baidu's Crowd Attribute Recognition API using PHP. This API is suitable for various scenarios such as e-commerce recommendations, personalized content delivery, and advertising optimization. We hope this guide helps you quickly implement and deploy related applications.
Related Tags:
API