With the rapid development of artificial intelligence, Optical Character Recognition (OCR) technology has become increasingly important in practical applications. Alibaba Cloud OCR, as a powerful OCR service, can efficiently recognize text in images. In this tutorial, we will introduce how to use PHP in combination with Alibaba Cloud OCR to perform text recognition.
First, you need to register for an account on the Alibaba Cloud website and enable the OCR service. Once you are registered, log in to the Alibaba Cloud Console, select "Artificial Intelligence" under the "OCR" service, and follow the instructions to enable it. After enabling the service, you will receive your Access Key ID and Secret Key, which will be used in the code later.
To use Alibaba Cloud OCR in your PHP project, you first need to install the Alibaba Cloud SDK. You can install it via Composer. Create a "composer.json" file in the root directory of your project and add the following content:
{ "require": { "aliyun/aliyun-oss-php-sdk": "~2.3" } }
Then, run the following command in the command line to install the SDK:
composer install
After installing the SDK, you can write a simple PHP script to use Alibaba Cloud OCR. Create a file named "aliyun_ocr_demo.php" and add the following code:
<?php require 'vendor/autoload.php'; // Load Alibaba Cloud SDK use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\SDK\Ocr\Ocr; use AlibabaCloud\SDK\Ocr\OcrParamType; // Alibaba Cloud AccessKey and SecretKey $accessKeyId = '<your-access-key-id>'; $accessKeySecret = '<your-access-key-secret>'; try { AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret) ->regionId('cn-shanghai') ->asDefaultClient(); $result = Ocr::v20191230()->recognizeVehicleLicense() ->contentType('img') ->imageURL('https://example.com/image.jpg') ->theme('auto') ->shouldPrintResult(true) ->paramType(OcrParamType::MULTI_FORM) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; } ?>
Be sure to replace “
Run the following command in the command line to execute the PHP script:
php aliyun_ocr_demo.php
Once executed, you will see the output of the image text recognition results.
Through this tutorial, you have learned how to integrate PHP with Alibaba Cloud OCR for basic image text recognition. Alibaba Cloud OCR not only supports vehicle plate recognition but also provides various other recognition functionalities. We hope this tutorial helps you get started quickly and effectively use Alibaba Cloud OCR technology in your development projects.