Monday, August 15, 2016

Is it there?

My mom, always grabs her mail, and she always puts it in the same place, but sometimes she forgets to mention it to me.  On occasion, when a piece of mail gets around my mail forwarding, then I'd like to know it exists.  So the last time I visited home I set up a webcam connected to my mom's computer, positioned directly above her mail spot.  From there all that was left was to set up a simple Python script to check to see if there is mail there.

I wanted something that works, and not the most complicated solution.  Grabbing the picture wasn't too hard, I tried a few methods I found online, but couldn't get one to work (this time I'm programming on Windows instead of Linux, so I may have had some issues there), but I ended up finding VideoCapture and with a few lines of code I got it working.

From there I used Pillow to scale down the image to a single pixel(think of taking the average of all the colors of an image).  This pixel is represented by a series of three numbers between 0 and 255 representing its RGB values.  For example white is (255,255,255), black is (0,0,0), blue is (0,0,255), etc. 

Now I just needed to compare that color to a stored color. I've worked with colors in Python before, and if you're thinking of the doing something with colors I would suggest using the colormath package.  Here I went with the quick and dirty and just calculated the Euclidean Distance between the two colors: d = sqrt((a1-b1)2+(a2-b2)2+(a3-b3)2).  For example the distance between lime green (34,255,0) and pink (255, 204, 229) is about 322.

If the distance (d) between the two colors exceeds a predetermined threshold, then the script sends an email to me with the original picture, and I know to bring it up in our next talk!

If you want to take a look at the code, take a look at it on my Github.

No comments:

Post a Comment