我已经使用opencv2编写了一个运动检测/视频程序,它将视频输出保存为x秒。如果在此期间检测到运动,则将输出保存为备用命名文件,但如果未检测到运动,则覆盖该文件。为了避免在基于闪存的存储系统上不必要的磨损,我想将文件写入RAM,如果检测到运动,则将其保存到非易失性存储器。
我正在尝试使用pyfilesystem-fs.ememyfs在RAM中创建此文件
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)这是运动检测部分
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')os.rename函数返回TypeError must be string, not None
我显然没有正确地使用内存文件系统,但是我找不到任何使用它的例子。
编辑我使用以下行打开文件对象并对其进行写操作
stuff.out.open(stuff.output, cv2.cv.CV_FOURCC(*'FMP4'),24,(640,480),True)但是,这将返回False,我不确定,但它似乎无法打开文件对象。
发布于 2015-09-01 07:32:28
要将你的文件从MemoryFS转移到真正的文件系统,你应该读取原始文件并将其写入到目标文件,类似于
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())https://stackoverflow.com/questions/32321216
复制相似问题