In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Python how to achieve screenshot recognition text", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Python how to achieve screenshot recognition text" bar!
Preface
System: win10
Python version: python3.8.6
Pycharm version: pycharm 2021.1.2 (Professional Edition)
Complete code download: Baidu_Ocr.py-Python
First, obtain Baidu Intelligent Cloud token
After logging in to Baidu Intelligent Cloud, find the text recognition under the artificial intelligence interface-> manage the interface to create and apply character recognition.
After the application is created, record the information of AppID, API key and Secret Key provided by the background interface.
Next, obtain the use of Token according to the official documentation.
# encoding:utf-8import requests# client_id is the AK obtained by the official website, and client_secret is the SKhost obtained by the official website = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=wgEHks0l6MCpalbs3lPuFX1U&client_secret=Z4Rn4ghBx9k06fUYPmSEIRbCFvWFxLyQ'response = requests.get (host) if response: print (response.json () [' access_token']).
After acquisition, token calls Baidu API to recognize and extract text from the picture.
# encoding:utf-8import requestsimport base64''' Universal character recognition (High Precision version)''request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"# binary mode to open the picture file f = open (' picture .png') 'rb') img = base64.b64encode (f.read ()) params = {"image": img} # call access_token=' 24.0d99efe8a0454ffd8d620b632c58cccc.2592000.1639986425.282335-24065278'request_url = request_url + "? access_token=" + access_tokenheaders = {'content-type':' application/x-www-form-urlencoded'} response = requests.post (request_url, data=params, headers=headers) if response: print (response.json ())
The acquired token is in json format.
In this step, we can see that the identified file is returned in json format, so in order to achieve the effect of taking out the text, we need to parse the return value in json format.
Third, set up windowed programs for easy use
The third-party class library that implements window visualization is Tkinter. You can enter pip install tkinter on the terminal to download and install it yourself.
Import tkinter module package to build our visual window, if the functions are screenshot recognition, Chinese and English separation, text recognition is automatically sent to the clipboard
From tkinter import * # create window window = Tk () # window name window.title ('qcc-tnw') # set window size window.geometry (' 400x600') # window title setting l=Label (window,text=' Baidu API call', bg='green', fg='white', font= ('Arial', 12), width=30, height=2) l.pack () # set text receive box E1 = Text (window,width='100',height='100') # set operation Button Click the run text recognition window window, text for button text, font for button text font, width for button width, height for button height, command for running function "img_txt = Button (window, text=' text recognition', font= ('Arial', 10), width=15, height=1) # set operation Button Click cut_en = Button (window, text='), font= ('Arial', 10), width=15, height=1) # to set the operation Button Click cut_cn = Button (window, text=' Chinese Segmentation', font= ('Arial', 10), width=15, height=1) # Parameter anchor='nw' indicates that in the north by west direction of the window, that is, the upper left corner of the window, img_txt.pack (anchor='nw') cut_en.pack (anchor='nw') cut_cn.pack (anchor='nw') # so that the constructed window is always displayed on the top layer of the desktop window.wm_attributes ('-topmost',1) window.mainloop ()
Fourth, realize the automatic saving of screenshots
Through the above analysis of Baidu interface, it is found that the interface does not support the extraction of files in the clipboard.
Therefore, the pictures intercepted through the PIL library are saved locally from the clipboard, and the text recognition in the pictures is realized by calling Baidu's interface.
Input pip install PIL into the installation terminal of PIL
From PIL import ImageGrab# takes out the clipboard file and saves it to the local image = ImageGrab.grabclipboard () s = 'xxx.png'image.save (s) # Baidu API calls request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"f = open (s) 'rb') img = base64.b64encode (f.read ()) params = {"image": img} access_token=' 24.ee0e97cbc00530d449464a563e628b8d.2592000.1640228774.282335-24065278'request_url = request_url + "? access_token=" + access_tokenheaders = {'content-type':' application/x-www-form-urlencoded'} response = requests.post (request_url, data=params, headers=headers) for i in response.json () ['words_result']: print (I [' words'])
After completion, you can use the screenshot function of qq or Wechat to take screenshots and run the program.
5. Display the recognized text output in the window text box and send the text to the clipboard if response: for i in response.json () ['words_result']: # accept the recognized text E1.insert ("insert", I [' words'] +'\ n') E1.pack (side=LEFT) # write the recognized text to the clipboard pyperclip.copy (E1.get) "end"))
6. Extract the Chinese (English) text from the recognized text
The judgment here is relatively simple to if len ('.join (re.findall (r' [A-Za-z]', I ['words'])
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.