[Python from entry to abandonment] text recognition based on Baidu OCR

[Python from entry to abandonment] text recognition based on Baidu OCR

First apply for Baidu OCR use, Baidu OCR use document description:

https://cloud.baidu.com/doc/OCR/s/Rjwvxzm3n. Install the Baidu aip library according to the document, the command line input magic is as follows:

pip install baidu-aip

After the application is used, the following information will be obtained:

from aip import AipOcr

""" Your APPID AK SK """
APP_ID ='Your App ID'
API_KEY ='Your Api Key'
SECRET_KEY ='Your Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

For example, to recognize the following picture (forOCR.png) as text, the sample code is as follows:

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 11 18:00:37 2019

@author: AiShuiShui-艾 Who Who
"""

from aip import AipOcr

#Please change the following 3 variables by yourself
from aip import AipOcr

""" Your APPID AK SK """
APP_ID ='Your App ID'
API_KEY ='Your Api Key'
SECRET_KEY ='Your Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

# Read picture
filePath = "forOCR.png"
def get_file_content(filePath):
    with open(filePath,'rb') as fp:
        return fp.read()

# Define parameter variables
options = {
  'detect_direction':'true',
  'language_type':'CHN_ENG',
}

# Call the universal text recognition interface
result = client.basicAccurate(get_file_content(filePath), options)

print(result)

The results of running in Spyder's IDE are as follows, it can be seen that the recognition effect is very good (self-brained). I originally wanted to write an answering assistant (screenshot + OCR + search), but the result was too simple, so let's write it here.

The number of characters is not enough 300, a random text:

Li Si is also from Chu Shang Cai. When I was young, I was a small official in the county. He was frightened when he saw that the rats in the latrine of the official's house were not eating, and the dogs were close to him. When Sri Lanka enters the warehouse, he observes the rats in the hamster, eats the millet, and lives under the big barn, without the worries of people and dogs. So Li Sinai sighed and said: "The sage of man is not like a rat, but he is in the ear!" He learned the emperor's art from Xun Qing. Learning has been completed, the king of Chu is not enough, but the six countries are all weak, and there is nothing to do, and they want to enter the Qin Dynasty from the west. Ci Yu Xun Qing said: "Since there is no slacker when he hears, the wanderers are in charge of the ten thousand times of the square. Today, the king of Qin wants to swallow the world and rule as the emperor. This is the autumn of lobbyists when the commoner is flying. Those who do not care about the position, this fowl and deer regard meat, human face but can force the ears. Therefore, the criticism is no greater than the humble, and the sadness is more than the poverty. For a long time in a humble place, a place of poverty, non-worldly but evil. Self-reliance on inaction, this is also the feeling of non-schist. Therefore, Si Jiangxi said that the king of Qin is stubborn."

Reference: https://cloud.tencent.com/developer/article/1489341 [python from entry to abandonment] text recognition based on Baidu OCR-Cloud + Community-Tencent Cloud