SoFunction
Updated on 2024-10-30

Python implementation of continuous graphic recognition

In this paper, we share the example of python to achieve continuous graphic recognition of the specific code, for your reference, the details are as follows

1. Tools:

1.1 Clipboard. I downloaded and installed the Clipboard Viewer (), which successfully displayed "Clipboard Viewer.exe"

1.2 Screenshot tool and set hotkeys. Save image key and exit key can be set arbitrarily, pay attention to not conflict with other hotkeys. I use WeChat screenshot, go to Settings---go to Shortcut Keys---change the screen capture key to F1.

1.3 Python, Windows environment

1.4 Register Baidu Cloud account, get Appid API Key Secret Key

1.5 Create a new folder. The folder I created is called 'Graphic Recognition' (C:\Users\Administrator\AppData\Local\Programs\Python\Python35-32\Graphic Recognition).

1.6 Under the 'Graphic Recognition' folder, there are 3 .py files, which are:; there is 1 configuration text file which is; and there is an image file which is.

1.7 The following third-party libraries are required: keyboard, PIL, aip, configparser, win32con, win32clipboard.

2 Complete code:

2.1 is the main program, can run independently, the main function is to take and save screenshots.

""" This program can be run independently, the main function is to take and save screenshots """
import sys
from time import sleep
import keyboard
from PIL import ImageGrab #pillow
from baiduap import BaiDuAPI
from getText import GetText
 
def screenShot():
   """For taking and saving screenshots."""
   print('Please press F1 to start screenshot')
   if (hotkey='f1')==None:
     print('To copy the image from the clipboard, press Ctrl+b and continue the screenshot without copying')
     if (hotkey='Ctrl+b')==None:
        sleep(0.02) #Preventing access is the last screenshot
        #Copying images from the clipboard
        im=()
        ('')
 
if __name__=='__main__':
   baiduapi=BaiDuAPI('')
   for _ in range():
     screenShot()
     texts=baiduapi.picture2Text('') 
     print(texts)
     (texts)  # Clipboard cut and paste
     sleep(0.02)
     ()
     print('To exit press Ctrl+x') 
     if (hotkey='Ctrl+x')==None:
        name=input('Please enter the name of the file to save the image recognition text:')
        f=open(name+'.txt','w')
        (texts)
        ()
        break

2.2 The program can be used independently and its main function is image text recognition.

from aip import AipOcr
import configparser
 
class BaiDuAPI:
   """Image Text Recognition"""
   # Priming methods
   def __init__(self,filePath): #self is BaiDuAPI()
     #Read work order information
     target=()
     (filePath)
     app_id=('My Work Orders','App_ID')
     app_key=('My Work Orders','App_KEY')
     secret_key=('My Work Orders','SECRET_KEY')
     =AipOcr( app_id, app_key,secret_key)
 
   def picture2Text(self,filePath):
     #Read the picture
     image=(filePath)
     texts=(image)
     #print(texts['words_result'])
     allTexts=''
     for word in texts['words_result']:
        allTexts=allTexts+('words','')
     return allTexts
    
   @staticmethod  
   def getPicture(filePath):
     with open(filePath,'rb') as fp:
        return ()
 
if __name__=='__main__':
   baiduapi=BaiDuAPI('')
   print(baiduapi.picture2Text(''))

2.3 Program, the main function is to take the text recognized from the image and save it to the clipboard.

import sys 
import  
import win32clipboard as w  
import win32con 
 
class GetText:
   
   def getText():#Read the clipboard
     () 
     d = (win32con.CF_TEXT) 
     () 
     return d 
   def setText(aString):# Write to clipboard
     () 
     () 
     (aString) 
     () 
 
if __name__=='__main__': 
   ('The Bowman of the Cloth')
   ()

2.4 File, written in notepad, fill in the relevant information obtained from Baidu cloud. Content:

[My work orders]; section
App_ID=151313**
App_KEY=1V2LlBhLUYaHu2Y9*******
SECRET_KEY=fGufC1CbiZ0tw1imTGoIsaGO******

3. Running.Launch qq and clipboard viewer.exe and run it in a python environment.

Tested: high recognition rate, can quickly capture multiple images, but every time you run the program, you can only recognize the text saved in the file image.

This is the whole content of this article.