我使用opencv2编写了一个运动检测/视频程序,可以将视频输出保存x秒 . 如果在此期间检测到运动,则输出将保存为备用命名文件,但如果未检测到运动,则会覆盖该文件 . 为了避免基于闪存的内存系统出现不必要的磨损,我想将文件写入RAM,如果检测到运动,则将其保存到非易失性存储器中 .
我试图使用pyfilesystem-fs.memoryfs在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
我显然错误地使用memoryfs,但找不到它的任何使用示例 .
编辑我使用以下行打开文件对象并写入它
stuff.out.open(stuff.output, cv2.cv.CV_FOURCC(*'FMP4'),24,(640,480),True)
但是这会返回 False
,我'm not sure but it appears it can' t打开文件对象 .
1 回答
要将文件从MemoryFS移动到真实文件系统,您应该读取orig文件并将其写入dest文件,例如