Skip to content

Instantly share code, notes, and snippets.

@smittytone
Created January 23, 2020 22:04
Show Gist options
  • Save smittytone/434cbcb3fe27b694c6e84b4158495fb5 to your computer and use it in GitHub Desktop.
Save smittytone/434cbcb3fe27b694c6e84b4158495fb5 to your computer and use it in GitHub Desktop.
Simple Raspberry Pi-based CCTV app. Requires Pi camera, Dropbox account
#!/usr/bin/python
from picamera import PiCamera
from time import sleep
from subprocess import call
import os
import sys
# CONSTANTS
KEEP_FILES = 10
WARM_TIME = 3
SOURCE_BASE = "~/stills/"
SCRIPT_BASE = "~/dropbox-uploader/dropbox_uploader.sh -q "
# NOTE dropbox_uploader.sh available from https://github.com/andreafabrizi/Dropbox-Uploader
# GLOBAL VARIABLES
cctv = PiCamera()
imageCount = 1
# FUNCTIONS
def takepic(destinationPath):
global WARM_TIME
cctv.start_preview()
sleep(WARM_TIME)
cctv.capture(destinationPath)
cctv.stop_preview()
# PROGRAM START
print('Starting PiCamera...')
cctv.resolution = (1024, 768)
cctv.led = False
# Picture right way up when Pi in landscape, power cable below
cctv.rotation = 90
# TODO read in current image count from /still
while True:
# Get the name of the next photo to take and
# its save path in the filesystem
imageName = "img{:04d}.jpg".format(imageCount)
piPath = SOURCE_BASE + imageName
# Take the image and save at the specified path
takepic(piPath)
# Check if we have more than 'KEEP_FILES' photos
if imageCount > KEEP_FILES:
# Delete the oldest image
fileName = "img{:04d}.jpg".format(imageCount - KEEP_FILES)
filePath = SOURCE_BASE + fileName
if os.path.exists(filePath):
# There is an image to delete, so delete it from the filesystem...
print('Deleting: ' + filePath)
os.remove(filePath)
# ...and from Dropbox
shellCommand = SCRIPT_BASE + "delete " + fileName
call([shellCommand], shell=True)
else:
print("[ERROR] Cannot find " + fileName + " for deletion")
# Upload the latest photo
shellCommand = SCRIPT_BASE + "upload " + piPath + " " + imageName
print("Uploading: " + piPath)
call([shellCommand], shell=True)
# Increment the count and cycle at 10,000 images
imageCount = imageCount + 1
if imageCount == 9999:
imageCount = 1
# Sleep between images
sleep(60 - WARM_TIME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment