【发布时间】:2015-08-31 23:05:38
【问题描述】:
我使用 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 ,我不确定,但它似乎无法打开文件对象。
【问题讨论】:
-
您是否有理由不只使用
io.BytesIO或cStringIO? -
可能是因为我首先遇到了memoryFS。你能给我一个例子来说明如何实现这个以将视频保存到缓冲区然后将缓冲区写入文件。
-
Em。也许,您不清楚 mem.createfile(...) 是否返回 None。总是。不是文件对象。
-
@shhdup 再次查看后我想到了这一点。然后我做了
f = mem.open('output.avi'),但这是一个 _io.TextWrapperIO 类型,而不是字符串或 unicode 类型。 -
是的,它是类似文件的对象。您可以使用
f.read()阅读它
标签: python memory filesystems