Current Location: Home> Latest Articles> PHP Integration with Baidu General Object and Scene Recognition API Tutorial: Quickly Implement Image Recognition Features

PHP Integration with Baidu General Object and Scene Recognition API Tutorial: Quickly Implement Image Recognition Features

gitbox 2025-06-16

1. Introduction to Baidu General Object and Scene Recognition API

Baidu General Object and Scene Recognition API provides a high-precision, highly available, and flexible image recognition service. It automatically identifies objects or scenes based on image content and is widely used in image content analysis, image retrieval, advertising, security monitoring, and other fields.

Before using this API, you need to register on Baidu AI Open Platform to get your API Key and Secret Key, and you also need to perform authentication.

<h3><span class="hljs-number">2. PHP Integration with Baidu General Object and Scene Recognition API</h3>
<h3>2.1 Obtaining API Key and Secret Key</h3>
<p>First, register on the Baidu AI Open Platform and create an application. After successful creation, you will obtain the API Key and Secret Key, which are required for authentication.</p>

<h3>2.2 Authentication</h3>
<p>Before using the API, you need to authenticate. The following example demonstrates how to authenticate using Baidu's aip-php-sdk's AipImageClass:</p>
<pre><code class="language-php">
// Include AipImageClass from Baidu's aip-php-sdk
require_once 'AipImageClass.php';
// Set up API Key and Secret Key for Baidu General Object and Scene Recognition API
define('APP_ID', 'your_app_id');
define('API_KEY', 'your_api_key');
define('SECRET_KEY', '<span>your_secret_key<span class="hljs-string">');
// Instantiate AipImageClass and authenticate
$client = new AipImageClass(APP_ID, API_KEY, SECRET_KEY);

Be sure to replace the placeholders `APP_ID`, `API_KEY`, and `SECRET_KEY` with your actual values.

2.3 Image Recognition

Once authentication is successful, you can perform image recognition. The following example shows how to recognize a local image file:


// Path to the image file to be recognized
$image_file = '</span>test.jpg<span class="hljs-string">';
// Read the image file data
$image_data = file_get_contents($image_file);
// Call Baidu General Object and Scene Recognition API for image recognition
$result = $client->advancedGeneral($image_data);
// Print the recognition result
print_r($result);

In this example, the `advancedGeneral` method is used to perform the image recognition, and the method takes the image file data as its input parameter.

2.4 Parsing Recognition Results

The recognition result is returned in JSON format, and you can parse it to extract useful information. Below is an example of parsing the object name and confidence score from the result:


// Parse the object name and confidence score from the recognition result
foreach ($result['</span>result'] as $res) {
    $name = $res['keyword'];
    $score = $res['scor<span class="hljs-string">e'];
    echo "Object Name: $name, Confidence: $score\n";
}

The code above loops through all recognition results, extracts the object name and confidence score, and prints them out.

3. Conclusion

This tutorial provided a step-by-step guide on how to integrate Baidu's General Object and Scene Recognition API using PHP. We covered the complete process, from obtaining the API Key to image recognition and result parsing. After following this guide, you'll be able to integrate Baidu's powerful image recognition capabilities into your PHP applications and enhance the intelligence of your projects.