Skip to content

Instantly share code, notes, and snippets.

@kamathln
Last active March 19, 2016 12:55
Show Gist options
  • Save kamathln/afd10c8a2ec729414448 to your computer and use it in GitHub Desktop.
Save kamathln/afd10c8a2ec729414448 to your computer and use it in GitHub Desktop.
Raspberry Pi USB WebCamera Recording for security timelapse *Without much CPU overhead*
# Copyright (c) 2016 Laxmianarayan G Kamath A [email protected]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This program allows you to use the RaspBerry Pi as a security DVR for :
# USB Webcamera and NOT the RaspberryPi Camera
# The most important feature of this program is that it uses very little CPU Overhead
# (unlike my previous attempt which used mencoder)
#
# Usage:
# python seccam.py [filename without extension] [camera number to use] [delay between frames]
#
# defaults:
# Filename = current timestamp as YYYYMMDD_hhmmss.mov
# camera number to use = 0
# delay between frame = 2
import cv2
import time
import sys
import os
argv = sys.argv
delay = int(argv[3]) if len(argv) > 3 else 2
camnum = int(argv[2]) if len(argv) > 2 else 0
vc = cv2.VideoCapture( camnum )
vc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH,640)
vc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT,480)
res, i = vc.read()
height,width,depth = i.shape
outfilename = argv[1] if len(argv) > 1 else time.strftime('%Y%m%d_%H%M%S_%Z')
outfilename += '.mov'
print outfilename
debug = os.environ.keys().count('DEBUG') > 0
vw = cv2.VideoWriter(outfilename, cv2.cv.FOURCC('m','p','4','v'),24,(width,height))
done = False
frctr = 0
while not done:
try:
time.sleep(delay)
res,i = vc.read()
if res:
ctime = time.strftime('%Y/%m/%d %H:%M:%S %Z')
cv2.putText(i,ctime,(30,30), cv2.FONT_HERSHEY_PLAIN, 1.0, (0,0,0))
cv2.putText(i,ctime,(31,31), cv2.FONT_HERSHEY_PLAIN, 1.0, (255,255,255))
vw.write(i)
frctr += 1
if debug:
sys.stderr.write("frame:%d\r" % (frctr))
except KeyboardInterrupt:
done = True
vw.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment