Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to identify chess boards #15

Open
rokopi opened this issue Jan 31, 2021 · 5 comments
Open

Failing to identify chess boards #15

rokopi opened this issue Jan 31, 2021 · 5 comments

Comments

@rokopi
Copy link

rokopi commented Jan 31, 2021

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
Copy link
Owner

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
Copy link
Author

rokopi commented Feb 2, 2021

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
Copy link
Owner

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
Copy link
Owner

PR's with improvements are also always welcome!

@rokopi
Copy link
Author

rokopi commented Mar 3, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants