topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 2:48 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: Dice analyzer machine project  (Read 54578 times)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #50 on: February 08, 2016, 04:42 PM »
I have ordered raspberry pi and motor control hat and touchscreen.. Gothic (dc member who helped set up servers and lives nearby and is a hardware guy) is going to come by to help me try to get things wired up in a couple of weeks.  if all goes well might have a prototype by march..

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #51 on: February 11, 2016, 10:47 PM »
Parts have arrived, I'm installing the libraries I need on the Raspberry Pi, and i have sketched out my wooden machine "box", which will be 15" long by 7.5" wide by 4" deep.
As absurd as it sounds, the best justification in my head for going through with this build is as an (absurdly) expensive (in terms of money and time) art project, and as a learning experience... Trying to justify it in any other way is even harder.

And in the back of my mind i'm hoping i come up with a better way to re-use these parts at some point..

I will post photos next week I hope.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,646
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #52 on: February 12, 2016, 06:39 AM »
As absurd as it sounds, the best justification in my head for going through with this build is as an (absurdly) expensive (in terms of money and time) art project, and as a learning experience... Trying to justify it in any other way is even harder.

There are those of us who feel that there is no better reason needed. As an example I submit exhibit 'A', which is posted here. It took me almost 3 years to get the $3,000 necessary to purchase a bike that has been sitting in storage for almost 10 years. And it will take a positively insane amount of money and time to complete what I have planned for the project.

I do it, because the deeply rooted desire to create something wondrous is obsessively strong, and because I can ... No other reason should ever be necessary, for those that are capable of understanding. ;)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #53 on: February 12, 2016, 11:49 AM »
My Raspberry Pi Die Analyzer Test Bench:

rpidiceh_ver001.jpg

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #54 on: February 12, 2016, 01:16 PM »
I'm really enjoying this thread. And even more now that you've come to the actual physical stuff. And the fact you're doing it all yourself :Thmbsup:

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #55 on: February 12, 2016, 01:25 PM »
Thanks for saying so, Ath.

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #56 on: February 12, 2016, 04:30 PM »
My Raspberry Pi Die Analyzer Test Bench:

[ Invalid Attachment ]

Now you just need a large green felt table to get busted for conducting "Vegas Night Operations" without a permit.  :)  I hope you have a check valve on that thing so your cup don't runneth over.  :)



mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #57 on: February 14, 2016, 12:42 PM »
Software running on rpi:
rpdicer.png


ps. for what it's worth, using this software as a benchmark, my desktop pc is about 20x faster than the RPI2.
« Last Edit: February 14, 2016, 02:13 PM by mouser »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #58 on: February 14, 2016, 04:10 PM »
Nicely done!

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #59 on: February 17, 2016, 10:15 PM »
Just a screenshot showing how well auto clustering (sometimes) works.  Given 159 images, it properly identified front face label, and then autonomously grouped them into equivalent clusters with no mistakes:

Screenshot - 2_17_2016 , 10_13_05 PM.pngDice analyzer machine project

No guidance was provided by the user other than to say this was a D20 die; there was no pre-training of labels (i.e. this was a pure unsupervised clustering task).
Note that the class numbers listed have no meaning at all.



Notice that it separated the 6s from the 9s, which differ only in the occurrence of a very small dot at the baseline of the 6s.  These classes can be hard for the algorithm to separate sometimes depending on how clear the dot is on a given die (for example if the camera isn't in good focus).



The 159 source camera images being processed look like this (after they've been cropped and focused to hide extraneous background):
image_01_cam.jpg
« Last Edit: February 17, 2016, 10:23 PM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #60 on: February 20, 2016, 12:22 AM »
Sample graph from the program.

NOTE: This data is not real, i don't have automated roller working yet.  I hope to have automated rolling working in next 10 days.

filelabel_report_dnew_histogram.png

The red lines you see show where the data for a fair die should lie with 95% confidence.

If these were real results, this die could be confidently labeled as NOT fair.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #61 on: February 20, 2016, 03:14 PM »
A real report from real rolls:
Screenshot - 2_20_2016 , 3_14_56 PM.pngDice analyzer machine project

This die looks like 5s come up more than they should (and 1s less)..

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,959
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #62 on: February 20, 2016, 04:57 PM »
Image looks great :)

In the image, what exactly does 'red is 0.95 confidence int. for fair die' mean?
Does it mean that within the range of the red lines in the bar-chart means you have 95% 'confidence' in the results?


Damn, I see you already answered that in your previous post. So, this die is not fair, due to the results for 5 being too high (took me a while to figure that out :-[ )
Tom

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #63 on: February 20, 2016, 05:00 PM »
In the image, what exactly does 'red is 0.95 confidence int. for fair die' mean

What is means is that if you repeated this experiment over and over with a perfectly fair die, rolling this many times, you would expect the counts to fall within those red bars, 95 times you ran the experiment out of 100.
« Last Edit: February 20, 2016, 05:07 PM by mouser »

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,959
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #64 on: February 20, 2016, 05:02 PM »
What is means is that if you repeated this experiment over and over with a perfectly fair die, rolling this many times, you would expect the counts to fall within those red bars, 95 times you ran the experiment out of 100.

thanks, I had missed your previous post (edited post to reflect that).
This is very impressive :up:
Tom

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #65 on: February 20, 2016, 05:04 PM »
So, this die is possibly fair?

I'm writing a little paper on this project and i will discuss the nature of these statistics a little bit.  These probabilistic things can be tricky to wrap your head around.

No matter what experiment you do, its ALWAYS going to be possible that the results came from a fair (or unfair) die.  The best you can do is say how unlikely it would be to get results like these if the die was fair.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #66 on: February 20, 2016, 05:11 PM »
So one aspect of the statistics im very wobbly on is the relation between the chisquared statistic and the confidence intervals for the histogram bars.

The chi-squared is evaluating how likely the complete set of results came from a fair die.  For a fair die, the frequency counts from all bins (die faces) should be roughly equal.

The histogram confidence intervals are evaluating each bin independently according to how likely the counts would be in that range.

Im not sure how to talk about how these statistics differ in their estimates..

The chi-squared suggests that this distribution wouldn't be surprising to see from a fair die, while the histogram confidence intervals seem to suggest that it would be..  I don't know if that means I'm calculating something wrong or misusing some statistic..
« Last Edit: February 20, 2016, 05:26 PM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #67 on: February 20, 2016, 06:41 PM »
Another kind of statistic you might want to look at when analyzing a die is whether there are patterns between two successive rolls (e.g. do 6's always follow 1s?).
For that we can use the same statistics as shown above when counting frequencies, but looking at a separate histogram for each die face, counting the subsequent die face frequencies.
And for pure visuals, a heatmap can help show unusually high/low frequencies of certain previous-subsequent die roll pairings:
filelabel_report_dnew_pairheatmap_histogram.png

Note: This chart has too little data to be truly informative-- it's just meant for illustrative purposes.
« Last Edit: February 20, 2016, 06:57 PM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #68 on: February 23, 2016, 07:55 PM »
Just a note that after some playing with raspberry pi i decided also to experiment with Arduino, and wired up the die roller to a relay and an arduino and now have the pc talking with the arduino and automatically engaging the die roller.  :up:
I try to stay away from hardware because i get too frustrated too quickly, but i must admit it's fun to manipulate physical mechanisms through code.
« Last Edit: February 25, 2016, 12:49 AM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #69 on: February 24, 2016, 08:28 PM »
After 2000+ rolls looks like we can be pretty confident that this D6 die i've been testing with has a real bias, and shouldn't be used in any tournaments*:
Screenshot - 2_24_2016 , 8_27_12 PM.png



* Technically speaking, the bias could be caused either by an unfair die or an insufficiently randomizing rolling machine.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #70 on: February 24, 2016, 11:27 PM »
Here's something cool.. I ran the autoclusterer function on a D20, and was unpleasantly surprised to see it was mistakenly creating two clusters of the 19 face..
Then i studied the die images closer and realized that the "18" label is worn off to make it look like a "19", and in fact the clusterer HAD successfully distinguished the two very similar looking sides:

The topmost cluster is the worn-out "18" side:
Screenshot - 2_24_2016 , 11_27_05 PM.png

You can see how it looks on the die here:
Screenshot - 2_24_2016 , 11_22_57 PM_ver001.png

Pretty cool!

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #71 on: February 25, 2016, 12:57 AM »
For those who might be curious, here is how i wired up the Arduino and Dice Roller:

First, as I posted here, I ordered a $10 battery (2xAA) powered dice spinner from Amazon.  You push a button and the base spins for a second or so, throwing the dice out to the edge where it tumbles against a sloped incline; the friction of the base and the angle of the edge seems to do quite a good job of randomly spinning dice of various shapes.

Next, I got an Arduino (R3) and a Sainsmart single channel Relay. A relay is a switch that allows an external source of power through it when it is closed.
I soldered an additional (parallel) connection from battery to motor through the relay -- basically acting as a parallel way to complete the circuit that the existing button on the device provides.  Now you can push the button to spin the dice, OR activate the relay.

Next a tiny script(sketch) was written for the Arduino, which just loops and listens to serial (usb) connection for a simple signal telling it to spin the die for a certain amount of time.  When the signal arrives, it turns on the relay (a simple command to the pin on the Arduino that drives the relay) briefly, and then turns it off.

Next, my python dice control code uses the PySerial library to send the signal to the Arduino when it wants the dice rolled.  The script already has code that notices when the dice are moving, and waits until they settle before performing another cycle of processing, so everything proceeds automatically once they die settles.

It can perform a roll and visual processing of a die about once every 1.5 seconds.

Complications:
The only real complication I've had is that the Arduino seems to sometimes loose connection with the serial communication. So my code has to check that the Arduino confirms it has received the command, and if not, occasionally disconnect and reconnect.  This works most of the time but occasionally the Arduino just won't wake up and requires a hardware reset.  This would not be acceptable for a setup that required a high-level of uptime, but for this project it's not a show stopper.
« Last Edit: February 25, 2016, 01:26 AM by mouser »

Deozaan

  • Charter Member
  • Joined in 2006
  • ***
  • Points: 1
  • Posts: 9,747
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #72 on: February 25, 2016, 01:25 AM »
Then i studied the die images closer and realized that the "18" label is worn off to make it look like a "19", and in fact the clusterer HAD successfully distinguished the two very similar looking sides

Pretty cool!

That is pretty cool. It's a case where human error may have mistaken the die value as a "19" but the computer was able to distinguish the subtle differences.  :Thmbsup:

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Dice analyzer machine project
« Reply #73 on: February 25, 2016, 01:31 AM »
I have ordered raspberry pi and motor control hat and ...
Why didn't you use that motor control hat to turn on the dice-roller? Or doesn't it have a relay for switching something external? (probably depends on the model, but a separate relay-hat isn't that expensive) Your python code on the RPi can then directly control the dice-roller without further external devices.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Dice analyzer machine project
« Reply #74 on: February 25, 2016, 01:40 AM »
Why didn't you use that motor control hat to turn on the dice-roller? Or doesn't it have a relay for switching something external? (probably depends on the model, but a separate relay-hat isn't that expensive) Your python code on the RPi can then directly control the dice-roller without further external devices.

Excellent question.

My original plan was to run the dicer software on the standalone Raspberry Pi as planned, and have the motor hat on the Raspberry Pi directly drive the motor in the dicer roller (or indeed a better replacement motor).

There were three reasons I did not go forward with this plan:
  • The first was that the Raspberry Pi is 30x slower than my PC, and my recognition code is quite slow (I need to find ways to be more clever and take a less brute force approach to image rotation alignment).  Because of this I wanted to go back to doing most of my development at least on my desktop PC.
  • The second was that the camera on the Raspberry Pi, while high resolution, has absolutely no focus control and would not focus well on anything shorter than about 24 inches from it.  This caused real trouble and I tried a bunch of kludges to fix this including misc. lens and even reading glass lenses, etc.  But I just couldnt get something working reliably.  It's really a shame the RPi camera doesnt have a focus ring.  Trying to use a standard webcam on Rpi was untsable/unsatisfactory.
  • The third was that I wanted to do minimal surgery on the $10 die roller that was made in China but shipped from Germany, because if I messed it up, it would be another month before I got a replacement.  And I wanted to still be able to engage it manually.

So right now I'm actually controlling the Arduino from my DESKTOP (windows) python code.  The Raspberry Pi version of the software can also control it (Raspberry Pi controlling an Arduino is kind of like a Turducken, but it does work).