Monday, December 19, 2016

New Python Project: Is there a way to build nonpartisan voting districts?

Gerrymandering  seems to make the news every couple of years and every couple of years I wander if there's a solution to the problem.  It seems that somebody should have figured out how to make fair and equitable congressional districts by now, right?  

I'm a little curious if something like that is possible using some geospatial analysis.  What are the roadblocks and what's stopping somebody from pulling this off?  Who knows?  It's an excuse to learn more about Python and its application to mapping and such.

How much experience do I have with Python and geospatial analysis?  none!  Well, I've done a little, but with esri's arcpy Python module.  This should be WAY harder, but a fun challenge. 

Poking around the internet, it looks like Python needs some packages to do what I want (or at least get me started).  A place called SciPy can hook me up.  

They allow installing via pip, which I've done a couple of times in the past.  They even tell me exactly what to type.  So, to Command Prompt we go and we'll do the install.


Easy peasy.  Next time, we'll see what we've installed and how it can help us.  

Monday, November 21, 2016

PI Critter Cam Day 16: Better Pictures

I set up the critter cam yesterday afternoon after adjusting the setup.py file in the pi-timolo folder to rotate the pictures 180 degrees and take pictures at a 1080 x 1920 resolution.  Luckily, a bird came by to test out the new settings.



The picture looks MUCH better!  However, we have a couple issue where it's taking and keeping a TON of pictures and it's overwriting all of the previous pictures.  

So, within the setup.py file, I'm going to change the threshold setting to 50, which should make the picture taker less sensitive.  I'm also going to change motionnumon to False so it'll use a date/time stamp to name the file.  I'm also change motionnummax to 0 in case it's using that setting as justification to delete stuff out there.  

We'll set it out again tomorrow morning to see how it does.  This time, I'll try setting it on the ground.

Sunday, November 20, 2016

PI Critter Cam Day 15: New Enclosures

After realizing that the previous enclosure wasn't going to work, I dug through my scrap wood bin and slapped together a few 2x2s to build a makeshift food holder.  I decided to use one of those birdfood suet bricks, which seemed easier to manage. 

I also sacrificed one of my sandwich boxes that I take to work to house the Raspberry PI, drilling a small 3/8" hole for the camera.  I sandwiched tack between the lunchbox and the PI in a feeble attempt to keep the water out. I used a clamp to attach the box to the birdfood holder.  

It's certainly not the most sophisticated design, but should work as a temporary solution.  Here's what we have.  


You can see the camera poking out from this angle with the new battery and PI inside.


I let it sit out all afternoon and it actually took some bird pictures, albeit upside down.    Yet, it's exciting to see this thing actually work!

NOTE:  I took the suet container out, just leaving the food brick.  I was worried the birds couldn't get to the food.  I should've moved that thread rod too!



Also, the battery held up really well.  The PI ran all day as there were some night pictures in the folder.  Although, I'm not sure exactly how long as I haven't been including the wifi dongle in the box to conserve battery, which messes up the PI's clock. 

 So, our next goals:  Get the pics right side up and see how much longer the battery lasts with higher quality pics and the wifi left running.


Friday, November 18, 2016

Pi Critter Cam Day 14: A Step Back

Well, things didn't go as planned. 

Every time I start the Pi, it'll take a few pictures, but soon stop working.  This is according to the TOP application accessible through the terminal that, I think, acts like a Task Manager and shows all the running applications.  pi-timolo shows up for about 15 minutes or so, then disappears from the list of active programs.  Somethings amiss.  

I tried reinstalling pi-timolo, but that didn't help.  

I then tried updating the Raspian operating system using the instructions at raspberrypi.org, which essentially has me run two lines in the terminal.

sudo apt-get update
sudo apt-get dist-upgrade

And that's pretty much it.  After it finished doing the update and performing a reboot, pi-timolo was able run all night without crashing.  Hooray!

