-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawer.py
172 lines (137 loc) · 6.46 KB
/
drawer.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import cv2 as cv
from logger import Logger
from windowCaptureYOLO import WindowCapture
class Drawer:
"""A class to draw a rectangles to define the area of interest
for each detection object. These objects are further checked by
RFBot class with the usage of matchTemplate if they are present
or not
"""
class Rectangle:
"""A class to store the coordinates of the rectangle
"""
def __init__(self, top_left, bottom_right):
self.top_left = top_left
self.bottom_right = bottom_right
logger = Logger(True) #implicitly log to true
animusDrawedRectangle = None
healthBarDrawedRectangle = None
def __init__(self, img) -> None:
self.drawing = False
self.img = img
def getAnimusRectangle(self):
return self.animusDrawedRectangle
def getHealthBarRectangle(self):
return self.healthBarDrawedRectangle
# Mouse callback function
def drawAnimusRect(self, event, x, y, flags = None, param = None) -> None:
"""A function to draw a rectangle on the screen and store the
rectangle coordinates into rectangle class
"""
# Define global variables
global x_init, y_init
# If left mouse button is pressed, record starting (x,y) coordinates
if event == cv.EVENT_LBUTTONDOWN:
self.drawing = True
x_init, y_init = x, y
# While left mouse button is pressed, draw rectangle on the screen
elif event == cv.EVENT_MOUSEMOVE:
if self.drawing:
img_temp = self.img.copy()
cv.rectangle(img_temp, (x_init, y_init), (x, y), (0, 255, 0), thickness=2)
cv.imshow("image", img_temp)
# When left mouse button is released, record ending (x,y) coordinates
elif event == cv.EVENT_LBUTTONUP:
self.drawing = False
x_end, y_end = x, y
# Ensure upper-left coordinates are first and lower-right coordinates are second
x_start, x_end = sorted([x_init, x_end])
y_start, y_end = sorted([y_init, y_end])
# Print rectangle coordinates
#self.logger.log("Upper left coordinates: ({}, {})".format(x_start, y_start))
#self.logger.log("Lower right coordinates: ({}, {})".format(x_end, y_end))
# Draw rectangle on the image and show it
cv.rectangle(self.img, (x_start, y_start), (x_end, y_end), (0, 255, 0), thickness=2)
cv.imshow("image", self.img)
# save found rectangle
self.animusDrawedRectangle = self.Rectangle((x_start, y_start), (x_end, y_end))
self.logger.log("Drawer: Animus rectangle defined and will be observed at this location")
return
# Mouse callback function
def drawHealthBarRect(self, event, x, y, flags = None, param = None) -> None:
"""A function to draw a rectangle on the screen and store the
rectangle coordinates into rectangle class
"""
# Define global variables
global x_init, y_init
# If left mouse button is pressed, record starting (x,y) coordinates
if event == cv.EVENT_LBUTTONDOWN:
self.drawing = True
x_init, y_init = x, y
# While left mouse button is pressed, draw rectangle on the screen
elif event == cv.EVENT_MOUSEMOVE:
if self.drawing:
img_temp = self.img.copy()
cv.rectangle(img_temp, (x_init, y_init), (x, y), (0, 0, 255), thickness=2)
cv.imshow("image", img_temp)
# When left mouse button is released, record ending (x,y) coordinates
elif event == cv.EVENT_LBUTTONUP:
self.drawing = False
x_end, y_end = x, y
# Ensure upper-left coordinates are first and lower-right coordinates are second
x_start, x_end = sorted([x_init, x_end])
y_start, y_end = sorted([y_init, y_end])
# Print rectangle coordinates
#self.logger.log("Upper left coordinates: ({}, {})".format(x_start, y_start))
#self.logger.log("Lower right coordinates: ({}, {})".format(x_end, y_end))
# Draw rectangle on the image and show it
cv.rectangle(self.img, (x_start, y_start), (x_end, y_end), (0, 0, 255), thickness=2)
cv.imshow("image", self.img)
self.healthBarDrawedRectangle = self.Rectangle((x_start, y_start), (x_end, y_end))
self.logger.log("Drawer: Healthbar rectangle defined and will be observed at this location")
return
def defineAnimusRectangle(self):
"""A function to define the rectangle for animus detection
defined rectangle is either saved ina class attribute or returned
"""
# Set mouse callback function
self.logger.log("Click and drag to define the animus rectangle.Then 'q' to continue")
cv.namedWindow('image')
cv.setMouseCallback('image', self.drawAnimusRect)
# Read in the captured frame
img = self.img
# Create a window and set mouse callback function
cv.namedWindow("image")
cv.setMouseCallback("image", self.drawAnimusRect)
# Display the image and wait for a key press
cv.imshow("image", img)
cv.waitKey(0)
# Clean up and exit
cv.destroyAllWindows()
return self.animusDrawedRectangle
def defineHealthBarRectangle(self):
"""A function to define the rectangle for healthbar detection
defined rectangle is either saved ina class attribute or returned
"""
# Set mouse callback function
self.logger.log("Click and drag to define the healthbar rectangle. Then 'q' to continue")
cv.namedWindow('image')
cv.setMouseCallback('image', self.drawHealthBarRect)
# Read in the captured frame
img = self.img
# Create a window and set mouse callback function
cv.namedWindow("image")
cv.setMouseCallback("image", self.drawHealthBarRect)
# Display the image and wait for a key press
cv.imshow("image", img)
cv.waitKey(0)
# Clean up and exit
cv.destroyAllWindows()
return self.healthBarDrawedRectangle
"""if __name__ == '__main__':
wincap = WindowCapture('RF Online')
wincap.start()
drawer = Drawer(wincap.get_screenshot().getImage())
drawer.defineAnimusRectangle()
wincap.stop()
"""