Home > Backend Development > Python Tutorial > Example of Python text recognition based on Baidu AI

Example of Python text recognition based on Baidu AI

不言
Release: 2018-04-21 16:00:45
Original
3242 people have browsed it

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"))
Copy after login

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


##directionNonumberImage direction, exists when detect_direction=true. log_id is the number unique log id, used for problem locationwords_result_num is the number identification result number, indicating the number of elements of words_resultwords_resultYesarrayLocation and identification result array wordsNo stringRecognition result string
Field Required Type Description
- -1: Undefined,
- 0: Forward,
- 1: 90 degrees counterclockwise,
- 2: 180 degrees counterclockwise,
- 3: 270 degrees counterclockwise
Related recommendations:


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!

Related labels:
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