Home > Article > Backend Development > Example of Python text recognition based on Baidu AI
This article mainly introduces examples of text recognition in Python based on Baidu AI. Now I share it with you and give it a reference. Let's take a look at it
Using Baidu AI's text recognition library, the call example is made, where filePath is the path of the image, and you can pass in a picture with text for recognition.
To download the baidu-aip library, you can directly use pip to download: pip install baidu-aip, or you can download it in development tools such as PyCharm.
Then run the following code.
# -*- coding: UTF-8 -*- from aip import AipOcr import json # 定义常量 APP_ID = '9851066' API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk' SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV' # 初始化AipFace对象 aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 读取图片 filePath = "WechatIMG1.jpeg" def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } # 调用通用文字识别接口 result = aipOcr.basicGeneral(get_file_content(filePath), options) print(json.dumps(result).decode("unicode-escape"))
Output result:
{"log_id": 1424393327, "direction": 0, "words_result_num": 2, "words_result": [{"words": "It's all about money!"}, {"words": "Skill"}]}
There were several errors in my identification this time, which were related to unclear pictures. Below is the picture I identified.
Universal text recognition return data parameter details
Field | Required | Type | Description |
---|---|---|---|
No | number | Image direction, exists when detect_direction=true. |
- -1: Undefined, - 0: Forward, - 1: 90 degrees counterclockwise, - 2: 180 degrees counterclockwise, - 3: 270 degrees counterclockwise |
is the | number | unique log id, used for problem location | |
is the | number | identification result number, indicating the number of elements of words_result | |
Yes | array | Location and identification result array | |
No | string | Recognition result string |
Python is based on recursion Algorithm implementation of the Tower of Hanoi and the Fibonacci sequence
Python implements a small chatting robot function based on TCP
The above is the detailed content of Example of Python text recognition based on Baidu AI. For more information, please follow other related articles on the PHP Chinese website!