GithubHelp home page GithubHelp logo

karayaman / play-online-chess-with-real-chess-board Goto Github PK

View Code? Open in Web Editor NEW
331.0 14.0 24.0 418 KB

Program that enables you to play online chess using real chess boards.

License: GNU General Public License v3.0

Python 100.00%
chess computer-vision

play-online-chess-with-real-chess-board's Introduction

Play online chess with real chess board

Program that enables you to play online chess using real chess boards. Using computer vision it will detect the moves you make on a chess board. After that, if it's your turn to move in the online game, it will make the necessary clicks to make the move.

Setup

  1. Turn off all the animations and extra features to keep chess board of online game as simple as possible. You can skip this step if you enter your Lichess API Access Token.

  2. Take screenshots of the chess board of an online game at starting position, one for when you play white and one for when you play black and save them as "white.JPG" and "black.JPG" similar to the images included in the source code. You can skip this step if you enable "Find chess board of online game without template images." option or enter your Lichess API Access Token.

  3. Enable auto-promotion to queen from settings of online game. You can skip this step if you enter your Lichess API Access Token.

  4. Place your webcam near to your chessboard so that all of the squares and pieces can be clearly seen by it.

  5. Remove all pieces from your chess board.

  6. Run "board_calibration.py"(Alternatively, click "Board Calibration" button in the GUI).

  7. Check that corners of your chess board are correctly detected by "board_calibration.py" and press key "q" to save detected chess board corners. You don't need to manually select chess board corners; it should be automatically detected by the program. The square covered by points (0,0), (0,1),(1,0) and (1,1) should be a8. You can rotate the image by pressing the key "r" to adjust that. Example chess board detection result:

  8. Note that "constants.bin" file is created or modified.

Usage

  1. Place pieces of chess board to their starting position.
  2. Start the online game.
  3. Run "main.py"(Alternatively, click "Start Game" button in the GUI).
  4. Switch to the online game so that program detects chess board of online game. You have 5 seconds to complete this step. You can skip this step if you enter your Lichess API Access Token.
  5. Wait until the program says "game started".
  6. Make your move if it's your turn , otherwise make your opponent's move.
  7. Notice that program actually makes your move on the internet game if it's your turn. Otherwise, wait until the program says starting and ending squares of the opponent's move.
  8. Go to step 6.

GUI

You can run "gui.py" to open the GUI. You can use it to do the steps in Setup and Usage sections and customize how you use the software. You can click "Start Game" button instead of running "main.py" and "Board Calibration" button instead of running "board_calibration.py". Also, you can enter your Lichess API Access Token via Connection→Lichess (You need to enable "Play games with the board API" while generating the token).

Video

In this section you can find video content related to the software.

Game against Stockfish 5 2000 ELO

Test game on chess.com

Test game on Lichess against Alper Karayaman

Game against Lionel45 on lichess org

Game against erpalazzi on Lichess

Play online chess with real chess board and web camera | NO DGT BOARD!

Frequently Asked Questions

What is the program doing? How does it work?

It tracks your chess board via a webcam. You should place it on top of your chess board. Make sure there is enough light in the environment and all squares are clearly visible. When you make a move on your chess board, it understands the move you made and transfers it to the chess GUI by simulating mouse clicks (It clicks the starting and ending squares of your move). This way, using your chess board, you can play chess in any chess program, either websites like lichess.org, chess.com, or desktop programs like Fritz, Chessmaster etc.

Placing a webcam on top of the chess board sounds difficult. Can I put my laptop aside with the webcam on the laptop display?

Yes, you can do that with a small chess board. However, placing a webcam on top of the chess board is recommended. Personally, while using the program I am putting my laptop aside and it gives out moves via chess gui and shows clocks. Instead of using the laptop's webcam, I disable it and use my old android phone's camera as a webcam using an app called DroidCam. I place my phone somewhere high enough (a bookshelf, for instance) so that all of the squares and pieces can be clearly seen by it.

How well does it work?

Using this software I am able to make up to 100 moves in 15+10 rapid online game without getting any errors.

I am getting error message "Move registration failed. Please redo your move." What is the problem?

The program asked you to redo your move because it understood that you had made a move. However, it failed to figure out which move you made. This can happen if your board calibration is incorrect or the color of your pieces are very similar to the color of your squares. If the latter is the case, you will get this error message when playing white piece to light square or black piece to dark square.

Why does it take forever to detect corners of the chess board?

It should detect corners of the chess board almost immediately. Please do not spend any time waiting for it to detect corners of the chess board. If it can't detect corners of the chess board almost immediately, this means that it can't see your chess board well from that position/angle. Placing your webcam somewhere a bit higher or lower might solve the issue.

Required libraries

  • opencv-python
  • python-chess
  • pyautogui
  • mss
  • numpy
  • pyttsx3
  • scikit-image
  • pygrabber
  • mac-say
  • berserk

play-online-chess-with-real-chess-board's People

Contributors

frenkel avatar karayaman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

play-online-chess-with-real-chess-board's Issues

2 Players Real Board

Plz make option for 2 player Real board option without playing online. So that I can use PGN Online

Extract moves

Is it possible to extract moves made? I want to send them to robot to move chess pieces

Speech sometimes confusing

