<commit_before>""" Implements functions that save video buffer stored in memory to a file.
    Will probably use OpenCV funcitons/types to accomtypes to accomplish this.
"""
import cv2
from Queue import Queue
from datetime import datetime, timedelta
import motion


class Frame:
    def __init__(self, timestamp, image, has_motion):
        self.timestamp = timestamp
        self.image = image
        self.has_motion = has_motion


def capture_from(camera):
    success, img = camera.read()
    return Frame(datetime.now(), img, motion.CV_detects_motion(img))


def record_on_motion(filename, fourcc=cv2.cv.CV_FOURCC(*"XVID"), fps=30, size=(640,480), color=True):
    vidbuf = Queue()
    camera = cv2.VideoCapture()
    bufsize = 60
    camera.open(0)
    output = cv2.VideoWriter(filename, fourcc, fps, size, color)
    recording = False

    for i in range(0, bufsize):
        vidbuf.put(capture_from(camera))

    while True:
        frame = capture_from(camera)
        if frame.has_motion:
            recording = True
            while 0 < vidbuf.qsize():
                output.write(vidbuf.get().image)
            output.write(frame.image)
        else: #if not frame.has_motion:
            vidbuf.put(capture_from(camera))
            if bufsize < vidbuf.qsize():
                vidbuf.get(True)
                if recording == True:
                    while 0 < vidbuf.qsize():
                        output.write(vidbuf.get().image)
                    recording = False
                    break


if __name__ == "__main__":
    record_on_motion(str(datetime.now()) + ".avi")
<commit_msg>Changed recording function so that files can be saved in a separate directory, and are named after the time that the recording-triggering motion occured.<commit_after>""" Implements functions that save video buffer stored in memory to a file.
    Will probably use OpenCV funcitons/types to accomtypes to accomplish this.
"""
import cv2
from Queue import Queue
from datetime import datetime
import motion


class Frame:
    def __init__(self, timestamp, image, has_motion):
        self.timestamp = timestamp
        self.image = image
        self.has_motion = has_motion


def capture_from(camera):
    success, img = camera.read()
    return Frame(datetime.now(), img, motion.CV_detects_motion(img))


def record_on_motion(savedir='.', fourcc=cv2.cv.CV_FOURCC(*"XVID"), fps=30, size=(640,480), color=True):
    vidbuf = Queue()
    camera = cv2.VideoCapture()
    output = cv2.VideoWriter()
    bufsize = 60
    camera.open(0)
    recording = False

    for i in range(0, bufsize):
        vidbuf.put(capture_from(camera))

    while True:
        frame = capture_from(camera)
        if frame.has_motion:
            recording = True
            if not output.isOpened():
                filename = savedir + '/' + str(datetime.now()) + ".avi"
                output.open(filename, fourcc, fps, size, color)
            while 0 < vidbuf.qsize():
                output.write(vidbuf.get().image)
            output.write(frame.image)
        else: #if not frame.has_motion:
            vidbuf.put(capture_from(camera))
            if bufsize < vidbuf.qsize():
                vidbuf.get(True)
                if recording == True:
                    while 0 < vidbuf.qsize():
                        output.write(vidbuf.get().image)
                    recording = False
                    break


if __name__ == "__main__":
    record_on_motion(str(datetime.now()) + ".avi")

{'fraction_non_alphanumeric': 0.10315186246418338, 'fraction_numerical': 0.010315186246418338, 'mean_word_length': 2.312144212523719, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6223323', 'n_tokens_mistral': 1047, 'n_tokens_neox': 991, 'n_words': 274}