-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovetotxt.py
43 lines (35 loc) · 1.4 KB
/
movetotxt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pytesseract
import pyautogui
import os
import cv2
import time
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def take_screenshot():
ss = pyautogui.screenshot()
ss.save("mouse_screenshot.png")
print("Screenshot taken")
def move_to_text(target_words=None, min_confidence=60):
take_screenshot()
time.sleep(2)
if target_words is None:
return
image_path = "mouse_screenshot.png"
image = cv2.imread(image_path)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = pytesseract.image_to_data(rgb, output_type=pytesseract.Output.DICT)
for i in range(len(results["text"])):
x, y, w, h = results["left"][i], results["top"][i], results["width"][i], results["height"][i]
text, conf = results["text"][i], int(results["conf"][i])
if conf > min_confidence and any(word.lower() in text.lower() for word in target_words):
# Move to the center of the text region
center_x = x + w // 2
center_y = y + h // 2
pyautogui.moveTo(center_x, center_y)
print(f"Moved to {text} with confidence: {conf}")
time.sleep(2)
os.remove("mouse_screenshot.png")
# Example usage:
#take_screenshot()
# move_to_text(target_words=[], min_confidence=60) # 70
# time.sleep(2)
# os.remove("mouse_screenshot.png")