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.