However, the birdseed holder and Pi container are other issues.  I've got another sandwich box that I plan on using with some scrap wood.  I think the USB battery I'm using is also shot.  So, I bought another one.  I'll put everything together tomorrow and see if we can finally get this thing working...

Saturday, October 15, 2016

PI Critter Cam Day 13 (Part 2): Setting the Trap

Now that we've got the camera looking for animals at startup and have all the other physical components, we are ready to get some animal pictures.

First, I need a way to put birdseed next to the camera.  I had some extra Y Guides shark tooth sifters laying around, which will make a great plain for holding birdseed.  I'll attach my PI enclosure to that with some wood screws.  I'm also going to cover them with tack in an attempt to keep water from entering the enclosure through the screw holes.


Now, we'll put the PI in the enclosure and here's the complete picture.


All we need now is some birdseed!

But wait... the sifter was built with chicken wire and birdseed will fall through that.  So, I'll put some plexiglassish material from a broken, cheap picture frame between the chicken wire and sifter frame.  

I've plugged in the battery,  added the birdseed and clamped it to my deck.  All we need now is some critters.


I'm not exactly sure how long the freebie battery will power the PI.  I'll check the PI at the end of the day to see if it took any pictures and, if I can figure it out, how long it ran before it died.  



PI Critter Cam Day 13: Pics on Startup

Today, we're going to get our PI set up to start looking for critters and take pictures at startup.  So, when I plug it into the battery, the PI will boot and be ready for action.  

We're using the pi-timolo package now to watch for and take pictures of these critters.  Included in the pi-timolo package is a bash script (program that runs in the Raspian terminal) that you can set to run on startup.  This is according to the pi-timolo's wiki.  To run this guy on startup, you stick a path to the program in the file rc.local

To alter rc.local, use the Terminal and type in sudo nano /etc/rc.local

This brings up the terminal's file updating/ word processing program.

Scroll down to the line right above exit 0 and stick in the filepath and file name.

/home/pi/pi-timolo/pi-timolo.sh



After that, save the file with a ctrl + x, confirm you want to use the same file name with an Enter and you are done.  

Now, I'm going to put a big pile of birdseed in front of my pi, plug it in and see what I get at the end of the day.  

Monday, October 10, 2016

PI Critter Cam Day 12: Enclosure

I've been slowly and methodically planning the enclosure for the critter cam.  I want something that can live outside, can hold a battery, and,obviously, the Pi's enclosure, but with the wifi dongle plugged in.  My ultimate goal is to have the Pi take pictures and post them automatically to Twitter.

Since I'm attempting to do this on the cheap, my enclosure plans started with a leftover container about the size of a few servings of mac and cheese, which should be the equivalent of all the components.  It's got latches on the side, which I'm hoping will keep it enclosed enough to keep the rain out.  


Due to my frugality, I'm attempting to use a freebie battery to power the Pi before investing in something more substantial.  This battery is small, but is intended for cell phones, which means you can plug the Pi into it via the mini USB port.  That really simplifies things significantly.  I tested it out and the Pi was able to boot  and take a picture with it as a power source.  Awesome!

Lastly, it has a tiny vent hole on the top that's about the same size as the camera lens.  To utilize this hole, but prevent rain from getting in, I bought some tack stuff via Amazon.  Here's what the completed enclosure looks like.


In order to fully utilize this case, however, I still need to figure out how to have the motion detector initialize on startup and figure out how to test this thing outside.  I'm thinking a tray with some birdseed might be a good test.  Until next time...

Tuesday, August 30, 2016

PI Critter Cam Day 11: More Motion Detection

Ok, it's been 4 weeks.  So, the software should now be installed.

We'll test it out using the Github instructions.

To do this, we simply enter these two lines into the terminal one at a time:

cd ~/pi-timolo 
./pi-timolo.py

And our screen shows the process checking for motion at approximately one second interval.  That must be these "failed to set port info" lines.

