forked from BuddhiGamage/chess_robot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturn_matrix_old.py
More file actions
69 lines (53 loc) · 2.21 KB
/
return_matrix_old.py
File metadata and controls
69 lines (53 loc) · 2.21 KB
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
import cv2
import numpy as np
import pytesseract
# Load the chessboard image
image_path = '/home/buddhi/Projects/chess_robot/extracted_chessboard_no_border.jpg'
image = cv2.imread(image_path)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply adaptive thresholding to enhance the grid
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
# Find contours to detect the board
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Assume the largest contour is the chessboard
contours = sorted(contours, key=cv2.contourArea, reverse=True)
chessboard_contour = contours[0]
# Get the bounding box of the chessboard
x, y, w, h = cv2.boundingRect(chessboard_contour)
chessboard = image[y:y+h, x:x+w]
# Resize the cropped chessboard to ensure consistent cell size
chessboard = cv2.resize(chessboard, (800, 800))
# Calculate the size of each square
square_size = chessboard.shape[0] // 8
# Initialize the matrix
matrix = []
# Loop through each square
for row in range(8):
matrix_row = []
for col in range(8):
# Extract each square
square = chessboard[row * square_size:(row + 1) * square_size, col * square_size:(col + 1) * square_size]
# Preprocess the square for OCR
square_gray = cv2.cvtColor(square, cv2.COLOR_BGR2GRAY)
square_blur = cv2.GaussianBlur(square_gray, (3, 3), 0) # Reduce noise
_, square_thresh = cv2.threshold(square_blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Debug: Save or display each square
# cv2.imshow(f"Square [{row}, {col}]", square_thresh)
# cv2.waitKey(0)
# Use pytesseract to recognize text
letter = pytesseract.image_to_string(square_thresh, config='--psm 10 -c tessedit_char_whitelist=RNBKQP')
letter = letter.strip()
# Append detected letter or '1' for empty square
if letter:
matrix_row.append(letter)
else:
matrix_row.append('1')
matrix.append(matrix_row)
# Print the resulting matrix
for row in matrix:
print(row)
# Optionally, display the processed chessboard
cv2.imshow('Chessboard', chessboard)
cv2.waitKey(0)
cv2.destroyAllWindows()