kochsebastian / ChessVisionBot

Chessbot using computer vision to play on any chess website
GNU General Public License v3.0
43 stars 22 forks source link

Failing to identify chess boards #15

Open rokopi opened 3 years ago

rokopi commented 3 years ago

Hi Sebastian,

I want to start by saying that I really am enjoying working with this project!

I've managed to get the program to run on Win64, but it is really struggling to find chessboards. With mss 4.0.3 I get an error:

Exception in Tkinter callback Traceback (most recent call last): File "F:\ProgramData\Anaconda3\lib\tkinter__init.py", line 1705, in call__ return self.func(*args) File "F:\ChessVisionBot\code\main.py", line 77, in start_playing resized_chessboard = chessboard_detection.get_chessboard(game_state) File "F:\ChessVisionBot\code\chessboard_detection.py", line 113, in get_chessboard img = np.array(sct.grab(monitor)) File "F:\ProgramData\Anaconda3\Lib\site-packages\mss\base.py", line 88, in grab return self._grab_impl(monitor) File "F:\ProgramData\Anaconda3\Lib\site-packages\mss\windows.py", line 262, in _grab_impl self._bmi.bmiHeader.biWidth = width TypeError: int expected instead of float

I tried forcing x1, x2, y1, y2 to be int, but that causes the system to find invalid chessboards:

resized_chessboard

which then lead to bad FEN strings and cause the chess engine to crash.

I would really appreciate any help you can offer here! I'm hoping it might be something simple like a missing package, but I'm not sure where to look at this point.

Thank you.

kochsebastian commented 3 years ago

I'm not that fond of mss. For me it's also sometime throws weird bugs. Forcing int's sound like a good idea. Maybe the chessboard detection also detects the region or something is wrong with the coordinate scaling etc.
What you could also consider is to switch to pyautogui screenshots instead. This is a little slower, but still fast.

rokopi commented 3 years ago

Thank you for your response.

I've noticed that if I manually identify the chessboard coordinates, force them to be int, and set all the scaling factors to 1, the rest of the program, including mss, functions as I expected (or maybe even better than I expected!). I've made a few slight adjustments to have better control of the engine strength, but overall I am very thrilled with the quality of this program.

I'm still unable to get the "find chessboard from image" functions to work on full screenshots though, unfortunately. They still select the above image as a chessboard (this pic was the resized_chessboard variable). I have to admit that the ML methodology for chessboard location identification is a bit beyond me.

kochsebastian commented 3 years ago

Finding the chessboard is actually not an ML algorithm but an optimization algorithm. It looks for a description of the chessboard which looks for the most coherent description of the chessboard. But in the chessboard detection file should also be some legacy code which just looks for 64 squares and then calculates the convex hull. This is an inferior approach, but maybe it's more stable for you

kochsebastian commented 3 years ago

PR's with improvements are also always welcome!

rokopi commented 3 years ago

Thank you. I've been busy recently, but will try to make some PRs soon.

With the help of your iPython notebook, I think I found the problem with chessboard identification. If the chessboard is too small, the hough gradient Y is very weak. Interestingly, this is not a problem with the hough gradient X. I wonder what the issue is here.

hough_gradients