-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (100 loc) · 3.79 KB
/
app.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import cv2
from mss import mss
from mss import tools as msstools
import time
import pyautogui
import numpy as np
from classes import Counter
import asyncio
from yolo_ops import build_model, detect, format_yolov5, load_classes, wrap_detection
from datetime import datetime
from pathlib import Path
class_list = load_classes()
colors = [(255, 255, 0), (0, 255, 0), (0, 255, 255), (255, 0, 0)]
is_cuda = False
net = build_model(is_cuda)
default_pause = 5
def format_datetime_for_filename():
now = datetime.now()
return now.strftime("%Y-%m-%d_%H-%M-%S")
async def click_coorditates(coordinates):
pyautogui.moveTo(coordinates[0], coordinates[1])
# Click the left mouse button
pyautogui.click(button='left')
time.sleep(0.005)
# Define endpoint to accept file uploads
async def predict(png_data, goldenclicks, wrathclicks, handclicks, deerclicks, spookyclicks, bunnyclicks):
"""
Get YOLO predictions and click if there are boxes with suitable type
"""
frame = cv2.imdecode(np.frombuffer(png_data, dtype=np.uint8), cv2.IMREAD_COLOR)
inputImage = format_yolov5(frame)
outs = detect(inputImage, net)
class_ids, confidences, boxes = wrap_detection(inputImage, outs[0])
if boxes:
for class_id, box in zip(class_ids, boxes):
coordinates = (box[0] + box[2] / 2, box[1] + box[3] / 2)
if class_id == 1:
#Golden Cookie
goldenclicks.increment()
await click_coorditates(coordinates)
print(goldenclicks)
if class_id == 2:
#Wrath Cookie
wrathclicks.increment()
await click_coorditates(coordinates)
print(wrathclicks)
if class_id == 3:
# Hand of Fate Spell
handclicks.increment()
await click_coorditates(coordinates)
print(handclicks)
if class_id == 5:
# Deers clicked
deerclicks.increment()
await click_coorditates(coordinates)
print(deerclicks)
if class_id == 6:
# Spooky clicked
spookyclicks.increment()
await click_coorditates(coordinates)
print(spookyclicks)
if class_id == 7:
# Bunnies clicked
bunnyclicks.increment()
await click_coorditates(coordinates)
print(bunnyclicks)
# cv2.imwrite(str(output_folder.joinpath(format_datetime_for_filename()).with_suffix(".png")), frame)
# if len(boxes) > 5:
# # Account for lots of cookies, click everything
# return 0.01
return default_pause
async def makescreenshot():
"""
Makes a screenshot with MSS directly to the memory
"""
with mss() as sct:
monitor = sct.monitors[1]
sct_img = sct.grab(monitor)
# Screenshot is not saved locally
png = msstools.to_png(sct_img.rgb, sct_img.size)
return png
if __name__ == "__main__":
goldenclicks = Counter(1, "Golden cookies")
wrathclicks = Counter(2, "Wrath cookies")
handclicks = Counter(4, "Hands of Fate")
deerclicks = Counter(5, "Deers")
spookyclicks = Counter(6, "Spooky")
bunnyclicks = Counter(7, "Bunnies")
async def main():
while True:
screenshot = await makescreenshot()
pause = await predict(screenshot,
goldenclicks,
wrathclicks,
handclicks,
deerclicks,
spookyclicks,
bunnyclicks)
await asyncio.sleep(pause)
asyncio.run(main())