Posts

Showing posts from December, 2020

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