#!/usr/local/bin/python ## pigcam is intended to film our guinea pigs using stop motion ## scripts have been taken from various blogs (listed below) and modified) ## The general process is: ## - use an LDR to only record when it's light ## - record timelapse images to a directory ## - when it is dark (very dark to allow for cloud cover) convert the stillss to a video ## - upload the video somewhere (youtube or flickr) ## still image generation from ## http://designspark.com/blog/time-lapse-photography-with-the-raspberry-pi-camera ## LDR code from ## http://www.element14.com/community/community/raspberrypi_projects/blog/2013/08/22/light-sensor-with-1-gpio import RPi.GPIO as GPIO, time, subprocess, os, sys from datetime import datetime ##Set global Variables dark = True ## stores if it is currently dark wasDark = True ## stores if it was dark on previous check lightLevel = 0 ## stores the light level diskSpaceToReserve = 40 * 1024 * 1024 # Keep 40 mb free on disk SAVEDIR='/var/pigcam/stills' ## Directory to store the stills # Tell the GPIO library to use # Broadcom GPIO references GPIO.setmode(GPIO.BCM) # Define function to measure charge time def RCtime (PiPin): measurement = 0 # return 30 # Discharge capacitor GPIO.setup(PiPin, GPIO.OUT) GPIO.output(PiPin, GPIO.LOW) time.sleep(0.1) GPIO.setup(PiPin, GPIO.IN) # Count loops until voltage across # capacitor reads high on GPIO while (GPIO.input(PiPin) == GPIO.LOW): measurement += 1 return measurement/1000 # Get available disk space def getFreeSpace(): st = os.statvfs(SAVEDIR) du = st.f_bavail * st.f_frsize return du # Generate the video from the stills def generateVideo(): os.system('ls ' + SAVEDIR + '/*.jpg > ' + SAVEDIR + '/stills.txt') os.system('mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o ' + SAVEDIR + '/pigcam.avi -mf type=jpeg:fps=24 mf://@' + SAVEDIR + '/stills.txt') print('Video Generated at /var/pigcam/stills/pigcam.avi') ## Remove th stills os.system("rm " + SAVEDIR + "/*.jpg") return def uploadVideo(): os.system('youtube-upload --email= --password= --title=\"Guinea Pigs Upload' + str(ROLL) + '\" --description="timelapse video of guinea pigs using raspberrypi" --category=Animals --keywords="Guinea Pigs" ' + SAVEDIR + '/pigcam.avi') os.system('rm ' + SAVEDIR + '/*.avi') return ## Open the config file for writing ROLL=open('/var/pigcam/series', 'rb').read() ##handle empty file if ROLL == '': ROLL = 0 ##Set up a pin to act as a switch to end the code GPIO.setup(18, GPIO.IN) ## Main program loop while GPIO.input(18) == GPIO.LOW: lightLevel = RCtime(4) # MEasure timing using GPIO4 print 'light level indicator = ' + str(lightLevel) if lightLevel < 50: dark = False else: dark = True ## If it was dark and is now light then increase the series log and start recording if wasDark == True and dark == False: #increment ROLL and save print('It was dark and is now light\n') print('setting roll value to ' + str(ROLL) + '\n') ROLL = int(ROLL) + 1 f = open('/var/pigcam/series','w') f.write(str(ROLL)) ## If it is not dark record elif dark == False and wasDark == False: print('recording image\n') timenow = datetime.now() filename="%s/%s-%04d%03d%02d_%02d%02d-%02d.jpg" % (SAVEDIR,str(ROLL),timenow.year, timenow.month, timenow.day, timenow.hour,timenow.minute, timenow.second) ## Only record if there is space if getFreeSpace() < diskSpaceToReserve: print('not enough free space') generateVideo() uploadVideo() else: subprocess.call("raspistill -o " + filename, shell=True) elif dark == True and wasDark == False: ## It must have changed from light to dark ## generate a video ## generate files to include print('it was light and is now dark\nGenerating video') generateVideo() #Upload the video to youtube uploadVideo() else: ##it's dark so do nothing print("it's still dark") wasDark = dark; time.sleep(4)