1

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.

6
  • 1
    Is there a reason you don't just use io.BytesIO or cStringIO? Commented Aug 31, 2015 at 23:08
  • probably because I ran into memoryFS first. Could you give me an example of how to implement this to save the video to the buffer then write the buffer to a file. Commented Aug 31, 2015 at 23:21
  • Em. Maybe, it isn't clear to you that mem.createfile(...) returns None. Always. Not a file object. Commented Aug 31, 2015 at 23:55
  • @shhdup I thought of that after I looked at it again. I then did f = mem.open('output.avi') but this is an _io.TextWrapperIO type, not a string or unicode type. Commented Sep 1, 2015 at 0:53
  • Yes, it's file-like object. You could read from it with f.read() Commented Sep 1, 2015 at 1:12

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

3 Comments

Use shutil.copyfileobj() or else you run out of RAM.
And don't forget to limit chunks size, or you run out of RAM whatever ;)
I think the problem comes before that point. it is calling my output a None object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.