-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision.py
57 lines (45 loc) · 2 KB
/
vision.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
# From @learncodebygaming on github
import cv2 as cv
import numpy as np
class Vision:
# given a list of [x, y, w, h] rectangles returned by find(), convert those into a list of
# [x, y] positions in the center of those rectangles where we can click on those found items
def get_click_points(self, rectangles):
points = []
# Loop over all the rectangles
for (x, y, w, h) in rectangles:
# Determine the center position
center_x = x + int(w/2)
center_y = y + int(h/2)
# Save the points
points.append((center_x, center_y))
return points
# given a list of [x, y, w, h] rectangles and a canvas image to draw on, return an image with
# all of those rectangles drawn
def draw_rectangles(self, haystack_img, rectangles):
# these colors are actually BGR
line_color = (0, 255, 0)
line_type = cv.LINE_4
for (x, y, w, h) in rectangles:
# determine the box positions
top_left = (x, y)
bottom_right = (x + w, y + h)
# draw the box
cv.rectangle(haystack_img, top_left, bottom_right, line_color, lineType=line_type)
return haystack_img
# given a list of [x, y] positions and a canvas image to draw on, return an image with all
# of those click points drawn on as crosshairs
def draw_crosshairs(self, haystack_img, points):
# these colors are actually BGR
marker_color = (255, 0, 255)
marker_type = cv.MARKER_CROSS
for (center_x, center_y) in points:
# draw the center point
cv.drawMarker(haystack_img, (center_x, center_y), marker_color, marker_type)
return haystack_img
def centeroid(self, point_list):
point_list = np.asarray(point_list, dtype=np.int32)
length = point_list.shape[0]
sum_x = np.sum(point_list[:, 0])
sum_y = np.sum(point_list[:, 1])
return [np.floor_divide(sum_x, length), np.floor_divide(sum_y, length)]