覆盖缓冲区仍会导致内存崩溃

时间:2016-01-04 15:34:02

标签: multithreading python-2.7 opencv

我正在尝试将AVI视频转换为一系列图像,只将最近8秒的视频保存到“缓冲区”中。我只保存两位信息:从AVI中提取图像的时间戳,以及图像本身。

我的问题是,即使为缓冲区写入程序以覆盖自身,它仍然会发生内存崩溃 - 大约2分钟,对于8s缓冲区。 (错误:( - 4)无法在函数cv :: OutofMemoryError中分配######字节。此线程的代码位于上下文的旁边。

(上下文:我正在尝试为帧抓取器创建一个仿真器。它不断/无休止地转换保存的AVI,并在到达视频结束时重新启动。缓冲区应该类似于任何硬件 - 实际上是一个HDD或SSD或其他任何 - 帧抓取器实际保存到的。这个模拟器作为自己的线程运行,与另一个线程并行处理并按照请求发送图像。崩溃发生时没有调用第二个线程。)

import base64, os, cv2, re, time, math
from PIL import Image
from threading import Thread

lobal numbertosave, framearray, sessionlist
framearray = []
vidfile = os.path.normpath("videolocation")
savepath = os.path.normpath("whereIsaverequestedimages"

class FrameInfo:
    def __init__(self, frame, time):
        self.f = frame
        self.t = time

def VidEmulator():
    global numbertosave, framearray, hit, fps
    video = cv2.VideoCapture(vidfile)
    fps = video.get(5)
    ##### Change the integer in the expression below to the number of seconds you want ot save
    numbertosave = int(fps*5)
    ######## numbertosave = framerate * (# of seconds to store in buffer)
    totalframes = video.get(7)
    atframe = 1
    count = 1
    framearray = [0] * numbertosave
    deltat = []
    converting = True
    while converting == True:
        try:
            ret, frame = video.read()
            timestamp = time.time()
            x = FrameInfo(frame, timestamp)
            framearray[count] = ((x.f, x.t))
            atframe = atframe + 1
            count = count + 1
            if count == numbertosave:
                count = 1
                VidEmulator()
        except KeyboardInterrupt:
            #### edited to remove mention of other thread, which isn't
            #### invoked when testing this one (it is on request; see
            #### comments) I honestly haven't tested this block since I
            #### usually ctrl+printbreak
            t1.join()
            print "threads killed"
            exit()
            print "hopefully you don't see this"

if __name__ == '__main__':
    t1 = Thread(target = VidEmulator)
    t1.start()

我猜我可能有一个非常简单的逻辑问题,我在这里阻止缓冲区实际覆盖自身,或者其他一些变量是“挂起”,累积连续运行并使程序崩溃。否则,有没有更好的方法来处理这个问题,这会逃避这个错误?

谢谢!

0 个答案:

没有答案