update litris with board detection

This commit is contained in:
2023-07-29 20:12:51 +02:00
parent a9b76cde97
commit c9081bd371
2 changed files with 51 additions and 20 deletions

View File

@@ -26,6 +26,7 @@ class NewStoneID(threading.Thread):
self.vision_stun = Vision()
self.stone_coordinates = np.zeros((4, 4), dtype=object)
self.data_coordinates = np.zeros((20, 20), dtype=object)
self.fill_data_coordinates()
self.needles = {1: cv.imread("litris/blue_needle.jpg", cv.IMREAD_UNCHANGED)}
@@ -133,6 +134,49 @@ class NewStoneID(threading.Thread):
return e, i
return None, None
def get_current_board_state(self):
# get an updated image of the game
screenshot = self.capture_window.get_screenshot()
#screenshot = cv.imread("litris/main_playfield.jpg")
screenshot = screenshot[40:1380, 610:1950] # 1000,1000
# cv.imshow("screenshot", screenshot)
# cv.waitKey(150)
# continue
data_coords = np.zeros((20, 20), dtype=object)
#field = Pickaxe_Field()
for needle_key in self.needles.keys():
#gray_needle = cv.cvtColor(self.needles[needle_key], cv.COLOR_BGR2GRAY)
#thresh_needle = cv.threshold(gray_needle, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1]
rectangles = self.vision_stun.find(screenshot, self.needles[needle_key], 0.8, 100)
if len(rectangles) == 0:
continue
points = self.vision_stun.get_click_points(rectangles)
for point in points:
x, y = self.point_in_rect(point)
if x is not None and y is not None:
data_coords[x][y] = int(needle_key)
#self.change_value(x, y, int(needle_key))
# print(field.data_value_grid)
# cv.circle(screenshot, points[0], 7, (0, 255, 0), -1)
#output_image = self.vision_stun.draw_rectangles(screenshot, rectangles)
#cv.imshow("output_image", output_image)
#cv.waitKey(150)
return data_coords.tolist()
def point_in_rect(self, point):
for e in range(0, 20, 1):
for i in range(0, 20, 1):
x1, y1, w, h = self.data_coordinates[e][i]
x2, y2 = x1 + w, y1 + h
x, y = point
if x1 < x and x < x2:
if y1 < y and y < y2:
return e, i
return None, None
def fill_data_coordinates(self):
# 610 to 1950 = 1340 - 76 / 20 = 63
# 40 to 1380 = 1340 - 76 / 20 = 63
@@ -142,4 +186,7 @@ class NewStoneID(threading.Thread):
i_spacing = 4
for e in range(0, 4, 1):
for i in range(0, 4, 1):
self.stone_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim]
self.stone_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim]
for e in range(0, 20, 1):
for i in range(0, 20, 1):
self.data_coordinates[e][i] = [(i * dim) + (i * i_spacing), (e * dim) + (e * e_spacing), dim, dim]