o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Привет, вот мой код: [code] Реализуй класс BotController, чтобы он мог: 1)…
- User
Привет, вот мой код:
import time import cv2 import numpy as np from PIL import ImageGrab class HexagonDetector: def __init__(self, rgb_color=(229, 224, 224)): self.rgb_color = rgb_color self.hexagon_coords = [] # Координаты основных шестиугольников self.bottom_hexagon_coords = [] # Координаты нижних шестиугольников self.offset = [] self.bottom_offset = [] self.hexagon_order = [] # Для сортировки по ID self.bottom_hexagon_order = [] # Для сортировки нижних шестиугольников по ID def detect_hexagons(self): # 1. Сделать скриншот screenshot = np.array(ImageGrab.grab()) screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR) # 2. Обработка и обрезка изображения для основных шестиугольников hsv = cv2.cvtColor(screenshot, cv2.COLOR_BGR2HSV) lower_gray = np.array([0, 0, 20]) upper_gray = np.array([180, 255, 50]) mask_gray = cv2.inRange(hsv, lower_gray, upper_gray) contours_gray, _ = cv2.findContours(mask_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) largest_gray_contour = max(contours_gray, key=cv2.contourArea) x, y, w, h = cv2.boundingRect(largest_gray_contour) crop_top = int(h * 1 / 6) crop_bottom = int(h * 3 / 8) cropped_screenshot = screenshot[y + crop_top: y + h - crop_bottom, x: x + w] self.offset = [y + crop_top, y + h - crop_bottom, x, x + w] # 3. Обнаружение основных шестиугольников hexagon_coordinates = self._detect_hexagons_in_cropped_area(cropped_screenshot) self.hexagon_coords = hexagon_coordinates self.hexagon_order = self._assign_ids_to_hexagons(hexagon_coordinates) # 4. Обработка и обрезка изображения для нижних шестиугольников crop_top2 = int(h * 5 / 8) crop_bottom2 = int(h * 1 / 20) bottom_cropped_screenshot = screenshot[y-crop_top2:y + h - crop_bottom2, x:x + w] self.bottom_offset = [y-crop_top2, y + h - crop_bottom2, x, x + w] # 5. Обнаружение нижних шестиугольников bottom_hexagon_coordinates = self._detect_bottom_hexagons(bottom_cropped_screenshot) self.bottom_hexagon_coords = bottom_hexagon_coordinates self.bottom_hexagon_order = self._assign_ids_to_bottom_hexagons(bottom_hexagon_coordinates) # 6. Отрисовка всех шестиугольников на отдельных скриншотах self._draw_main_hexagons(cropped_screenshot) self._draw_bottom_hexagons(bottom_cropped_screenshot) return hexagon_coordinates, bottom_hexagon_coordinates def _detect_hexagons_in_cropped_area(self, cropped_screenshot): # Поиск шестиугольников, как было раньше hsv = cv2.cvtColor(cropped_screenshot, cv2.COLOR_BGR2HSV) lower_color = np.array([0, 0, 200]) upper_color = np.array([179, 255, 255]) mask = cv2.inRange(hsv, lower_color, upper_color) contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) hexagons = [] for contour in contours: perimeter = cv2.arcLength(contour, True) approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True) area = cv2.contourArea(contour) if len(approx) == 6 and area > 100: hexagons.append(approx) hexagon_coordinates = [] for hexagon in hexagons: x, y, w, h = cv2.boundingRect(hexagon) hexagon_coordinates.append((x, y, w, h)) return hexagon_coordinates def _detect_bottom_hexagons(self, bottom_cropped_screenshot): # Convert to grayscale gray = cv2.cvtColor(bottom_cropped_screenshot, cv2.COLOR_BGR2GRAY) # Apply thresholding to segment the shapes _, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY) # Adjust threshold as needed # Find contours in the thresholded image contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) bottom_hexagons = [] for contour in contours: # Get bounding rectangle x, y, w, h = cv2.boundingRect(contour) # Check if the contour has a reasonable size if w > 20 and h > 20: # Adjust these thresholds as needed bottom_hexagons.append((x, y, w, h)) # Sort the hexagons by x-coordinate to ensure they're in the correct order bottom_hexagons.sort(key=lambda h: h[0]) # Filter overlapping hexagons bottom_hexagons = self._filter_overlapping_hexagons(bottom_hexagons) return bottom_hexagons def _filter_overlapping_hexagons(self, hexagons): filtered_hexagons = [] for i, (x1, y1, w1, h1) in enumerate(hexagons): is_overlapping = False for j, (x2, y2, w2, h2) in enumerate(hexagons): if i != j: # Don't compare with itself # Check for overlap (simple rectangle intersection) if x1 < x2 + w2 and x1 + w1 > x2 and y1 < y2 + h2 and y1 + h1 > y2: # If overlapping, keep the larger hexagon if w1 * h1 < w2 * h2: is_overlapping = True break # No need to check further if already overlapping with a larger one if not is_overlapping: filtered_hexagons.append((x1, y1, w1, h1)) return filtered_hexagons def _assign_ids_to_hexagons(self, hexagons): # Функция, которая присваивает шестиугольникам ID на основе их положения hexagon_order = [] for i, hexagon in enumerate(hexagons): # Сортируем шестиугольники по расположению на поле (вручную, как в изображении) hexagon_order.append((i + 1, hexagon)) # ID, начинающийся с 1 return hexagon_order def _assign_ids_to_bottom_hexagons(self, hexagons): # Сортируем шестиугольники слева направо sorted_hexagons = sorted(hexagons, key=lambda h: h[0]) return [(i+1, hexagon) for i, hexagon in enumerate(sorted_hexagons)] # ID начинаются с 20 def _draw_main_hexagons(self, cropped_screenshot): for _, (x, y, w, h) in self.hexagon_order: cv2.rectangle(cropped_screenshot, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imwrite("screenshot_with_main_hexagons.png", cropped_screenshot) def _draw_bottom_hexagons(self, bottom_cropped_screenshot): for _, (x, y, w, h) in self.bottom_hexagon_order: cv2.rectangle(bottom_cropped_screenshot, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imwrite("screenshot_with_bottom_hexagons.png", bottom_cropped_screenshot) def analyze_stacks(self): screenshot = np.array(ImageGrab.grab()) screenshot = cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGR) stacks_info = [] bottom_stacks_info = [] # Для каждого шестиугольника из отсортированного списка по ID print(f"self.hexagon_order = {self.hexagon_order}") for hex_id, (x, y, w, h) in self.hexagon_order: stack_info = self._analyze_stack_at_coords(screenshot, x, y, w, h, hex_id, is_bottom=False) stacks_info.append(stack_info) # Анализ нижних шестиугольников print(f"self.bottom_hexagon_order = {self.bottom_hexagon_order}") for hex_id, (x, y, w, h) in self.bottom_hexagon_order: stack_info = self._analyze_stack_at_coords(screenshot, x, y, w, h, hex_id, is_bottom=True) bottom_stacks_info.append(stack_info) return stacks_info, bottom_stacks_info def _analyze_stack_at_coords(self, screenshot, x, y, w, h, hex_id, is_bottom): stack = [] if is_bottom: screenshot = screenshot[self.bottom_offset[0]:self.bottom_offset[1], self.bottom_offset[2]:self.bottom_offset[3]] else: screenshot = screenshot[self.offset[0]:self.offset[1], self.offset[2]:self.offset[3]] hexagon_region = screenshot[y:y + h, x:x + w] if is_bottom: cv2.imwrite(f"abc/___hexagon_{hex_id}_is_bottom.png", hexagon_region) # Определяем цвет по пикселю (10, 8) if is_bottom: pixel_x = 17 pixel_y = 10 else: pixel_x = 10 pixel_y = 8 color = "empty" if pixel_x < w and pixel_y < h: pixel_bgr = hexagon_region[pixel_y, pixel_x] print(f"id={hex_id}, color_bgr:{pixel_bgr}") color = self._detect_chip_color_from_pixel(pixel_bgr) stack.append(color) # Сохранение изображения с именем (ID + цвет) if not color: color = "empty" cv2.imwrite(f"abc/hexagon_{hex_id}_{color}.png", hexagon_region) return stack def _detect_chip_color_from_pixel(self, pixel_bgr): # Определение цвета по пикселю pixel_hsv = cv2.cvtColor(np.uint8([[pixel_bgr]]), cv2.COLOR_BGR2HSV)[0][0] # Откалиброванные диапазоны цветов color_ranges = { "red": ([0, 150, 50], [10, 255, 255]), "blue": ([100, 100, 50], [130, 255, 255]), "green": ([40, 50, 50], [80, 255, 255]), "purple": ([130, 50, 50], [160, 255, 255]), "orange": ([10, 100, 100], [25, 255, 255]), "yellow": ([25, 100, 100], [40, 255, 255]), "empty": ([0, 0, 0], [180, 50, 80]) # Диапазон для пустых ячеек } for color_name, (lower, upper) in color_ranges.items(): lower = np.array(lower) upper = np.array(upper) if np.all(lower <= pixel_hsv) and np.all(pixel_hsv <= upper): return color_name return "empty" # Если цвет не найден class BotBrain: def __init__(self): self.neighbors = { 1: [2, 3], 2: [1, 4, 5, 7], 3: [1, 5, 6, 8], 4: [2, 7, 9], 5: [1, 2, 3, 7, 8, 10], 6: [3, 8, 11], 7: [2, 4, 5, 9, 10, 12], 8: [3, 5, 6, 10, 11, 13], 9: [4, 7, 12, 14], 10: [5, 7, 8, 12, 13, 15], 11: [6, 8, 13, 16], 12: [7, 9, 10, 14, 15, 17], 13: [8, 10, 11, 15, 16, 18], 14: [9, 12, 17], 15: [10, 12, 13, 17, 18, 19], 16: [11, 13, 18], 17: [12, 14, 15, 19], 18: [13, 15, 16, 19], 19: [15, 17, 18] } def get_empty_slots_value(self, stacks_info, bottom_stacks_info): take_values = [ [0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 1], [0, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 2] ] #[[0, [0]*19]] * 3 # Ценность каждой из 3 главных ячеек с информацией о самых ценных внутри слотах: # [0] - ценность основного слота, [1] ценность ячеек на поле для основного слота print(f"!take_values = {take_values}") for take_id, take_slot_color in enumerate(bottom_stacks_info): if take_slot_color == ["empty"]: print("|||||||||WARNING||||||||| -- take_slot_color is empty!") # Проходимся по каждой ячейке и сравниваем соседей пустых с текущей основной снизу for id, slot_color in enumerate(stacks_info): id += 1 # Id ячеек начинается с 1 if slot_color == ["empty"]: # Только если слот пустой slot_neighbors = self.neighbors[id] for neighbor_id in slot_neighbors: #print(f"PROBLEM: stacks_info={stacks_info}, `neighbor_id-1` = {neighbor_id-1}") neighbor_color = stacks_info[neighbor_id-1] print(f"))){take_id} - take_slot_color:{take_slot_color}, neighbor_color:{neighbor_color} --{id}-- {neighbor_color == take_slot_color}") if neighbor_color == take_slot_color: # Если цвет соседа совпадает с одним из нижних цвет take_values[take_id][1][id - 1] += 1 # Повышаем ценность слота для текущей главной ячейки# ов if take_values[take_id][1][id - 1] > take_values[take_id][0]: take_values[take_id][0] = take_values[take_id][1][id - 1] # Повышаем ценность текущей главной ячейки # Сортировка по первому элементу каждого подсписка, чтобы получить самый ценный главный слот из всех sorted_take_values = sorted(take_values, key=lambda x: x[0]) # Получение наибольшего значения первого элемента max_value = sorted_take_values[-1] print(f"Отсортированный список: {sorted_take_values}") print(f"Наибольшее значение [0]: {max_value[0]}") # Если не найдено подходящих слотов, выбираем первый пустой if all(v == 0 for v in max_value[1]): for id, slot in enumerate(stacks_info): if slot == ["empty"]: values[id] = 1 break return values return max_value # Пример использования if __name__ == "__main__": time.sleep(5) # Wait for the initial empty board screenshot _detector = HexagonDetector() _brain = BotBrain() main_hexagons, bottom_hexagons = _detector.detect_hexagons() # Detect and store all hexagon coordinates print("1 - done ##################") print(f"Main hexagons: {main_hexagons}") print(f"Bottom hexagons: {bottom_hexagons}") time.sleep(4) # Wait for the game to start and some chips to be placed stacks_info, bottom_stacks_info = _detector.analyze_stacks() print(f"Stacks information: {stacks_info}") print(f"Stacks information_bottom: {bottom_stacks_info}") values = _brain.get_empty_slots_value(stacks_info, bottom_stacks_info) print("---") print(f"values = {values}") print(f"Main_cell_id: {values[2]}") #for i, v in enumerate(values[1]): # print(f"{i+1} = {v}") print("-" * 20) print("-" * 20) print("-" * 20) max_value = max(values[1]) # Находим максимальное значение в массиве max_index = values[1].index(max_value) # Находим индекс (Id) первого вхождения максимального значения print(f"То есть, нужно перетащить {values[2]+1}-ю ячейку в {max_index+1}-ю ячейку, т.к. там {max_value} соседа(ей)") - Assistant