-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCursorMonitoring_script
74 lines (55 loc) · 2.5 KB
/
CursorMonitoring_script
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import cv2
import numpy as np
import pynput
import time
import pyautogui
from random import randint
# Define the screenshot size
size = (70, 70)
# Set the threshold for movement detection in pixels
threshold = 10000
# Monitor the segmented image for changes
while True:
# Get the mouse and keyboard controllers
mouse = pynput.mouse.Controller()
keyboard = pynput.keyboard.Controller()
# Get the current mouse position
x, y = pyautogui.position()
# Take a screenshot at the mouse position
screenshot = pyautogui.screenshot(region=(x - size[0] // 2, y - size[1] // 2, size[0], size[1]))
# Convert the screenshot to grayscale
gray = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
# Fine-tune the dark and bright pixels using adaptive thresholding
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# Find the contours in the thresholded image
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# If there are any contours, find the largest one
if len(contours) > 0:
largest_contour = max(contours, key=cv2.contourArea)
# Create a mask for the largest contour
mask = np.zeros_like(thresh)
cv2.drawContours(mask, [largest_contour], -1, 255, -3)
# If this is not the first frame, compare the new mask to the previous mask
if 'prev_mask' in locals():
# Calculate the absolute difference between the masks
diff = cv2.absdiff(mask, prev_mask)
# Count the number of white pixels in the difference image
count = np.count_nonzero(diff)
# If enough pixels have moved, print "movement detected"
if count >= threshold:
print("Collecting your fish...")
keyboard.press(pynput.keyboard.Key.shift)
time.sleep(1)
mouse.click(pynput.mouse.Button.right, 1)
keyboard.release(pynput.keyboard.Key.shift)
delay = randint(2, 5)
print("Recasting Fishing Line... " + str(delay) + "s")
time.sleep(delay)
# Store the current mask for comparison in the next frame
prev_mask = mask
time.sleep(0.0001)
# Display the current screenshot and previous mask images
cv2.imshow('screenshot', np.array(screenshot))
cv2.imshow('prev_mask', prev_mask)
# Wait for a key press to update the images
cv2.waitKey(1)