jeffpkamp
jeffpkamp

Reputation: 2866

making a memory only fileobject in python with pyfilesystem

I have written a motion detection/ video program using opencv2 which saves video output for x seconds. if motion in detected during that time, the output is saved as an alternate named file, but if not motion is detected then the file is overwritten. To avoid needless wear on a flash based memory system, I want to write the file to the RAM, and if motion is detected then save it to the non-volatile memory.

I am trying to create this file in the RAM using pyfilesystem-fs.memoryfs

import numpy as np
import cv2, time, os, threading, thread
from Tkinter import *
from fs.memoryfs import MemoryFS

class stuff:

    mem=MemoryFS()
    output = mem.createfile('output.avi')
    rectime=0
    delay=0
    kill=0
    cap = cv2.VideoCapture(0)
    #out = cv2.VideoWriter('C:\motion\\output.avi',cv2.cv.CV_FOURCC('F','M','P','4'), 30, (640,480),True)
    out = cv2.VideoWriter(output, cv2.cv.CV_FOURCC('F','M','P','4'), 30, (640,480),True)

This is the motion detection part

if value > 100:
        print "saving"
        movement=time.time()
        while time.time()<int(movement)+stuff.rectime:
            stuff.out.write(frame)
            ret, frame = stuff.cap.read()


        if stuff.out.isOpened() is True:
            stuff.out.release()
        os.rename(stuff.output, 'c:\motion\\' + time.strftime('%m-%d-%y_%H-%M-%S') + '.avi')

the os.rename function returns TypeError must be string, not None

I'm clearly using memoryfs incorrectly, but cannot find any examples of its use.

EDIT I use the following line to open the file object and write to it

stuff.out.open(stuff.output, cv2.cv.CV_FOURCC(*'FMP4'),24,(640,480),True)

however this returns False , I'm not sure but it appears it can't open the file object.

Upvotes: 1

Views: 4878

Answers (1)

mingaleg
mingaleg

Reputation: 2087

To move your file form MemoryFS to real file system, you should read orig file and write it to dest file, something like

with mem.open('output.avi', 'b') as orig:
    with open('c:\\motion\\' + time.strftime('%m-%d-%y_%H-%M-%S') + '.avi')) as dest:
        dest.write(orig.read())

Upvotes: 1

Related Questions