Dice-Distinguisher-Box, from Zurich University of Applied Sciences (ZHAW). In development!
A project to display Computer-Vision Machine-Learning and Statistics in a playful way.
![](https://private-user-images.githubusercontent.com/64488738/320233571-46b51cfd-4974-4864-b354-d0685827d79d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDU5MDksIm5iZiI6MTcyNDE0NTYwOSwicGF0aCI6Ii82NDQ4ODczOC8zMjAyMzM1NzEtNDZiNTFjZmQtNDk3NC00ODY0LWIzNTQtZDA2ODU4MjdkNzlkLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA5MjAwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk4YmU0MjgyY2IzYzVjOGUyYjY0NmJhODI1NTMxMDIxMmYzNTUwYjJiZTliNTRjNGJkZTg4YWRjN2Y4MGY0MzImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.XHme3-e7THlGMVVzS9BK5o4qkkvCaxuAkfdXFrQhXJg)
![](https://private-user-images.githubusercontent.com/64488738/326241638-65bcf43e-40bd-4e6f-898c-62354c127ece.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDU5MDksIm5iZiI6MTcyNDE0NTYwOSwicGF0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zMDAmWC1BbXotU2lnbmF0dXJlPTQ0NGMwMGM3MjVmMjQ2MmMzMTNmZmZhNTQ5ZWQwZjFhMDk0NjY5OTVjYmFlZmUzNzhkNWM1MmUwYWM4Y2Q0Y2MmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.l4fSAKntZI4rt-T-dhoQ6WsRJ48eH18jQhBPdQPjgFA)
Dices used in this project from: https://trick-dice.com/
-
4 dice are used 2 red and 2 whites, 1 red is biased towards #3 and 1 white is biased towards #6, the others are normal die
-
choose 2 dice (1 red and 1 white) from the 4, the other 2 put aside.
-
roll the two selected dice at the same time
-
based on the scene detection, the image is automatically taken and sent to processing to identify the die
-
Numbers are saved
-
statistics of the rolled dice are displayed
-
based on a binomial test it is determined whether there is evidence for a manipulated die
-
rerun with the same selected dice
With traditional Computer vision, we can distinguish 3 states of the scene "empty scene", "moving scene" and "dice still on the mat scene". If there is a still scene we can send an image to an algorithm that crops out the dice in the images which then get sent to the next step to the CNN
for each of the frames from the video stream, we can calculate a simple metric by subtracting the maximum pixel value from the mean of the grayscale image. If this score is small then we can assume that the image is empty. Based on the difference to the last image's score we can also infer the "moving scene" state. So the "dice still on the mat scene" is just based on a threshold that has to be set. This simple detection is efficient and proved to be robust.
If the still state is detected, a single frame is taken and is ready for isolating the dices on the images. By converting the images to the HSV colorspace the removal of the green background is easy, as well as getting the 2 different colors of the dice.
with the ConnectedComponents algorithm as well as the Bounding Rectangle, the isolation is swift. the output of these chained operations are grayscale, nearly quadratic cropped images of the individual dice in the image
There is a small CNN with just 3 layers, an increasing amount of filters, Dropout, and ReLu activation functions with a classification Head at the end and CrossEntropy loss. The network is trained with an augmentation strategy on images like these:
![](https://private-user-images.githubusercontent.com/64488738/326742000-c6e68c11-3828-4188-b71b-95074504c4f8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDU5MDksIm5iZiI6MTcyNDE0NTYwOSwicGF0aCI6Ii82NDQ4ODczOC8zMjY3NDIwMDAtYzZlNjhjMTEtMzgyOC00MTg4LWI3MWItOTUwNzQ1MDRjNGY4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA5MjAwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTE0NGNhNzczNGJhZDc2MjkxNmE4MWE2ZTMxZDc5YTI3YzAxM2Q5MWRhZDkxZmY4YTVjZDU3Y2I4OTAwMTBlMDEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.AiqC6Ttn0KNBIaSwgzyYWjPniPKJCND7_CRlticZuoU)
The dataset to train the model is combined from the object detection dataset https://github.com/nell-byler/dice_detection (just cut the dice according to the box coordinates) and own images taken from the dice detector Box. On a testset with 10 samples each (confusion matrix on the left Accuracy=100%), the outcome distribution (right )indicates that the classes were well-learned by the model.
![](https://private-user-images.githubusercontent.com/64488738/326749589-8f528b2b-d547-4cff-8721-49673aa087c5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDU5MDksIm5iZiI6MTcyNDE0NTYwOSwicGF0aCI6Ii82NDQ4ODczOC8zMjY3NDk1ODktOGY1MjhiMmItZDU0Ny00Y2ZmLTg3MjEtNDk2NzNhYTA4N2M1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA5MjAwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQxMmU3NTgyNjMxZDYzMTAwNzY3NWRjY2EwOGYwYmFhYzg2NGQ0ZTYzODMzMjFhODY0NjI0MjYxNTZiNDU5ZjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.KI4Wjebg2fNNX9caMMk9KCd4HXRfGLPX50AJNypErAE)
Of course, the whole part with traditional CV and the CNN could be replaced by just an object detection pipeline...
Since the power of a Chisquare test is very low for a small number of rolls it is decided to assume that we are suspicious that the red die might be biased toward 3 and the white die might be biased towards 6 Therefore we can formulate the Null hypothesis that if a die is not fake the true probability is equal to 1/6. Hence we can perform a binomial test to test this for both die with the observed amounts. To confidently say that this dice is biased towards our suspected numbers we can not solely rely on the p-value of said test. We have to consider the statistical power (The likelihood of not rejecting the Null hypothesis if the alternative is true, or the Probability of making an error of type 2 https://www.statisticshowto.com/probability-and-statistics/statistics-definitions/statistical-power/). For a more precise power analysis, we'd also need the real probability of the fake die, which we could empirically get by rolling the dice several hundred times. Not that the power of this test is reliant on the true probability of the alternative and also the sample size
![](https://private-user-images.githubusercontent.com/64488738/326738316-ff6f8fbe-b07a-4b51-8ae5-6535a90aa594.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxNDU5MDksIm5iZiI6MTcyNDE0NTYwOSwicGF0aCI6Ii82NDQ4ODczOC8zMjY3MzgzMTYtZmY2ZjhmYmUtYjA3YS00YjUxLThhZTUtNjUzNWE5MGFhNTk0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIwVDA5MjAwOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJiZWVlMTU2M2Y4NGEwYTM4MTY0NjRmNDY3OWQ1MjhjMjExY2MxNzUyY2NjYjhlZDM3YTY1N2ExOWViNThhOGUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.yFJMN7zZq5tN-wCqFh7YKAccqQHWajg89sTO10FofIE)
working app in:
/app
testing different UI's , frameworks, QT etc.:
/dev_app
Jupyter notebooks for model , statistical tests and plotting:
/workspace
- clone Repo
git clone https://github.com/buehlpa/Dice
- go to /app and create
python -m venv dice_env
- activate environment
source dice_env/bin/activate
- install dependecies
pip install -r requirements_unix.txt
- start app
python main.py
This module was tested on a Rasperry PI-5 with Raspian