python创建文件对象,用pyfilesystem在python中创建一个只有内存的文件对象

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()

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.

解决方案

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())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值