This article is participating in Python Theme Month. See the link for details.

1. Environment and configuration requirements

The overall implementation is in Python. The third-party libraries required include AIP, PIL, Keyboard and PyInstaller. If not installed, Can be used in the CMD PIP install Baidu – AIP/pillow/install the rid_device_info_keyboard/pyinstaller instructions.

Baidu interface

Open thecloud.baidu.com/If not, please register first, then log in and click the management console, click the product on the leftserviceArtificial intelligence (ai)Character recognition, click Create Application, and enter the application nameBaidu_OCR, for exampleLearning office, and then a simple application description can be clickedImmediately create. A list of applications appears, includingAppID, API Key, Secret KeyAnd so on, which we’ll use later.

2. Specific implementation steps

The general idea of the whole program is that after using the screenshot software to get A screenshot, give baidu interface recognition, and return the results, of course, can also be local picture recognition, I use QQ own screenshot software, shortcut CTRL+ALT+A to open the screenshot, after selecting the cut area, press Enter to save to the paste board, Other screenshot software is also available.

① Obtaining a Screenshot

The Keyboard library is used to listen for keyboard action, so when a shortcut key is pressed, an action is taken. Use ImageGrab in PIL to get the clipboard images and generate local image files.

# 1. The screenshot
keyboard.wait('ctrl+alt+a')
print('Start screenshot')
 
keyboard.wait('enter')
print('Save screenshot')
time.sleep(0.1)
 
#2. Save the image
image = ImageGrab.grabclipboard()
image.save('img.png')
Copy the code

② Call Baidu AIP to identify and print the text

The application is initialized with the AppID, API Key and Secret Key generated during registration, and then the saved picture is read and the identified text is printed.

with open('img.png'.'rb') as fp:
    image = fp.read()
    text_list = client.basicAccurate(image)['words_result']
    for text in text_list:
        print(text['words'])
Copy the code

The running result is as shown in the figure:We can also add an infinite loop to keep the screenshots going.

③ Call the packer to generate exclusive recognition text applets

Pyinstaller library to achieve small program package, generate. Exe file, so that you can recognize the text at any time. On the command linepyinstaller xxx.pyExe file can be found in the generated dist folder as follows:Finally, attach the entire source code:

# Introduce screenshots software, obtain files to the local end, and identify pictures and text, and finally package
import keyboard # Control keyboard
from PIL import ImageGrab # Save images
import time
from aip import AipOcr

""" Your APPID AK SK """
APP_ID = '17076767'
API_KEY = 'Af3Rj5HALMz5AN8prSgwTH4m'
SECRET_KEY = '* * * * * * * * * * * * * * * * * *'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
i = 0

while True:
    # 1. The screenshot
    keyboard.wait('ctrl+alt+a')
    print('Start screenshot')

    keyboard.wait('enter')
    print('Save screenshot')
    time.sleep(0.1)

    #2. Save the image
    image = ImageGrab.grabclipboard()
    image.save('img{}.png'.format(i))


    "" Call universal Text Recognition (High Precision) ""

    with open('img{}.png'.format(i), 'rb') as fp:
        image = fp.read()
        text_list = client.basicAccurate(image)['words_result']
        for text in text_list:
            print(text['words'])

    i+=1
Copy the code