Загрузка данных
import cv2
import numpy as np
import os
class ChessAnalyzer:
def __init__(self, templates_dir='templates', win_size=(64, 64)):
self.templates_dir = templates_dir
self.win_size = win_size
self.piece_symbols = {
'white_pawn': 'wP', 'white_knight': 'wN', 'white_bishop': 'wB',
'white_rook': 'wR', 'white_queen': 'wQ', 'white_king': 'wK',
'black_pawn': 'bP', 'black_knight': 'bN', 'black_bishop': 'bB',
'black_rook': 'bR', 'black_queen': 'bQ', 'black_king': 'bK'
}
self.templates = {}
self.load_templates()
def load_templates(self):
"""Загрузка PNG-шаблонов"""
if not os.path.exists(self.templates_dir):
print(f"[!] Ошибка: Папка '{self.templates_dir}' не найдена.")
return
for filename in os.listdir(self.templates_dir):
if filename.lower().endswith('.png'):
piece_name = os.path.splitext(filename)[0]
path = os.path.join(self.templates_dir, filename)
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
if img is None:
continue
img_resized = cv2.resize(img, self.win_size)
# Наложение альфа-канала на серый фон для стандартизации
if len(img_resized.shape) == 3 and img_resized.shape[2] == 4:
alpha = img_resized[:, :, 3] / 255.0
gray_piece = cv2.cvtColor(img_resized[:, :, :3], cv2.COLOR_BGR2GRAY)
bg = np.ones_like(gray_piece, dtype=np.uint8) * 128
composite_gray = (gray_piece * alpha + bg * (1 - alpha)).astype(np.uint8)
else:
composite_gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY) if len(img_resized.shape) == 3 else img_resized
self.templates[piece_name] = composite_gray
def remove_green_highlights(self, cell_img):
"""
Убирает зелёные подсветки и точки возможных ходов (Lichess / Chess.com),
заменяя их средним цветом окружающего фона клетки.
"""
if cell_img is None or cell_img.size == 0 or len(cell_img.shape) < 3:
return cell_img
# Переводим в HSV для точного выявления зелёного спектра
hsv = cv2.cvtColor(cell_img, cv2.COLOR_BGR2HSV)
# Диапазон зелёного цвета в OpenCV HSV
lower_green = np.array([30, 35, 35])
upper_green = np.array([85, 255, 255])
green_mask = cv2.inRange(hsv, lower_green, upper_green)
# Если есть зелёные пиксели (подсветка ходов)
if np.any(green_mask):
clean_img = cell_img.copy()
non_green_pixels = cell_img[green_mask == 0]
if len(non_green_pixels) > 0:
# Заменяем зеленые пиксели усредненным цветом нормальной клетки
mean_bg_color = np.mean(non_green_pixels, axis=0).astype(np.uint8)
clean_img[green_mask != 0] = mean_bg_color
return clean_img
return cell_img
def is_empty_cell(self, cell_img, stddev_threshold=12.0):
"""Проверка клетки на пустоту по дисперсии в центре"""
gray = cv2.cvtColor(cell_img, cv2.COLOR_BGR2GRAY) if len(cell_img.shape) == 3 else cell_img
h, w = gray.shape
inner_crop = gray[int(h*0.25):int(h*0.75), int(w*0.25):int(w*0.75)]
_, stddev = cv2.meanStdDev(inner_crop)
return stddev[0][0] < stddev_threshold
def identify_piece(self, cell_img):
"""Определение фигуры с предварительным удалением зеленых маркеров"""
if cell_img is None or cell_img.size == 0 or not self.templates:
return ""
# 1. Сначала «очищаем» клетку от зелёных точек/подсветок
cleaned_cell = self.remove_green_highlights(cell_img)
# 2. Проверяем клетку на пустоту
if self.is_empty_cell(cleaned_cell):
return ""
cell_resized = cv2.resize(cleaned_cell, self.win_size)
gray_cell = cv2.cvtColor(cell_resized, cv2.COLOR_BGR2GRAY) if len(cell_resized.shape) == 3 else cell_resized
best_piece = ""
max_corr = -1.0
# 3. Сравниваем очищенную клетку с шаблонами
for piece_name, template_img in self.templates.items():
res = cv2.matchTemplate(gray_cell, template_img, cv2.TM_CCOEFF_NORMED)
max_val = res[0][0]
if max_val > max_corr:
max_corr = max_val
best_piece = piece_name
if max_corr > 0.35 and best_piece:
return self.piece_symbols.get(best_piece, "")
return ""
def get_board_state(self, image_input):
"""Получение матрицы 8x8"""
if image_input is None:
return []
if isinstance(image_input, str):
if not os.path.exists(image_input):
print(f"[!] Файл '{image_input}' не найден!")
return []
board_img = cv2.imread(image_input)
else:
board_img = image_input
if board_img is None:
return []
h, w = board_img.shape[:2]
cell_h = h // 8
cell_w = w // 8
board_matrix = []
for row in range(8):
row_pieces = []
for col in range(8):
y1, y2 = row * cell_h, (row + 1) * cell_h
x1, x2 = col * cell_w, (col + 1) * cell_w
cell_crop = board_img[y1:y2, x1:x2]
symbol = self.identify_piece(cell_crop)
row_pieces.append(symbol)
board_matrix.append(row_pieces)
return board_matrix
def analyze_initial_board(self, image_input):
return self.get_board_state(image_input)