def __init__(self, threshold=20, doRecord=True, showWindows=True):
        self.writer = None
        self.font = None
        self.doRecord = doRecord  # Either or not record the moving object
        self.show = showWindows  # Either or not show the 2 windows
        self.frame = None

        self.MotionCollection = mongoLABhelper.startMotionDatabaseURI("MotionCollection", "uri")
        self.AlertCollection = mongoLABhelper.startAlertDatabaseURI("AlertCollection", "uri")

        self.capture = cv.CaptureFromCAM(0)
        self.frame = cv.QueryFrame(self.capture)  # Take a frame to init recorder

        self.frame1gray = cv.CreateMat(self.frame.height, self.frame.width, cv.CV_8U)  # Gray frame at t-1
        cv.CvtColor(self.frame, self.frame1gray, cv.CV_RGB2GRAY)

        # Will hold the thresholded result
        self.res = cv.CreateMat(self.frame.height, self.frame.width, cv.CV_8U)

        self.frame2gray = cv.CreateMat(self.frame.height, self.frame.width, cv.CV_8U)  # Gray frame at t

        self.width = self.frame.width
        self.height = self.frame.height
        self.nb_pixels = self.width * self.height
        self.threshold = threshold
        self.isRecording = False
        self.trigger_time = 0  # Hold timestamp of the last detection

        if showWindows:
            cv.NamedWindow("Image")
            cv.CreateTrackbar("Detection threshold: ", "Image", self.threshold, 100, self.onChange)
#!/usr/bin/python
import rospy
import cv2
import mongoLABhelper
import pymongo
import datetime

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
AlertCollection = mongoLABhelper.startAlertDatabaseURI("AlertCollection", "URI")
MotionCollection = mongoLABhelper.startMotionDatabaseURI("MotionCollection", "URI")
print AlertCollection
date = datetime.datetime.utcnow()
print date

def AlertSaveImage(AlertCollection, MotionCollection, date, frame):
	if mongoLABhelper.alertQuery(AlertCollection, date) == True:
		ImageName = "AlertMotionDetected"+datetime.datetime.now().strftime("%b %d, %H:%M:%S")+".jpg"
		cv2.imwrite(ImageName, frame)
		motion_id = mongoLABhelper.addMotionData(MotionCollection, ImageName, ImageName)
		print "Saved Image from second angle"

	return datetime.datetime.utcnow()

if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False


while rval: