gstlal  1.4.1
matplotlibhelper.py
Go to the documentation of this file.
1 # Copyright (C) 2010 Leo Singer
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 2 of the License, or (at your
6 # option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 # Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17 
18 
19 
20 """
21 Classes and functions for building Matplotlib-based GStreamer elements
22 """
23 __author__ = "Leo Singer <leo.singer@ligo.org>"
24 __all__ = ("padtemplate", "figure", "render", "BaseMatplotlibTransform")
25 
26 
27 import gi
28 gi.require_version('Gst', '1.0')
29 from gi.repository import GObject
30 from gi.repository import Gst
31 GObject.threads_init()
32 Gst.init(None)
33 from gstlal.pipeutil import *
34 from gstlal import pipeio
35 
36 
37 """Pad template suitable for producing video frames using Matplotlib.
38 The Agg backend supports rgba, argb, and bgra."""
39 padtemplate = Gst.PadTemplate(
40  "src",
41  Gst.PadDirection.SRC, Gst.PadPresence.ALWAYS,
42  Gst.caps_from_string("""
43  video/x-raw,
44  format = (string) {RGB, ARGB, RGBA, BGRA},
45  width = (int) [1, MAX],
46  height = (int) [1, MAX],
47  framerate = (fraction) [0/1, MAX]
48  """)
49 )
50 
51 
52 def figure():
53  """Create a Matplotlib Figure object suitable for rendering video frames."""
54  import matplotlib
55  matplotlib.rcParams.update({
56  "font.size": 8.0,
57  "axes.titlesize": 10.0,
58  "axes.labelsize": 10.0,
59  "xtick.labelsize": 8.0,
60  "ytick.labelsize": 8.0,
61  "legend.fontsize": 8.0,
62  "figure.dpi": 100,
63  "savefig.dpi": 100,
64  "text.usetex": False,
65  "path.simplify": True
66  })
67  from matplotlib import figure
68  from matplotlib.backends.backend_agg import FigureCanvasAgg
69  figure = figure.Figure()
70  FigureCanvasAgg(figure)
71  return figure
72 
73 
74 def render(fig, buf, (width, height), fmt):
75  """Render a Matplotlib figure to a GStreamer buffer."""
76  fig.set_size_inches(
77  width / float(fig.get_dpi()),
78  height / float(fig.get_dpi())
79  )
80  fig.canvas.draw()
81  if fmt == "RGB":
82  imgdata = fig.canvas.renderer._renderer.tostring_rgb()
83  elif fmt == "ARGB":
84  imgdata = fig.canvas.renderer._renderer.tostring_argb()
85  elif fmt == "RGBA":
86  imgdata = fig.canvas.renderer._renderer.buffer_rgba()
87  elif fmt == "BGRA":
88  imgdata = fig.canvas.renderer._renderer.tostring_bgra()
89  else:
90  raise ValueError('invalid format "%s"' % fmt)
91  datasize = len(imgdata)
92  buf[:datasize] = imgdata
93  buf.datasize = datasize
94 
95 
96 class BaseMatplotlibTransform(Gst.BaseTransform):
97  """Base class for transform elements that use Matplotlib to render video."""
98 
99  __gsttemplates__ = padtemplate
100 
101  def __init__(self):
102  self.figure = figure()
103  self.axes = self.figure.gca()
104 
105  def do_transform_caps(self, direction, caps):
106  """GstBaseTransform->transform_caps virtual method."""
107  if direction == Gst.PadDirection.SRC:
108  return self.get_static_pad("sink").get_fixed_caps_func()
109  elif direction == Gst.PadDirection.SINK:
110  return self.get_static_pad("src").get_fixed_caps_func()
111  raise ValueError(direction)
112 
113  def do_transform_size(self, direction, caps, size, othercaps):
114  """GstBaseTransform->transform_size virtual method."""
115  if direction == Gst.PadDirection.SINK:
116  return pipeio.get_unit_size(self.get_static_pad("src").query_caps(None))
117  raise ValueError(direction)
118 
119 GObject.type_register(BaseMatplotlibTransform) # MOD: Found type_register in line: [gobject.type_register(BaseMatplotlibTransform)]
def do_transform_size(self, direction, caps, size, othercaps)