当前位置:网站首页>Use Baidu AIP to obtain the text in the specified area of the screen
Use Baidu AIP to obtain the text in the specified area of the screen
2022-06-12 14:31:00 【zz891422822】
Go straight to the code
Press... After running F7 Is the first coordinate ,F8 Is the second coordinate . Can get F7 and F8 Previous information
import pyautogui
from pynput.keyboard import Listener
from PIL import ImageGrab
from aip import AipOcr
# Baidu identification characters
class FanYi(object):
# Used to store coordinates
coordinate = []
# For storing data
data = []
def __init__(self, file, APP_ID, API_KEY, SECRET_KEY, num=0):
self.num = num
self.file = file
self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
if num != 0:
print(" Get the number of columns as "+str(num)+" Table for , Move the mouse to the first coordinate and press F7 Start .")
else:
pass
# Baidu character recognition
def Ocr_baidu(self):
with open(self.file, 'rb') as f:
image = f.read()
text = self.client.basicAccurate(image)
words_result=text['words_result']
if self.num == 0:
for i in range(len(words_result)):
print(words_result[i]['words'])
else:
for i in range(len(words_result)):
data_in = words_result[i]['words']
self.data.append(data_in)
j = 1
str_data = ''
for i in self.data:
if j < self.num:
str_data = str_data+i+"\t"
j += 1
else:
j = 1
str_data = str_data+i+"\n"
self.data.clear()
print(str_data)
print("##################### Move the mouse to the first coordinate position and press F7########################")
# Execute screenshot
def make_screenshot(self, x1, y1, x2, y2):
bbox = (x1, y1, x2, y2)
im = ImageGrab.grab(bbox)
im.save(self.file)
def on_press(self, key):
pass
# Get screenshot location
def on_release(self, key):
print(str(key))
if str(key) == "Key.f7":
x1,y1 = pyautogui.position()
print(" The first coordinate is :"+str(x1), str(y1)+", Move the mouse to the second coordinate position and press F8 Start getting information ")
self.coordinate.append(x1)
self.coordinate.append(y1)
elif str(key) == "Key.f8":
if len(self.coordinate) == 2:
x2,y2 = pyautogui.position()
print(" The second coordinate is :"+str(x2), str(y2))
self.coordinate.append(x2)
self.coordinate.append(y2)
print(self.coordinate)
if self.coordinate[0] < self.coordinate[2] and self.coordinate[1] < self.coordinate[3]:
self.make_screenshot(self.coordinate[0],self.coordinate[1],self.coordinate[2],self.coordinate[3])
print(" Picture obtained successfully ")
self.Ocr_baidu()
self.coordinate.clear()
elif self.coordinate[0] > self.coordinate[2] and self.coordinate[1] < self.coordinate[3]:
self.make_screenshot(self.coordinate[2],self.coordinate[1],self.coordinate[0],self.coordinate[3])
print(" Picture obtained successfully ")
self.Ocr_baidu()
self.coordinate.clear()
elif self.coordinate[0] > self.coordinate[2] and self.coordinate[1] > self.coordinate[3]:
self.make_screenshot(self.coordinate[2],self.coordinate[3],self.coordinate[0],self.coordinate[1])
print(" Picture obtained successfully ")
self.Ocr_baidu()
self.coordinate.clear()
elif self.coordinate[0] < self.coordinate[2] and self.coordinate[1] > self.coordinate[3]:
self.make_screenshot(self.coordinate[0],self.coordinate[3],self.coordinate[2],self.coordinate[1])
print(" Picture obtained successfully ")
self.Ocr_baidu()
self.coordinate.clear()
else:
print(" Please press f7 Get the first coordinate again ")
self.coordinate.clear()
else:
print(" Please press f7 Get the first coordinate again ")
self.coordinate.clear()
else:
print(" Move the mouse to the first coordinate position and press F7")
# Monitor the keyboard f7 and f8 Button
def start_listen(self):
with Listener(on_press=None, on_release=self.on_release) as listener:
listener.join()
# num=int( Number of table columns ) Default equal to 0
set_num = input(" If it is a table, please enter the number of columns in the table , If it's not a carriage return skip ")
if set_num == '':
set_num = str(0)
FanYi = FanYi(file='find_cor_img.jpg',
APP_ID='baiduaip In order to get ',
API_KEY='9zA7FyHZCpkdkicY6NBG3aZ17',
SECRET_KEY='WD9n2SDPhqIe5W9WRY0nN8MLCvQU8ROU3',
num=int(set_num)
)
FanYi.start_listen()
边栏推荐
- Shell脚本到底是什么高大上的技术吗?
- How to set, reset and reverse bit
- SystemC simulation scheduling mechanism
- TestEngine with ID ‘junit-vintage‘ failed to discover tests
- JMeter (V) pressure test of Excel file upload interface
- Huawei equipment configuration BGP as number replacement
- QT link error: undefined reference to VTable for "XXX“
- Notepad common settings
- Software package for optimization scientific research field
- C secret script Chapter 3 (detailed explanation of string function) (Section 2)
猜你喜欢

How to use Android studio to create an Alibaba cloud Internet of things app
![[early knowledge of activities] list of recent activities of livevideostack](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[early knowledge of activities] list of recent activities of livevideostack

The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)

Leetcode 2185. Counts the string containing the given prefix

Reverse order of Excel

If you want to build brand awareness, what bidding strategy can you choose?

C語言中主函數調用另外一個函數,匯編代碼理解

Player actual combat 25 unpacking module add close

TestEngine with ID ‘junit-vintage‘ failed to discover tests

程序分析与优化 - 6 循环优化
随机推荐
新技术:高效的自监督视觉预训练,局部遮挡再也不用担心!
C secret script Chapter 1: data storage (in-depth analysis) supplement
Llvm 13.1 new pass plug-in form [for win]
SystemC simulation scheduling mechanism
Word insert picture blocked by text
3. Hidden processes under the ring
How to use Android studio to create an Alibaba cloud Internet of things app
Location (I) error: command erred out with exit status
QA of some high frequency problems in oauth2 learning
Three common methods of C language array initialization ({0}, memset, for loop assignment) and their principles
Ppt cannot be opened, always prompt how to fix it
PostgreSQL14安装使用教程
QT database realizes page turning function
ADB command (I) record
SystemC time
Tlm/systemc: TLM socket binding problem
浅谈中国程序员为什么要跳槽?
Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge
OAuth2学习中的一些高频问题的QA
String concatenation and ternary operators