Home  >  Article  >  Backend Development  >  Alibaba Cloud OCR and PHP development: a practical tutorial example

Alibaba Cloud OCR and PHP development: a practical tutorial example

WBOY
WBOYOriginal
2023-07-19 16:29:071144browse

Alibaba Cloud OCR and PHP development: a practical tutorial example

  1. Introduction
    With the development of artificial intelligence and big data technology, OCR (Optical Character Recognition, optical character recognition) technology Applications in various fields are becoming increasingly widespread. Alibaba Cloud OCR is an excellent OCR solution provided by Alibaba Cloud, which can realize the recognition, extraction and conversion of text in images. This article will introduce how to use Alibaba Cloud OCR and PHP for development, and give a practical tutorial example.
  2. Preparation
    Before starting to use Alibaba Cloud OCR, we need to do some preparations:
    (1) Register an Alibaba Cloud account and activate the OCR service. For specific operations, please refer to Alibaba Cloud official documentation.
    (2) Install PHP. Make sure PHP is properly installed in your development environment and able to run PHP scripts.
    (3) Obtain the API key of Alibaba Cloud OCR. Create an AccessKey in the Alibaba Cloud console and record the AccessKeyId and AccessKeySecret.
  3. Using Alibaba Cloud OCR SDK
    Alibaba Cloud provides SDKs for OCR development using multiple programming languages, including PHP SDK. We can install Alibaba Cloud OCR SDK through Composer:
composer require alibabacloud/sdk

After the installation is complete, we can create an OCR Client and set the AccessKeyId and AccessKeySecret:

<?php
require_once __DIR__ . '/vendor/autoload.php';

use AlibabaCloudClientAlibabaCloud;
use AlibabaCloudClientExceptionClientException;
use AlibabaCloudClientExceptionServerException;
use AlibabaCloudOcrOcr;

// 设置AccessKeyId和AccessKeySecret
AlibabaCloud::accessKeyClient('{AccessKeyId}', '{AccessKeySecret}')
    ->regionId('cn-hangzhou')
    ->name('default')
    ->timeout(20)
    ->connectTimeout(0)
    ->asGlobalClient();

// 创建OCR Client
$client = new Ocr();

Next, we You can use OCR Client to call the OCR API. For example, the following sample code demonstrates how to use the OCR API to perform text recognition on a picture:

<?php
try {
    $result = $client->v20191230()->recognizeBusinessCard()
        ->withImageUrl('https://example.com/image.jpg')
        ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}

The above code implements text recognition on a business card picture and prints out the recognition results.

  1. Example: Using Alibaba Cloud OCR to identify business license information
    Next, we will use an example to demonstrate how to use Alibaba Cloud OCR to identify business license information. Suppose we have a business license picture business_license.jpg, from which we want to extract the company name, legal representative, registered capital and other information.
<?php
try {
    $result = $client->v20191230()->recognizeBusinessLicense()
        ->withImageUrl('https://example.com/business_license.jpg')
        ->request();
    
    $info = $result->toArray()['data']['config']['tables'][0]['rows'];
    
    $companyName = $info[0]['row'][1]['text'];
    $legalPerson = $info[6]['row'][1]['text'];
    $registeredCapital = $info[8]['row'][1]['text'];
    
    echo '公司名称:' . $companyName . PHP_EOL;
    echo '法定代表人:' . $legalPerson . PHP_EOL;
    echo '注册资本:' . $registeredCapital . PHP_EOL;
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}

The above code extracts the information from the business license image and prints it out by calling the recognizeBusinessLicense interface.

  1. Summary
    This article introduces how to use Alibaba Cloud OCR and PHP to develop text recognition functions, and gives a practical example. I hope this tutorial can help you better apply OCR technology in actual development. Of course, Alibaba Cloud OCR has many other functions and interfaces, which can be adjusted and expanded according to specific needs. For detailed API documentation and sample code, please refer to Alibaba Cloud official documentation. I wish you success!

The above is the detailed content of Alibaba Cloud OCR and PHP development: a practical tutorial example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn