Simple tutorial: How to connect PHP to Baidu handwriting recognition interface?
In recent years, with the development of artificial intelligence technology, handwritten text recognition has become an area of great concern. Baidu's handwritten text recognition interface provides convenient and accurate handwritten text recognition functions. The following is a simple tutorial on how to use PHP language to connect to Baidu's handwritten text recognition interface.
Step One: Preparation
First, you need to have a Baidu account and create a new application on the Baidu Developer Platform. After creating the application, you will get an API Key and Secret Key, which will be used for authentication.
Install the necessary PHP extensions: curl, php-json, php-mbstring. You can install them by running the following command in the terminal:
sudo apt-get install php-curl sudo apt-get install php-json sudo apt-get install php-mbstring
Step 2: Write the code
Create a new php file, such as "baidu_handwriting_recognition.php", add the following in the file Code:
<?php /** * 百度手写文字识别接口 * @param string $imagePath 图片路径 * @return string|bool 手写文字识别结果或者错误信息 */ function handwritingRecognition($imagePath) { $appId = 'Your App ID'; $apiKey = 'Your API Key'; $secretKey = 'Your Secret Key'; $url = 'https://aip.baidubce.com/oauth/2.0/token'; $param = array( 'grant_type' => 'client_credentials', 'client_id' => $apiKey, 'client_secret' => $secretKey ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); $result = curl_exec($ch); curl_close($ch); $accessToken = json_decode($result, true)['access_token']; $url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting'; $param = array( 'access_token' => $accessToken, 'image' => base64_encode(file_get_contents($imagePath)) ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); $result = curl_exec($ch); curl_close($ch); $resultArray = json_decode($result, true); if (isset($resultArray['error_code'])) { return $resultArray['error_msg']; } else { return $resultArray['words_result']; } } // 使用示例 $imagePath = 'path/to/your/image.jpg'; $result = handwritingRecognition($imagePath); if (is_array($result)) { foreach($result as $word) { echo $word['words'] . " "; } } else { echo $result; }
Step 3: Replace the key and path
Replace "Your App ID" in the code with the App ID of the app you created, "Your API Key" and " Replace "Your Secret Key" with your API Key and Secret Key. At the same time, replace "$imagePath" with the path of the handwritten text image you want to recognize.
Step 4: Run the code
Save and close the file, and execute the following command in the terminal to run the code:
php baidu_handwriting_recognition.php
You will see the recognition result or error message.
Summary:
Through the above simple tutorial, you have learned how to use PHP to connect to Baidu's handwritten text recognition interface to realize the handwritten text recognition function. You can modify and extend the code according to your own needs to form a more complete application. Hope this tutorial helps you!
The above is the detailed content of Simple tutorial: How to connect PHP to Baidu's handwritten text recognition interface?. For more information, please follow other related articles on the PHP Chinese website!