Home  >  Article  >  Backend Development  >  Tutorial: Python connects to Huawei Cloud interface to implement OCR text recognition

Tutorial: Python connects to Huawei Cloud interface to implement OCR text recognition

WBOY
WBOYOriginal
2023-07-05 13:28:371052browse

Tutorial: Python connects to the Huawei Cloud interface to implement OCR text recognition

First, in this tutorial we will learn how to use the Python programming language to connect to the Huawei Cloud interface and use OCR (Optical Character Recognition, optical Character recognition) technology realizes text recognition function. OCR technology can convert text in images into editable and searchable electronic text, which is very suitable for various text recognition scenarios.

  1. Preparation
    Before starting, we need to complete the following preparations:
  2. Register and log in to a Huawei Cloud account (https://www.huaweicloud.com/)
  3. Create an OCR service instance and obtain the corresponding API key and service endpoint (Endpoint)
  4. Install dependent libraries
    Before connecting to the Huawei Cloud interface, we need to use Python requests library to send HTTP requests. If you have not installed the requests library, you can install it through the following command:

    $ pip install requests
  5. Writing Python code

First, we need to import the requests library and define some constant variables :

import requests

# 华为云OCR服务端点
ENDPOINT = 'https://ocr.cn-north-4.myhuaweicloud.com'

# 替换成你的API密钥
API_KEY = 'your_api_key'
API_SECRET = 'your_api_secret'
  1. Implementing the OCR text recognition function

Next, we can implement the OCR text recognition function. We can upload the image to be recognized to the server and send a request to the Huawei Cloud API to obtain the text recognition result.

def recognize_text(image_path):
    url = f'{ENDPOINT}/v1.0/ocr/general-text'
    headers = {
        'Content-Type': 'application/json',
        'X-Auth-Token': 'Token ' + get_auth_token()
    }
    data = {
        "url": image_path
    }
    response = requests.post(url, headers=headers, json=data)
    result = response.json()

    if 'result' in result:
        text = ""
        for item in result['result']['words_block_list']:
            text += item['words'] + ' '
        return text
    else:
        return None


def get_auth_token():
    url = 'https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "auth": {
            "identity": {
                "methods": [
                    "password"
                ],
                "password": {
                    "user": {
                        "name": API_KEY,
                        "password": API_SECRET,
                        "domain": {
                            "name": "hwid"
                        }
                    }
                }
            },
            "scope": {
                "project": {
                    "name": "cn-north-4"
                }
            }
        }
    }
    response = requests.post(url, headers=headers, json=data)

    if 'X-Subject-Token' in response.headers:
        return response.headers['X-Subject-Token']
    else:
        return None


# 使用示例
image_path = '/path/to/your/image.jpg'
result = recognize_text(image_path)
if result:
    print('文字识别结果:', result)
else:
    print('识别失败')

In the above code example, we defined a recognize_text function, which receives the path of an image file as a parameter and returns the recognized text result. We send an HTTP POST request to the OCR interface of Huawei Cloud, and send the URL of the image to the API in the form of JSON data.

The result returned by the API is data in JSON format. We extract the text recognition results by parsing the JSON data.

Note: In the code example, we use the API key and service endpoint we got in the preparation work, please make sure to replace it with your own API key.

  1. Summary
    Through this tutorial, we learned how to use Python to connect to the OCR interface of Huawei Cloud and implement the text recognition function. OCR technology is widely used in various scenarios, such as scanning documents, license plate recognition, image translation, etc. You can further adjust the code to meet more complex recognition needs based on your actual needs.

The above is the detailed content of Tutorial: Python connects to Huawei Cloud interface to implement OCR text recognition. 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