0

I am trying to identify all the pieces present on the Chessboard via machine learning.Currently I am predicting for a single piece.I want to load the trained model from the disk,loop through the board, get the playing square crop, and the model will predict the piece that is on that square. I want to do like this- https://www.youtube.com/watch?v=jcFvrCsoY_w

This is my current code for prediction of single piece.Help me to loop through the board and get playing square crop like above video.

import cv2
import time
import os
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
model = load_model('/home/tejas/Videos/chess/model_50x50.hd5')
label_map = list('KQRBNP_kqrbnp')

def predict(img, model, img_size=(50,50), plot=False):
    img = cv2.resize(img, img_size) 
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY )

    if plot:
        plt.imshow(img, cmap='gray')


    img = img.reshape(1, *img_size, 1) / 255
    pred = model.predict(img)
    return label_map[np.argmax(pred)]
path = '/media/tejas/creator/chess/train_data/black/r/r_90_1579252980.226565.jpg'
name_map = {
    'K':'White King',
    'Q':'White Queen',
    'R':'White Rook',
    'B':'White Bishop',
    'N':'White Knight',1y0
    'P':'White Pawn',
    '_':'Empty Square',
    'k':'Black King',
    'q':'Black Queen',
    'r':'Black Rook',
    'b':'Black Bishop',
    'n':'Black Knight',
    'p':'Black Pawn',
}

img = cv2.imread(path)
pred = predict(img, model, plot=True)
print('The image is a', name_map[pred])

Thanks !!!

  • Please see [ask] and [mcve]. – kaya3 Jan 20 '20 at 16:38
  • @kaya3 Tell me which part your not getting in my question,I will try to elaborate that one.... – TEJAS SHAHA Jan 20 '20 at 17:46
  • Well, for a start, there is nothing in your question which says you're trying to do image classification - I infer that you are trying to do image classification based on the `import cv2` line. You haven't given any sample inputs with expected results vs. actual results, and you haven't said anything about what is wrong with the code you provided. It's not that there is a part of your question which "I'm" not getting - it's that your question is missing things which you are expected to include when asking on Stack Overflow. The two pages I linked describe what you should include. – kaya3 Jan 20 '20 at 17:50
  • @kaya3 Thanks for your help.Basically Image processing is one of the part in my big project.I am trying to build robotic arm which can play chess with humans.With the help of image processing I am trying to predict the piece present on the playing square and via serialization my robotic arm will reflect corresponding move on the chessboard.Currently I am able to predict a single piece through my model,my question is how to loop through board and get playing square crop as shown in that link.This is the documentation I am trying to refer http://www.chuckorde.com/chess-vision-dnn-2.html. – TEJAS SHAHA Jan 20 '20 at 18:04
  • In that documentation they haven't told how to loop through the board,they just predicted for single piece.This is where I stuck... – TEJAS SHAHA Jan 20 '20 at 18:10

0 Answers0