<jupyter_start><jupyter_text><jupyter_code># install required libraries
!pip install -q cairosvg
!pip install -q chess
# import modules and libraries
import chess
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches

from pathlib import Path
from cairosvg import svg2png
from sklearn.model_selection import train_test_split
# mount google drive
from google.colab import drive
drive.mount('/content/drive')
# define data dir
data_dir = Path('/content/drive/MyDrive/MAID/502 CI/project/data')
# visualize chessboard
b = chess.Board(None)
b.set_piece_at(chess.parse_square('a1'), chess.Piece.from_symbol('Q'))
b
# create dataset template
image_names = []
boards = []
labels = []

for idx, piece in enumerate(['k', 'K', 'q', 'Q']):
    for pos in chess.SQUARE_NAMES:
        b = chess.Board(None)
        b.set_piece_at(chess.parse_square(pos), chess.Piece.from_symbol(piece))
        
        # store image names, boards and labels
        img_name = 'image_' + str(idx) + str(pos) + '.png'
        image_names.append(img_name)
        boards.append(b)
        labels.append(idx)
# split data into training and validation
train_names, valid_names, train_boards, valid_boards, train_labels, valid_labels = train_test_split(image_names, boards, labels, train_size=0.8, shuffle=True, stratify=labels, random_state=42)
train_names[:5]
valid_names[:5]
# print size
len(train_names), len(valid_names)
train_boards[:5]
train_labels[:5]
%%bash

mkdir -p "/content/drive/MyDrive/MAID/502 CI/project/data"/{train/{images/{blackKing,whiteKing,blackQueen,whiteQueen},annot_csv},valid/{images/{blackKing,whiteKing,blackQueen,whiteQueen},annot_csv}}

# create dictionary to convert labels to class names
class_names = {
    0: 'blackKing',
    1: 'whiteKing',
    2: 'blackQueen',
    3: 'whiteQueen',
}
# save training images
for img_name, b, lbl in zip(train_names, train_boards, train_labels):
    img_path = str(data_dir / 'train' / 'images' / class_names[lbl] / img_name)
    svg2png(bytestring=chess.svg.board(b, size=224),write_to=img_path)
# save validation images
for img_name, b, lbl in zip(valid_names, valid_boards, valid_labels):
    img_path = str(data_dir / 'valid' / 'images' / class_names[lbl] / img_name)
    svg2png(bytestring=chess.svg.board(b, size=224),write_to=img_path)
# define variables for automated labelling
border_width = 8
width = 224

low = border_width
high = width - border_width
gap = (high - low) // 8
low, high, gap
# define x coordinate at the center of chess position a1
x_center0 = low + gap / 2
x_center0
# define y coordinate at the center of chess position a1
y_center0 = high - gap / 2
y_center0
# find all possible center coordinates
xvals = np.arange(x_center0, y_center0+1, gap, dtype=int)
yvals = np.arange(y_center0, x_center0-1, -gap, dtype=int)
xvals, yvals
# create center coordinates for every chess position
coordinates = []
for xcenter in xvals:
    for ycenter in yvals:
        coordinates.append([xcenter, ycenter])
        
coordinates[:5]
len(coordinates)
# create name for every chess position
pos = []
for filename in chess.FILE_NAMES:
    for rankname in chess.RANK_NAMES:
        pos.append(filename + rankname)
        
pos[:5]
# merge the above two lists into a dictionary
pos_dict = dict(zip(pos, coordinates))
## visualize random images and their corresponding annotations

# get random index
idx = np.random.randint(len(train_names))

# get filename and stem of random index
_stem = Path(train_names[idx]).stem
img_name = Path(train_names[idx]).name

# get position and label
pos = _stem.split('_')[1][1:]
label = int(_stem.split('_')[1][0])

# get image path
img_path = str(data_dir / 'train' / 'images' / class_names[label] / img_name)

# read image
img = plt.imread(img_path)

# find center coordinates
xcenter, ycenter = pos_dict[pos]

# plot image with annotations
plt.imshow(img, origin='upper')
rect = patches.Rectangle((xcenter - gap/2, ycenter - gap/2), gap, gap, linewidth=1, edgecolor='b', facecolor='none')
plt.gca().add_patch(rect)

plt.xticks([])
plt.yticks([])

plt.show()
print('Label is', class_names[label])
# create train csv file
train_data = []
for img_name in train_names:
    # get filename and stem of random index
    _stem = Path(img_name).stem

    # get position, label and class name
    pos = _stem.split('_')[1][1:]
    label = int(_stem.split('_')[1][0])
    class_name = class_names[label]

    # find center coordinates
    xcenter, ycenter = pos_dict[pos]

    # store details
    train_data.append([img_name, class_name, xcenter, ycenter, gap, gap])
# store all annotations in a simple format
train_data = pd.DataFrame(train_data, columns=['filename', 'label', 'xcenter', 'ycenter', 'width', 'height'])
train_data.head()
# save as csv file
train_data.to_csv(data_dir / 'train' / 'annot_csv' / 'train.csv', index=False)
# create validation csv file
valid_data = []
for img_name in valid_names:
    # get filename and stem of random index
    _stem = Path(img_name).stem

    # get position, label and class name
    pos = _stem.split('_')[1][1:]
    label = int(_stem.split('_')[1][0])
    class_name = class_names[label]

    # find center coordinates
    xcenter, ycenter = pos_dict[pos]

    # store details
    valid_data.append([img_name, class_name, xcenter, ycenter, gap, gap])

# store all annotations in a simple format
valid_data = pd.DataFrame(valid_data, columns=['filename', 'label', 'xcenter', 'ycenter', 'width', 'height'])

# save as csv file
valid_data.to_csv(data_dir / 'valid' / 'annot_csv' / 'valid.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12337315029417008, 'fraction_numerical': 0.011410233553218043, 'mean_word_length': 4.593220338983051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '359798', 'license': 'MIT', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1869, 'n_words': 517, 'path': 'code/1.1 create_images_with_labels.ipynb', 'repo_name': 'faizankshaikh/chessDetection', 'size': '5609'}