Posts

What is CamChess?

Image
CamChess is a free open source program that enables you to play against a computer with a standard tournament board and pieces. It uses a camera to identify moves on the board. It is optimised for a vinyl roll up board with green squares and black and "light wood" coloured plastic pieces. I got mine from Chess Direct , but I expect that there are other suppliers. I am using a Raspberry Pi Zero with an attached ZeroCam to capture board images. I fixed them to a door frame with sticky tape. The Pi Zero sends the board images to a more conveniently placed Raspberry Pi 4B. You can also use a different computer running Linux, Windows or MacOS. I used WiFi for my initial testing, but I found it was much better to set up the Zero as an Ethernet gadget, and connect to my desktop computer via a USB cable (which carries both data and power). The ZeroCam overlooks the chess board. The board is offset to the side so that I can get through the door. That nonetheless 

How to Use CamChess

Image
I will describe the process of setting up CamChess process in later sections. In this section, I will assume that everything has been set up correctly, and that CamChess has just been launched. When CamChess is launched, it sends a request for a picture to the camera server. The camera server responds by taking a picture and sending it back to CamChess. CamChess displays the picture. (If the board is not sufficiently well centred, see Setting up the Camera .) CamChess will show the message shown above when it successfully identifies the chess board. The OpenCV program that I use for that was not designed to identify chess boards with pieces on. (It was designed to identify checker patterns to calibrating lenses.) Nonetheless, with my set up, the program has never failed to identify the board with the pieces set up in the start position. If you are having trouble, it is best to start with an empty board, and then set up the start position. (CamChess uses the image of the start position

How CamChess Works

Image
CamChess uses OpenCV and Numpy to analyse images of the chess board. It uses python-chess for its chess related processing. After taking its first picture of the board, CamChess needs to recognise the board and find its four corners. OpenCV has a very helpful function called findChessboardCorners . Its main use appears to be for identifying checker board patterns for calibrating lenses, but it serves our purpose. It finds the inner corners of the chess board. Unfortunately, it does not return them in a consistent order for different images, but this problem is easily side stepped, as we shall see. The 49 inner corners are shown in red and blue above. We are particularly interested in the red corners. If we have the coordinates of the red corners, we can easily find approximate coordinates for the outer corners of the chess board (shown in green). We can do this by simple proportion along the two long diagonals. We can find the coordinates of the top-left and bottom right inner corne

Setting up a Camera Server

Image
The picture shows the Raspberry Pi Zero W with the ZeroCam attached. (The ZeroCam still has its protective cover over the lens in that picture. You will need to take that off before taking pictures. I got my ZeroCam from Pimoroni , but the PiHut appears to have an equivalent product.) The ZeroCam is a special camera for the Pi Zero. You can use the more expensive official camera, but it requires a special adaptor cable to connect it to the Zero. The ZeroCam is fine for CamChess. I housed the Zero in a "premium case" from PiHut. Despite its name, it was the cheapest case available. Here is a helpful video showing the set up of another ZeroCam project. In my initial testing, I used a Raspberry Pi Zero W as the camera server, and sent pictures to my desktop computer over WiFi. More recently, I have configured the Zero as an Ethernet gadget and send the pictures to a Raspberry Pi 4B via a USB cable, which also powers the Zero. The set up for that is mostly the same as for WiFi,

Setting up the Camera

Image
You can set up the camera using CamSet.py, which you can copy from here . (You will need to install the Python image modules before you can run CamSet.py, but you do not need to install OpenCV. See the next section . You will also need to have the camera server running on the Zero. See the previous section .) CamSet.py requests an image from the camera server whenever you click the left mouse button. (The CamSet.py window must have focus when you do that.) When the image is received, it is displayed in the window. I took these pictures with a Raspberry Pi Zero with an attached ZeroCam taped to a door frame looking down at a chess board 1.55 m below. This section shows the effect of the camera settings at the beginning of the camera server  PiCam.py . There is no specific documentation for the ZeroCam, but it supports the standard Python  Pi Camera interface . Here is a camera image with no geometry settings applied at the default resolution of 720 x 480: The board is reasonably well ce

Installing CamChess

Image
You can download CamChess from here by clicking the Code button. The file paths at the beginning of  CamChess.py should configure themselves to work for Linux, an Apple Mac and Windows. They are based on the assumption that you have set up a CamChess directory in your home directory, and that the relevant files are in that directory. You can edit any of these file paths. (Chess_Games.txt contains any games that you have saved, and the Pieces directory contains images of the chess pieces for display on chess diagrams.) The file path for Stockfish is Linux specific. That is where Stockfish will be installed if you install it from the repository. If you are using Windows, you will need to edit that path to that of the Stockfish executable. You can download Stockfish from  here . (You can use another engine, but I do not know another that supports the Skill Level setting. If that setting is not supported by the engine, the Level button will not work. The options that the engine supports