2013-03-15 31 views
5

Tôi gặp sự cố rất giống với this questionLưu hình ảnh động phân tán bằng matplotlib tạo tệp video trống

nhưng giải pháp được đề xuất không có tác dụng đối với tôi.

Tôi đã thiết lập một ô phân tán hoạt hình bằng mô-đun hoạt ảnh matplotlib. Điều này làm việc tốt khi nó được hiển thị trực tiếp. Tôi muốn lưu nó vào một tập tin avi hoặc một cái gì đó tương tự. Mã tôi đã viết để làm điều này không lỗi nhưng video nó tạo ra chỉ hiển thị một tập hợp các trục trống hoặc một màn hình màu đen. Tôi đã thực hiện một số kiểm tra và dữ liệu đang được chạy và con số cập nhật nó chỉ không được lưu vào video ...

Tôi đã thử xóa "animated = True" và "blit = True" như được đề xuất trong this question nhưng điều đó đã làm không khắc phục được sự cố.

Tôi đã đặt mã có liên quan bên dưới nhưng có thể cung cấp thêm nếu cần. Bất cứ ai có thể đề nghị những gì tôi nên làm để có được điều này làm việc?

def initAnimation(self): 
     rs, cfgs = next(self.jumpingDataStreamIterator)  
     #self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], marker='o') 
     self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], marker='o', animated=True) 
     return self.scat, 


def updateAnimation(self, i): 
    """Update the scatter plot.""" 
    rs, cfgs = next(self.jumpingDataStreamIterator) 
    # Set x and y data... 
    self.scat.set_offsets(rs[:2,].transpose()) 
    #self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], animated=True) 
    # Set sizes... 
    #self.scat._sizes = 300 * abs(data[2])**1.5 + 100 
    # Set colors.. 
    #self.scat.set_array(cfgs[0]) 
    # We need to return the updated artist for FuncAnimation to draw.. 
    # Note that it expects a sequence of artists, thus the trailing comma. 
    matplotlib.pyplot.draw() 
    return self.scat, 

def animate2d(self, steps=None, showEvery=50, size = 25): 
    self.figAnimation, self.axAnimation = matplotlib.pyplot.subplots() 
    self.axAnimation.set_aspect("equal") 
    self.axAnimation.axis([-size, size, -size, size]) 
    self.jumpingDataStreamIterator = self.jumpingDataStream(showEvery) 

    self.univeseAnimation = matplotlib.animation.FuncAnimation(self.figAnimation, 
          self.updateAnimation, init_func=self.initAnimation, 
          blit=True) 
    matplotlib.pyplot.show() 

def animate2dVideo(self,fileName=None, steps=10000, showEvery=50, size=25): 
    self.figAnimation, self.axAnimation = matplotlib.pyplot.subplots() 
    self.axAnimation.set_aspect("equal") 
    self.axAnimation.axis([-size, size, -size, size]) 
    self.Writer = matplotlib.animation.writers['ffmpeg'] 
    self.writer = self.Writer(fps=1, metadata=dict(artist='Universe Simulation')) 
    self.jumpingDataStreamIterator = self.jumpingDataStream(showEvery) 

    self.universeAnimation = matplotlib.animation.FuncAnimation(self.figAnimation, 
          self.updateAnimation, scipy.arange(1, 25), init_func=self.initAnimation) 

    self.universeAnimation.save('C:/universeAnimation.mp4', writer = self.writer) 
+0

Bạn có thể giảm số lượng mã xuống tối thiểu mà bạn cần để tái tạo sự cố không? Điều này rõ ràng là một phần của một lớp học lớn hơn, và nó không phải là rõ ràng rằng đây là chức năng như mã độc lập. Hãy giúp chúng tôi giúp bạn. – tacaswell

+0

mã _exact_ trong http://stackoverflow.com/a/14740703/380231 có hoạt động hay không hoạt động nếu bạn chạy nó trên hệ thống của mình? – tacaswell

+1

Xin lỗi vì sự chậm trễ. Tôi đã tìm thấy một công việc xung quanh bằng cách đơn giản là tiết kiệm rất nhiều hình ảnh cá nhân và sau đó gọi ffmpeg để chuỗi chúng lại với nhau. Đây không phải là lý tưởng nhưng hoàn thành công việc. Khi tôi chạy mã ở đây http://stackoverflow.com/a/14740703/380231 Tôi nhận được lỗi sau: .... Tệp "C: \ Python27 \ lib \ site-packages \ matplotlib \ backend_bases .py ", dòng 2093, in print_figure ** kwargs) Tệp" C: \ Python27 \ lib \ site-packages \ matplotlib \ backends \ backend_agg.py ", dòng 483, trong print_raw renderer._renderer.write_rgba (filename_or_obj) RuntimeError: Lỗi ghi vào tệp – user2175850

Trả lời

0

Xin lỗi vì sự chậm trễ. Tôi đã tìm thấy một công việc xung quanh bằng cách đơn giản là tiết kiệm rất nhiều hình ảnh cá nhân và sau đó gọi ffmpeg để chuỗi chúng lại với nhau. Đây không phải là lý tưởng nhưng hoàn thành công việc. (một phần của lớp học lớn hơn)

def easyway(self, frames): 
    ##INSERT CODE TO GET xdata and ydata! 
    for i in range(0, frames): 
     fileName = "videoName"+"%04d.png" % i 
     matplotlib.pyplot.scatter(xdata,ydata,c=coldata, marker='*', 
           s=15.0, edgecolor='none') 
     matplotlib.pyplot.savefig(self.workingDirectory+'temp/'+fileName, dpi=dpi) 
     matplotlib.pyplot.close() 
     ##INSERT CODE TO UPDATE xdata and ydata! 
    self.createVideoFile(fps)#calls FFMpeg to chain together all the pictures 

    if cleanUp:#removes all the picture files created in the process 
     print "temporary picture files being removed..." 
     self.clearDirectory() 
    print "FINISHED" 

def clearDirectory(self,directoryName='temp'): 
    files = glob.glob(self.workingDirectory+directoryName+"/*") 
    for f in files: 
     os.remove(f) 

def createVideoFile(self,fps=3): 
    command="ffmpeg -f image2 -r %s -i %s%s" % (str(fps), self.workingDirectory+'temp/', self.universe.description) 
    command+="%04d.png" 
    command+=" -c:v libx264 -r 30 %s.mp4" % (self.workingDirectory+'videos/'+self.universe.description) 
    print "Running command:" 
    print command 
    p = subprocess.Popen(command, shell=True, stdout = subprocess.PIPE, stderr=subprocess.STDOUT) 
    output = p.communicate()[0] 
    print "output\n"+"*"*10+"\n" 
    print output 
    print "*"*10 
    print "Video file has been written" 
Các vấn đề liên quan