Live data from Hacker News

Show HN: My weekend project - Chessboard picture recognition

codebazaar.blogspot.com

31–40 of 57 posts

Re: Show HN: My weekend project - Chessboard picture recognition

#31
post #6

Earlier quoted context omitted.

Knights can make it everywhere. The only position you'd never see is pawns on the first or last rank (their movement is forward and they promote to stronger pieces when reaching the other side). EDIT: Another feature ... in a properly set up board, the bottom right square is always white.

Nor will you ever find both bishops of one color on the same color square.

You have to be careful there, you can only make that assertion if you can prove that no pawn has been promoted to a bishop (eg if all 8 pawns are still on the board, or 7 pawns and 2 queens etc).

Re: Show HN: My weekend project - Chessboard picture recognition

#32
post #2

I don't know anything about "chess theory" but are there some squares that only certain pieces can get to? I don't think it's possible for a Knight to ever be on some squares, so maybe you could use logic like that to narrow down the piece detection. It may be easier to just track the game state from the start instead of trying to identify the position of the board at a random snapshot in time. Cool project though, s…

The only thing I can think of is that pawns cannot be in the first rank nor the last rank.

Re: Show HN: My weekend project - Chessboard picture recognition

#33
post #3

"For example, the program is able to determine whether each square is white or black by calculating its average color and comparing it to the black and white colors" I bet you could get away with determining the color of a corner square and assuming the rest.

Thanks for the feedback. I did not think about this. Some of the square edges actually belongs to the surroundings squares because the cut is not 100% accurate and the chess piece are not always position at the center of the square but I could get around that. The way this algorithm works is simple. I compare the RGB values for each pixel of a square to the black color and white color. Black is Red=0, Green=0, Blue=0…

Most pieces are positioned in the middle of a square. A simple way to recognize the color of the piece and its square is to do a flood fill with a bit of tolerance (say 32/256) from the center pixel. The area covered by the flood fill will only contain the pixels making up the piece, while the area outside the flood fill will have pixels the color of the square. Reflections might cause a bit of noise, but a slight gaussian blur should be enough to get over that.

Re: Show HN: My weekend project - Chessboard picture recognition

#34
post #2

I don't know anything about "chess theory" but are there some squares that only certain pieces can get to? I don't think it's possible for a Knight to ever be on some squares, so maybe you could use logic like that to narrow down the piece detection. It may be easier to just track the game state from the start instead of trying to identify the position of the board at a random snapshot in time. Cool project though, s…

Pawns can't be on the first or last rank (they'd have to be promoted). There are plenty of impossible (illegal) positions like double mate, and more complicated ones where for example there are two dark-colored bishops + 8 not promoted pawns. Perhaps that kind of info will come in handy when the image algo is in doubt.

Re: Show HN: My weekend project - Chessboard picture recognition

#35
It seems like you can avoid having to recognize the pieces altogether if you are taking photos of each move during a match, since they have a known starting position.

Edit: what I mean is that in this case the problem becomes answering "does this place on the board have a piece on it" not "which piece is on this place on the board." Once you know which places on the board have any pieces whatsoever you can simply infer between moves which pieces moved where.

Re: Show HN: My weekend project - Chessboard picture recognition

#36
I may have a good solution:

Take two photos. One at the starting position and one at the current position you'd like to save. You don't need to take them in order (take the current position, reset the board, and take the other photo). This way you know what the pieces are from the starting position, then use an algo to match based on that.

Also some good hints for most standard sets while checking your current position...both kings are always on the board and they are usually the tallest. there is never more than one queen per side, and its usually a close height to the king, but has a circular crown. Rooks are circular on top, bishops come to a point, if there are more than two pawns per side that gives them away. Knights could be singled out by eliminating the rest.

Good luck!

Re: Show HN: My weekend project - Chessboard picture recognition

#37
yes..like the other hackers are saying....you have to deal with this problem like a poker player tries to guess his opponents cards....so the first step is to create the piece expectation function...which will basically calculate the probability that a piece is (say knight) at square x,y given the probabilty expectation functions of all the other squares..one way to create probablilities for all squares would be to use a combination of dynamic programming and neural networks...you will have to create a dynamic programming algorithm that sort of trains itself(by storing data rather than changing its code!!) do better and better as you tell it how right or wrong it was....so the topics you are looking to cover in the next exercise (assuming tht u have already done some reading on image processing) are basic probability theory,dynamic programming and neural nets...those are my two cents on the problem.

Re: Show HN: My weekend project - Chessboard picture recognition

#38
You might look into the academic literature for OCR, which this will reduce to. (Most AI problems are "How can I apply human intelligence to cleverly reduce this problem from something which appears to require intelligence to something which actually only requires well-understood math?")

I think the punchline is likely to be that you end up taking dot products of feature vectors generated by library code and writing fairly little chess-related code. Sorry to burst your bubble if you thought this was going to be very sexy. On the plus side, it will work, and if you're as good at programming as you appear to be you're almost certain to succeed on this.

P.S. I hope you like taking photos because odds are you're going to get the opportunity to take lots.

Re: Show HN: My weekend project - Chessboard picture recognition

#40
post #2

I don't know anything about "chess theory" but are there some squares that only certain pieces can get to? I don't think it's possible for a Knight to ever be on some squares, so maybe you could use logic like that to narrow down the piece detection. It may be easier to just track the game state from the start instead of trying to identify the position of the board at a random snapshot in time. Cool project though, s…

Thanks for leaving a comment. "It may be easier to just track the game state from the start instead of trying to identify the position of the board at a random snapshot in time." This is certainly true! But the original idea was to take a picture (probably from a mobile phone) and continue the game online. Taking a picture after each most is a lot of work :D

attach a camera (or phone) to the clock. It takes a picture whenever you hit the clock (i.e. after every move), and records the move history. You can stop scribbling it on those little pads.
Post reply on HN