1

ファイルから一連の画像を実行する matplotlib アニメーションを作成しています。私が視覚化しているファイルは通常非常に大きく、画像のスタックごとにかなりの読み込み時間 (~5 秒) があります。マルチプロセッシングで読み込みプロセスをずらしてアニメーションをスムーズに実行することはできましたが、アニメーションをビデオ ファイルとして保存するのに問題があります。

コードは次のとおりです。

from matplotlib import animation
import pylab as plt
import numpy as np
import multiprocessing as mp
import logging
logger = mp.log_to_stderr(logging.INFO)
import time

def qloader(queue, threshold=100, nfiles=3):
    '''trigger a load process if number of items in queue drops below threshold'''
    while nfiles:
        if queue.qsize() < threshold:
            logger.info( 'qsize {}'.format(queue.qsize()) )

            time.sleep( 1 )     #pretend to load data
            data = np.random.rand(25,100,100)

            logger.info( 'Adding data to queue' )
            for d in data:
                queue.put(d)
            logger.info( 'Done adding data!' )
            nfiles -= 1
    else:
        queue.put( None )        #sentinal

def update(frame, im, queue):
    '''update the image'''
    logger.info( 'Updating frame %d'%frame )
    data = queue.get()
    if data is None:
        print( 'Queue is empty!' )
        return

    im.set_data( data )
    return im


#create data queue
mgr = mp.Manager()
queue = mgr.Queue()
threshold = 20          #

#start load process
p = mp.Process( name='loader', target=qloader, args=(queue, threshold) )
p.start()

#start animation
fig, ax = plt.subplots()
im = ax.imshow( np.random.rand(100,100) )
ani = animation.FuncAnimation( fig, update, frames=75, interval=100, repeat=0, fargs=(im, queue) )
ani.save('foo.mp4', 'ffmpeg')

コードはエラーなしで実行されますが、生成されるファイルは何らかの理由で破損しています。vlc で表示しようとすると、長い繰り返しのエラー ストリームが表示されます...

$ vlc foo.mp4 
VLC media player 2.0.8 Twoflower (revision 2.0.8a-0-g68cf50b)
[0xf69108] main libvlc: Running vlc with the default interface. Use 'cvlc' to use vlc without interface.
[0x7f37fcc01ac8] mp4 demux error: cannot find any /moov/trak
[0x7f37fcc01ac8] es demux error: cannot peek
...
[0x7f37fcc01ac8] ps demux error: cannot peek
[0x7f37fcc01ac8] mpgv demux error: cannot peek
[0x7f37fcc01ac8] mjpeg demux error: cannot peek
[0x7f37fcc01ac8] ps demux error: cannot peek
[0x7f3824000b78] main input error: no suitable demux module for `file/://.../foo.mp4'
...

さまざまなライターとエンコーダーを使用して、さまざまなファイル形式で保存しようとしましたが、ほとんど同じ結果が得られました。

multiprocessingこの問題は、 を使用してデータをロードする場合にのみ発生します。でデータを作成するだけdata = np.random.rand(75,100,100)で、アニメーションは問題なく保存されます。

質問:matplotlib.animationと一緒にプレイするに はどうすればよいmultiprocessingですか?

4

1 に答える 1

1

デフォルトでanimation.MovieWriterは、 a を使用しsubprocess.PIPEてフレームをライターに送ります。multiprocessingこれは、使用時になぜか機能しないようです。最後の行を に変更する ani.save('foo.mp4', 'ffmpeg_file') と、ムービーを作成する前にフレームを一時的にディスクに保存するようライターに指示し、問題を回避します。

于 2014-10-30T20:50:31.200 に答える