Szymon Machajewski
Created November 21, 2023

Chess Vision-Inclusive

Bridges the digital and physical worlds by using computer vision to capture moves from a screen to enable real-time, inclusive gameplay.

16
Chess Vision-Inclusive

Story

Read more

Custom parts and enclosures

Artist concept

Schematics

Artist concept

Code

Chess computer vision module

Python
from ultralytics import YOLO
from shapely.geometry import Polygon
import numpy as np

# Load the model.
model = YOLO('yolov8n.pt')

# Training.
results = model.train(
    data='data.yaml',
    imgsz=640,
    epochs=100,
    batch=8,
    name='yolov8n_corners')

model_trained = YOLO("runs/detect/yolov8n_corners/weights/best.pt")

results = model_trained.predict(source='1.jpgresized.jpg', line_thickness=1, conf=0.25, save_txt=True, save=True)

# Assuming FEN_annotation is defined somewhere
FEN_annotation = ...

def connect_square_to_detection(detections, square):
    # Implement this function based on your requirements
    # It should return the piece on the given square using detections
    pass

board_FEN = []
corrected_FEN = []
complete_board_FEN = []

for line in FEN_annotation:
    line_to_FEN = []
    for square in line:
        piece_on_square = connect_square_to_detection(detections, square)    
        line_to_FEN.append(piece_on_square)
    corrected_FEN = [i.replace('empty', '1') for i in line_to_FEN]
    print(corrected_FEN)
    board_FEN.append(corrected_FEN)

complete_board_FEN = [''.join(line) for line in board_FEN] 

to_FEN = '/'.join(complete_board_FEN)

print("https://lichess.org/analysis/" + to_FEN)

def calculate_iou(box_1, box_2):
    poly_1 = Polygon(box_1)
    poly_2 = Polygon(box_2)
    iou = poly_1.intersection(poly_2).area / poly_1.union(poly_2).area
    return iou

ptsT = ...  # Assuming ptsT is defined somewhere
ptsL = ...  # Assuming ptsL is defined somewhere

xA = ptsT[0][0]
xB = ptsT[1][0]

y9 = ptsL[0][1]
y8 = ptsL[1][1]

a8 = np.array([[xA,y9], [xB, y9], [xB, y8], [xA, y8]])
a7 = np.array([[xA,y8], [xB, y8], [xB, y7], [xA, y7]])

def order_points(pts):
    rect = np.zeros((4, 2), dtype="float32")
    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]
    
    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]
    
    return rect

# Use order_points function with the appropriate points
ordered_pts_a8 = order_points(a8)
ordered_pts_a7 = order_points(a7)

Credits

Szymon Machajewski

Szymon Machajewski

1 project • 1 follower

Comments