Home > Backend Development > PHP Tutorial > How to use PHP and Alibaba Cloud OCR to quickly extract text from images?

How to use PHP and Alibaba Cloud OCR to quickly extract text from images?

PHPz
Release: 2023-07-19 16:24:02
Original
1776 people have browsed it

How to use PHP and Alibaba Cloud OCR to quickly extract text from images?

In recent years, with the rapid development of artificial intelligence technology, OCR (optical character recognition) technology has been widely used in various fields. Alibaba Cloud OCR is a powerful OCR service that can identify and extract text information in images, providing developers with great convenience. This article will introduce how to use PHP language and Alibaba Cloud OCR service to quickly extract text from images, and provide corresponding code examples.

Step 1: Preparation
In order to use the Alibaba Cloud OCR service, we first need to create an OCR service instance on the Alibaba Cloud console. Go to the Alibaba Cloud official website, click on the console to enter the management interface, and then select "Text Recognition" -> "OCR (Optical Character Recognition)" to enter the OCR service page. Click "Create Instance", fill in the relevant information according to the steps of the creation wizard, and create an OCR service instance.

Step 2: Obtain the access key
After creating the OCR service instance, we need to obtain the access key (Access Key) and the access key password (Access Key Secret) for OCR service through API for a visit. You can find the corresponding access key information on the instance details page of the console.

Step 3: Install PHP Alibaba Cloud SDK
In order to facilitate the use of Alibaba Cloud OCR service, we can use PHP SDK to call related APIs. First, we need to use Composer to install the PHP Alibaba Cloud SDK. Execute the following command in the command line:

composer require alibabacloud/sdk
Copy after login

Step 4: Write a code example
The following is a simple PHP code example that demonstrates how to use PHP and Alibaba Cloud OCR service to extract text from images. First, Alibaba Cloud SDK and signature classes must be introduced.

<?php
require 'vendor/autoload.php';
use AlibabaCloudClientAlibabaCloud;
use AlibabaCloudClientExceptionClientException;
use AlibabaCloudClientExceptionServerException;

// 设置访问密钥信息
AlibabaCloud::accessKeyClient('Your Access Key', 'Your Access Key Secret')
    ->regionId('cn-shanghai')
    ->asDefaultClient();

// 调用API提取图片文字
function extractTextFromImage($imageUrl)
{
    $task = [
        'ImageUrl' => $imageUrl,
    ];
    
    try {
        $result = AlibabaCloud::rpc()
            ->product('ocr')
            ->version('2019-12-30')
            ->action('RecognizeCharacter')
            ->method('POST')
            ->host('ocr.cn-shanghai.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => 'cn-shanghai',
                    'Tasks' => json_encode([$task]),
                ],
            ])
            ->request();
            
        $response = $result->toArray();
        
        // 在这里处理提取到的文字信息
        // ...
        
        return $response;
    } catch (ClientException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    } catch (ServerException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    }
}

// 测试
$inputImageUrl = 'http://example.com/image.jpg';
$response = extractTextFromImage($inputImageUrl);
var_dump($response);
?>
Copy after login

In the above code, we first initialize based on the access key information obtained. Then, we defined a function named extractTextFromImage, which is used to call the API of the Alibaba Cloud OCR service to extract text from the image. By passing in the URL of the image, we can set the corresponding parameters in $task, and then call the RecognizeCharacter interface to perform text recognition.

In practical applications, we can obtain the extracted text information by processing $response and process it accordingly.

Step 5: Usage example
Save the above code as a PHP file, and access the file through the command line or browser to extract text from the image. It should be noted that Your Access Key and Your Access Key Secret in the code need to be replaced with the actual access key information, and $inputImageUrl The URL of the image to be processed.

Summary
It is very convenient to use PHP and Alibaba Cloud OCR service to quickly extract text from images. By correctly configuring the access key information and using the Alibaba Cloud SDK to call the API, we can easily implement the text recognition function. I hope this article can help developers who need to use OCR technology to speed up development and improve efficiency.

The above is the detailed content of How to use PHP and Alibaba Cloud OCR to quickly extract text from images?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template