If we wave our hand frantically in front of the camera, we see that the message changes (Found Motion).  It then tells us it captured a picture and added white text.


If I check out the /home/pi/pi-timolo/ folder, I see there are pictures of my waving hand!   So, I think we have a successful test.

Now, what do I do with this?  Well, I need an enclosure and I need to figure out how to make the PI run this program at startup.  I should probably also figure out how to make it stop also if I need to.  Maybe a button or a switch to kill the python switch?

Our initial box will just be an indoors one for further testing.  I think I've got some tupperware that I can use...

Tuesday, August 2, 2016

PI Critter Cam Day 10: Motion Detection

After a bit of a hiatus, I am back.

I think I'm going to attempt to use pi-timolo after reading this guy's blog, which appears to be very similar to what I want to do.  He's capturing hedgehogs and I'm capturing whatever comes along.  Other than that, our goals are very similar.

So, tonight, we're going to try and install the software.

Using his GitHub instructions, I installed the software, but this must've kicked off a system wide update as it's updating a ton of stuff.  I'm going to leave it running overnight and see what we end up with tomorrow...


Sunday, June 12, 2016

PI Critter Cam Day 9: Turn the Delete Files Process into a Function

While I'm at it and before I move on to figuring the motion sensor part of our project, I'm going to really quickly turn the portion of the code that checks for and deleting old files into a function with the python code.  This way, I can keep it separate from the rest of the process and call it wherever I want.

It's pretty easy to do this.  You can just define it like so.
def delete_old_files():
    #create our expiration date
    current_date = datetime.datetime.today()
    #The variable representing the duration we want to subtract to create our expiration date.
    y = datetime.timedelta(weeks = 2)
    #And create our expiration date variable.
    exp_date = current_date - y
    #Create a list of picture files in our critter folder and delete the pics that are older than our expiration date.
    for x in os.listdir(critter_path):
        
        if datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)) < exp_date:
            os.remove(critter_path + x)
            print('removing ' + critter_path + x) 
        else: print('not old enough to delete: (' + critter_path + x + ')')

and when you want to use it, you just put in the following:  delete_old_files()

I'm not really sure where I want to do this check.  Having it as a function allows me to put off the decision.

Thursday, June 9, 2016

PI Critter Cam Day 8: Looping through our pictures and deleting old ones

Our slow progression continues. We're still building a critter cam with our raspberry pi that'll take pictures of critters in my backyard when detected.

Today:  going through all the pictures already taken and deleting ones that have reached a certain age.  We're doing this to avoid the tiny hard drive on the Pi from getting too full.

In Python, there's an os package that allows you to create a list of files and folders.  If we create a list of files in our critter cam folder, we can iterate through it, checking the age of the pictures and deleting the ones that are too old.

First, we create a list of files in our critter folder using the listdir function.

critter_path = '/home/pi/Pictures/critter_cam/'
os.listdir(critter_path)

Next, we go through the list and check out how old the file is (building on what we learned last time by using the datetime module):

import datetime

for x in os.listdir(critter_path):
print(datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)))

Building on that, if the file is older than two weeks, delete it.

for x in os.listdir(critter_path):
if datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)) < exp_date:
os.remove(critter_path + x)
print('removing ' + x)

Now let's put it all together.

#import our needed modules
import os.path, datetime

#folder where we keep our critter pics
critter_path = '/home/pi/Pictures/critter_cam/'

#create our expiration date
current_date = datetime.datetime.today()
#The variable representing the duration we want to subtract to create our expiration date.
y = datetime.timedelta(weeks = 2)
#And create our expiration date variable.
exp_date = current_date - y

#Create a list of picture files in our critter folder and delete the pics that are older than our expiration date.
for x in os.listdir(critter_path):
if datetime.datetime.fromtimestamp(os.path.getmtime(critter_path + x)) < exp_date:
os.remove(critter_path + x)
print('removing ' + x)

