CV Chess
This is the page for the CV Chess project.
Contents
Project Overview
This project aims to use a camera, a raspberry pi, and computer vision software to recognize the movements of chess pieces in a game of chess. The final product will be able to recognize individual pieces, as well as determine the change in positions (squares) they occupy. This will ultimately yield a project that can verify valid moves, transcribe games, and perhaps implement an AI to act as an opponent of a lone player.
Powerpoint: https://drive.google.com/open?id=1YedAReM6O2aCmyuhbexrzyZo2OzDirnc
Demo day powerpoint(rough draft): https://drive.google.com/file/d/1Bia1HT9QYs1-1q9sQBzvP3-1uzONgBiM/view?usp=sharing
Team Members
Robert Goodloe
Nhut Dang
TA: Ethan Shry
Instructor: Prof. Jim Feher
Objectives
- Use OpenCV software to recognize chess board.
- Use OpenCV software to recognize the chess pieces.
- Use OpenCV software to recognize the movement of the pieces.
- Transcribe game of chess and present in user-friendly fashion.
- Add an AI component that responds to a users movements. It would display a move which the user must execute on behalf of the AI.
Challenges
- Limited knowledge of Raspberry Pi
- Zero knowledge using OpenCV or any computer vision software
- Have been told that nobody has gotten OpenCV compiled and running in ESE 205 despite several attempts
- Using object recognition to differentiate between similar pieces i.e. bishop versus pawn
- testing
Gantt Chart
Budget
Item needed | Unit | Price | Total(max) | Description | Link |
---|---|---|---|---|---|
Chessboard | 1 | 9.76 | 9.76 | wood | https://www.amazon.com/Yellow-Mountain-Imports-Magnetic-Travel/dp/B0019FATKG/ref=sr_1_10?s=toys-and-games&ie=UTF8&qid=1538161920&sr=1-10&keywords=chess+set |
color dot(optional) | 1 | 9.99 | 9.99 | may needed if we are going to color code the chessboard's pieces. | https://www.amazon.com/gp/aw/d/B06X6LT9G9/ref=sspa_mw_detail_4?ie=UTF8&psc=1 |
Pi touchscreen (optional) | 1 | 79.95+tax+ship | 79.95+tax+ship | may needed if we are going to color code the chessboard's pieces. | https://www.amazon.com/gp/aw/d/B06X6LT9G9/ref=sspa_mw_detail_4?ie=UTF8&psc=1 |
tripod | 1 | free | free | for mounting | https://www.thingiverse.com/thing:2348881 |
Plexiglass(optional) | 1 | 12 | 12 | clear board | https://www.amazon.com/MIFFLIN-Plexiglass-Transparent-Acrylic-Plastic/dp/B076VR7D9C/ref=sr_1_28?s=office-products&ie=UTF8&qid=1538162741&sr=1-28&keywords=clear%2Bglass&th=1 |
111.7 |
Design Solutions
Hardware: The project used a Raspberry Pi model 2b and an accompanying Raspberry Pi camera module. The camera was connected to the Pi using a 24-inch cable and mounted to the bottom of a tripod. The original design was intended to have a 3D printed tripod to mount the Pi camera, but this was changed. The tripod was able to stand directly above the chessboard and the camera directed straight down at the board managed to capture it in its entirety.
Software: The program was written in python and implemented using Object-oriented programming. The program had a graphical user interface created using the tkinter library. The GUI allowed the user to start a new game, choose the difficulty of the CPU opponent, and choose which color they would like to play.
Board Recognition Algorithm: At the start of the program the GUI prompts the user to clear the board so that it may initialize the board. This process took a picture of the empty board and performed an analysis of the image in which it could properly map the chessboard. In doing so, the photo was first turned into a grayscale copy of itself. Using the OpenCV library's built in algorithm Adaptive Thresholding, pixels above a thrshold rgb color value were turned white and those below turned black. This binarization of the image allowed for contours (shapes) within the image to be more easily found. The largest of these contours found was the edge of the chessboard. Given the edge of the chessboard, a black copy of the image was created and the original image within the edge of the chessboard was written onto the black image. This effectively created a new image with the chessboard surround by black. This is important because it excludes any noise that may interfere with the following OpenCV algorithms. Next, using the OpenCV Canny Edge Detection, the edges of the chessboard squares were inferred from the sharpest gradient of the color values in the image. Given these edges, the Hough Line Transform was able to infer the lines in the image, mapping a grid onto the image. The corners of the squares were determined by the intersection of the vertical and horizontal lines. After removing duplicates and sorting the corners by both row and column, the squares were created. Using these squares, the program was able to initialize a board that would hold much of the information about the game.
Detecting Piece Movement Algorithm:
Objects:
Line Square These Square objects held much of the information used throughout the entirety of a program during a game of chess. The Squares stored their standard algebraic notation (i.e. a1, a2,...). It also stored their state. The state of a square
Board
Chess Engine
Game
Camera
The image analysis was done using OpenCV an open source computer vision library.
The OpenCV library had many built in algorithms that proved helpful. Among these were the adaptive thresholding, Hough Line transform, and Canny edge detection algorithms.
References
https://www.pyimagesearch.com/2018/04/09/how-to-quickly-build-a-deep-learning-image-dataset/
https://web.stanford.edu/class/cs231a/prev_projects_2016/CS_231A_Final_Report.pdf
https://www.pyimagesearch.com/2018/04/16/keras-and-convolutional-neural-networks-cnns/
https://www.pyimagesearch.com/2017/10/02/deep-learning-on-the-raspberry-pi-with-opencv/
https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/
Tkinter Tutorials: https://www.youtube.com/watch?v=HjNHATw6XgY&list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk
Similar Projects: https://github.com/jayhack/CVChess
Open CV: https://docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html
https://docs.opencv.org/3.4.0/d9/db0/tutorial_hough_lines.html
https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
Corner
(Fast) https://www.edwardrosten.com/work/fast.html
Lines
https://stackoverflow.com/questions/45322630/how-to-detect-lines-in-opencv