Sometimes the program should be a bit more talkative, assuming the player doesn't look at the screen.

  • In case there is an error message or the game is interrupted, it should be told to the player
  • If the opponent's move couldn't be recognized, the expected move should be said aloud again (in case the player didn't hear or misunderstood it for any reason)

AttributeError: '_thread._local' object has no attribute 'display'

This happens once I either run main.py or start from the GUI.

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/threading.py", line 1009, in _bootstrap_inner
    self.run()
  File "commentator.py", line 22, in run
    resized_chessboard = self.game_state.get_chessboard()
  File "commentator.py", line 50, in get_chessboard
    img = np.array(np.array(self.sct.grab(monitor)))
  File ".env/lib/python3.10/site-packages/mss/base.py", line 90, in grab
    screenshot = self._grab_impl(monitor)
  File ".env/lib/python3.10/site-packages/mss/linux.py", line 428, in _grab_impl
    self._handles.display,
AttributeError: '_thread._local' object has no attribute 'display'

Anyone knows what this is about?

Game doesn't start

Hello,
I've installed the code on an Raspi Zero with Bullseye and it looks good so far. I'm able to scan the board, but if I try to start the game, I got an error (see below)
Do you know, how to solve this issue?
I run python (3.9.2) with sudo in an virtual environment - all libraries are installed...

Traceback (most recent call last):
File "/home/pi/Play-online-chess-with-real-chess-board-1.15/main.py", line 10, in
from board_basics import Board_basics
File "/home/pi/Play-online-chess-with-real-chess-board-1.15/board_basics.py", line 3, in
from skimage.metrics import structural_similarity
File "/usr/local/lib/python3.9/dist-packages/skimage/init.py", line 151, in
from ._shared import geometry
File "skimage/_shared/geometry.pyx", line 1, in init skimage._shared.geometry
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 48 from C header, got 40 from PyObject
Game finished.

is possible to promote a pawn?

HI.
This is not an issue but i want to know.
Thanks.

Amazing project!

ps.Delete this... i read better your readme Thanks!

Constants.bin

HI! Board calibration works just fine, but when it comes to starting the game the programs abruptly interrupts, and I get the same error message another user got before me, in the closed issues section (#17).
Is there a solution to this?
Many thanks in advance

Edit: apparently it is sufficient to create the file 'constant.bin' in the folder, and everything works properly

App?

This should be made into an iOS or Android app (that uses the phone camera) without necesssitating the use of a computer. Just connect the phone and play!

webcam selection

I'm having difficulty selecting the webcam. In the gui.py it does have a selector but is only showing 'default' and I am attempting to use a virtual cam client so I can use my phone to point to the board (droidcam on win10) but it not allowing this. If there is any input on this I would appreciate it.

Only one game can be played at a time

It displays the error that only one game can be played at a time. He asks me to close the other open games. But there is only one game at that time.

Write PGNs?

Can this app simply write to PGN instead of playing online?

French language

Could you please add french language ?
Below the traduction needed.
Thanks.

class French: def init(self): self.game_started = "Partie démarrée" self.move_failed = "La reconnaissance a échoué. Veuillez réessayer." def name(self, piece_type): if piece_type == chess.PAWN: return "pion" elif piece_type == chess.KNIGHT: return "cavalier" elif piece_type == chess.BISHOP: return "fou" elif piece_type == chess.ROOK: return "tour" elif piece_type == chess.QUEEN: return "reine" elif piece_type == chess.KING: return "roi" def comment(self, board, move): check = "" if board.is_checkmate(): check = " échec et mat" elif board.is_check(): check = " échec" board.pop() if board.is_kingside_castling(move): board.push(move) return "petit roc" + check if board.is_queenside_castling(move): board.push(move) return "grand roc" + check piece = board.piece_at(move.from_square) from_square = chess.square_name(move.from_square) to_square = chess.square_name(move.to_square) promotion = move.promotion is_capture = board.is_capture(move) board.push(move) comment = "" comment += self.name(piece.piece_type) comment += " " + from_square comment += " prend" if is_capture else " vers" comment += " " + to_square if promotion: comment += " promu en " + self.name(promotion) comment += check return comment

cant run "Error creating bean with name 'pendule'"

Hi.
I have this problem. Can you give me a hand to solve?

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pendule' defined in URL [jar:file:/home/xxxxxxx/Descargas/webcamChess.jar!/game/chess/graphics/Pendule.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [game.chess.graphics.Pendule]: Constructor threw exception; nested exception is java.lang.NullPointerException
Thanks!

Option for changing the default camera

Hi Karayaman,

Thank you for the great program. I am trying to play through this. It is good option to avoid screen and play with real chess board!

One input - I used Droidcam to use mobile camera. But one problem is that the board calibration program uses only the default camera. For now, I disabled the windows camera and the program identified the Droidcam. It would be good if you have option to set the camera for using.

Thanks,
Srivatsan

cv2

Ich arbeite auf Windows 11 und das Programm findet cv2 nicht. Wo finde ich cv2?

Can't run

it says that it failed to execute script main due to unhandled exeption do you kno how to fix that

Human vs Human on real Board

Hello,
Is it possible to add an option for (Human vs. Human) game? I want to play on real board and after that I want to analyze the game.
please add this option then users can play chess on their chess boards like DGT e-boards!
thanks a lot for your awesome project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.