Awesome!  Next up, we'll start working on getting the Pi to take a picture automatically when it sees an animal through the camera.

Friday, June 3, 2016

PI Critter Cam Day 7: Is the File Old Enough to Delete?

As promised, I'm working on figuring out whether a critter_cam pic is old enough to be deleted.

To figure this out, we need to figure out our expiration date.  We did that last time.

To get the age of a file, you've got to use the os.path module, which comes with the PI version of Python.

That module comes with a function that pulls the creation date and the modification date for you.

os.path.getmtime('/home/pi/Pictures/critter_cam/2016_05_23_19_41_16.jpg'))

HOWEVER, it'll return it in a long decimal value representing the date and time.  NOT HELPFUL.

The datetime module, however, can handle this for you.  It has a fromtimestamp function that'll turn it into a legit datetime format you can compare your expiration date to the file date.

All together now:

#import our needed modules.
import datetime, os.path

#create our expiration date
current_date = datetime.datetime.today()
#The variable representing the duration we want to subtract to create our expiration date.
y = datetime.timedelta(weeks = 2)
#And create our expiration date variable.
exp_date = current_date - y

#get the modification date for the picture file and convert it to a legit datetime value.
file_date = datetime.datetime.fromtimestamp(os.path.getmtime('/home/pi/Pictures/critter_cam/2016_05_23_19_41_16.jpg'))

#And see if the file_date is old enough to be deleted
#If it returns TRUE, we should delete it.  
exp_date > file_date

Next time, we'll put in a loop to check all the files in our critter cam folder and delete the ones that meet this criteria.

Monday, May 30, 2016

PI Critter Cam Day 6: Calculating Date Values

My goal for this go round is to figure out the age of a Raspberry PI file through Python and figure out if it's old enough to delete based on the current date and whatever parameter defines a file as being too old.

To do this, we're going to need the Python TIME module again.

I'm thinking that I can use one of the time functions to subtract my age parameter from the current date and create a variable consisting of my "expiration" date.  Any critter pic with a creation date older than this date should be deleted.

I actually need the datetime module to perform the math with the dates.  It doesn't appear that the TIME module allows for math with time components?

So, I have to collect the current date as a variable:
current_date = datetime.date.today()

And a variable (or, specifically a duration to the datetime module) representing the amount of time to take away to create an expiration date:
y = datetime.timedelta(weeks = 2)

The datetime module allows you to subtract a duration object from a date object.  The expiration date will be as follows:
exp_date = current_date - y

As usual, I didn't get as far as I had hoped.  Next time, we'll work on getting the file dates from the critter pics and doing the compare.





Monday, May 23, 2016

PI Critter Cam Day 5: Picture File Names with the Date and Time

Today, my goal is expand on the picture taking, have it take a picture with a date and time stamp file name and also delete pictures that have reached a certain age.  I'm hoping there will be lots of critters to take pictures of, but my micro SD card is only 8gb.

Since I can't store my pics on the PI long term, my ultimate goal is to have the pictures automatically post to a Twitter handle where they can be stored and archived (and shared).  That'll be further down the road.

So:

  • Capture a picture with a date and time stamp to make it unique and easier to manage.
  • Delete pictures that are older than 2 weeks.
We'll just do both in the same process for now.  It might not be ideal, but I think it'll be alright.  Trying to keep it simple.

We'll use the Python Time package for collecting the time.  

It's actually pretty straightforward manipulating the time into a string for a file name using the strftime function:

time.strftime('%Y_%m_%d_%H_%M_%S')

I can just stick it in like so.  I'm also going to add a folder so everything won't write to a root directory:


#Needed packages...
import picamera
import time

#create object and initialize camera.
cam = picamera.PiCamera()

#take picture!
cam.capture('/home/pi/Pictures/critter_cam/' + time.strftime('%Y_%m_%d_%H_%M_%S') + '.jpg')

