用pyfilesystem在python中创建一个只有内存的文件对象 [英] making a memory only fileobject in python with pyfilesystem

查看:1518
本文介绍了用pyfilesystem在python中创建一个只有内存的文件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用opencv2编写了一个运动检测/视频程序,它将视频输出保存x秒。如果在此期间检测到运动,则输出将保存为备用命名文件,但如果未检测到运动,则会覆盖该文件。为了避免在基于闪存的存储器系统上不必要的磨损,我想将文件写入RAM,如果检测到运动,则将其保存到非易失性存储器中。



我试图在内存中使用pyfilesystem-fs.memoryfs创建这个文件
$ b $ pre $ import $ numpy as $ p $ b $ import cv2,time,os,线程,线程
从Tkinter导入*
从fs.memoryfs导入MemoryFS

类的东西:

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

这是动作侦测部分

  if value> 100:
printsaving
movement = time.time()
while time.time()< int(移动)+ stuff.rectime:
stuff.out。写(框架)
ret,frame = stuff.cap.read()


如果stuff.out.isOpened()为真:
stuff.out。 release()
os.rename(stuff.output,'c:\motion\\'+ time.strftime('%m-%d-%y_%H-%M-%S') +'.avi')

os.rename函数返回 TypeError必须是字符串,而不是无



我明显使用memoryfs不正确,但找不到任何使用的例子。

编辑
我使用下面这行打开文件对象并写入它

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

但是,这返回 False ,我不确定,但它似乎无法打开文件为了将你的文件从MemoryFS移动到真正的文件系统,你应该读取原始文件并将其写入到dest文件中,像

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


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.

解决方案

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

这篇关于用pyfilesystem在python中创建一个只有内存的文件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