Note:  you have to create the actual folder via the File Manager.  Python will not create it automatically by referencing it in the file name.

And it seems to work.  Here's the pic in the folder with the date and time stamp.


We ran out of time regarding deleting old files.  That'll have to wait until next time.

Thursday, May 19, 2016

PI Critter Cam Day 4: Camera Introduction and Keyboard Layout

Today, we make the camera work!

So, we've got it connected and we've got the picamera Python module.

I create a file with the Text Editor and put in the following.

#Needed packages...
import picamera
#create object and initialize camera.
cam = picamera.PiCamera()
#take a pic! 
 cam.capture("test.jpg")
Save it to the desktop as 'test.py.'  You can then double click the file on your desktop to run it.

(Yet another) Interesting tidbit about the PI.  It defaults the keyboard to use the British layout.  This'll cause [SHIFT] + 3 to enter a BRITISH pound, not the number pound (#).

To fix this, you can go to the Menu --> Preferences --> Keyboard and Mouse. Select the Keyboard tab, then press the Keyboard Layout button.

From that window, select the first US layout.  This is what I chose:
















Hopefully, this will be the layout I'm used to.

So, I didn't get a lot worked out after having to figure out the keyboard layout problem.  Tomorrow, more Python.

Wednesday, May 18, 2016

PI Critter Cam Day 3: Correcting IP and Screenshots.

We left off from last time trying to get the PI to connect to the internet.

I've forced my router to assign the PI to a static IP address.  I can see the PI now, but it's still not connecting to the internet.

Ah, I have to put the static IP address for the PI here, not the router's address:

And we have the internet!  In fact, I'm writing this on the PI. :)

Obviously, I also figured out screenshots.  You use Scrot, which comes out of the box with Raspian.  It takes some getting used to, but has lots of features, which you can see with it's help (scrot -h).

Here's what I used to get the active window with a 10 second delay (scrot -d10 -u).  



Thanks to this guy for getting me started with the screenshot.

So, now that we're off that tangent, we can start with the camera again.  Next, we'll work on seeing if we can get the camera to respond to commands through Python...

----------   UPDATE   --------------

I was still having trouble, until I saw the PI had no default gateway.  I was wondering where that was supposed to go!

Anyway, I found via stackexchange how to add it.  Here's a screenshot:







Tuesday, May 17, 2016

PI Critter Cam Day 2 Internet Issues...

Just when I thought I was rolling along, I run into a brick wall.

My pi can't connect to the internet anymore.

I tried resetting the pi, the router, but nothing seems to work.

I can type ifconfig into the terminal to get the IP address and can see it connected to the router.

When I hover over the wireless icon in the top right corner, it says "Configured."

I can even ping my router and other computers on my network, but not anything outside.  The screen states "Destination Host Unreachable."

I'll have to regroup and try and figure this out next time...

Sunday, May 15, 2016

Start of a New Project: PI Critter Cam

So, I'm starting a new project that I will inevitably not finish.  This one involves the use of a Raspberry PI.  My primary goal is to learn more about electronics and programming and how they interact.  My secondary goal is to build an outdoor camera that will take pictures of critters, particularly critters that are in the woods behind the house that my dog has taken a keen interest in.

So far, all of the components for the beginnings of the project have been ordered (I think) and I've got the Raspberry PI working.  In fact, I'm writing this on it!  I've got a long way to go, though.  A lot this is pretty new to me.

Will I get to the end of this project?  We'll see.  History is certainly not on my side, but I'll hopefully have fun along the way, regardless of where I end up.

The first step (after getting the PI to work as a computer):  getting the camera connected.  Here's it hooked up.


and I installed the Python module through the terminal.

I typed in:  sudo apt-get install python3-picamera.  I'd show a screenshot, but it looks like I have to install a PI program to do that.  I'll need to do a little research there.

Anyway, my next step will be to test it out, see if I can get Python to take a picture...