gstlal 1.13.0
Loading...
Searching...
No Matches
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"""
21Classes 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
27import gi
28gi.require_version('Gst', '1.0')
29gi.require_version('GstBase', '1.0')
30from gi.repository import GObject, Gst, GstBase
31GObject.threads_init()
32Gst.init(None)
33
34from gstlal.pipeutil import *
35from gstlal import pipeio
36
37
38"""Pad template suitable for producing video frames using Matplotlib.
39The Agg backend supports rgba, argb, and bgra."""
40padtemplate = Gst.PadTemplate.new(
41 "src",
42 Gst.PadDirection.SRC, Gst.PadPresence.ALWAYS,
43 Gst.caps_from_string("""
44 video/x-raw,
45 format = (string) {RGB, ARGB, RGBA, BGRA},
46 width = (int) [1, MAX],
47 height = (int) [1, MAX],
48 framerate = (fraction) [0/1, MAX]
49 """)
50)
51
52
53def figure():
54 """Create a Matplotlib Figure object suitable for rendering video frames."""
55 import matplotlib
56 matplotlib.rcParams.update({
57 "font.size": 8.0,
58 "axes.titlesize": 10.0,
59 "axes.labelsize": 10.0,
60 "xtick.labelsize": 8.0,
61 "ytick.labelsize": 8.0,
62 "legend.fontsize": 8.0,
63 "figure.dpi": 100,
64 "savefig.dpi": 100,
65 "text.usetex": False,
66 "path.simplify": True
67 })
68 from matplotlib import figure
69 from matplotlib.backends.backend_agg import FigureCanvasAgg
70 figure = figure.Figure()
71 FigureCanvasAgg(figure)
72 return figure
73
74
75def render(fig, buf, dims, fmt):
76 """Render a Matplotlib figure to a GStreamer buffer."""
77 width, height = dims
78 fig.set_size_inches(
79 width / float(fig.get_dpi()),
80 height / float(fig.get_dpi())
81 )
82 fig.canvas.draw()
83 if fmt == "RGB":
84 imgdata = fig.canvas.renderer._renderer.tostring_rgb()
85 elif fmt == "ARGB":
86 imgdata = fig.canvas.renderer._renderer.tostring_argb()
87 elif fmt == "RGBA":
88 imgdata = fig.canvas.renderer._renderer.buffer_rgba()
89 elif fmt == "BGRA":
90 imgdata = fig.canvas.renderer._renderer.tostring_bgra()
91 else:
92 raise ValueError('invalid format "%s"' % fmt)
93 datasize = len(imgdata)
94 buf[:datasize] = imgdata
95 buf.datasize = datasize
96
97
98class BaseMatplotlibTransform(GstBase.BaseTransform):
99 """Base class for transform elements that use Matplotlib to render video."""
100
101 __gsttemplates__ = padtemplate
102
103 def __init__(self):
104 self.figure = figure()
105 self.axes = self.figure.gca()
106
107 def do_transform_caps(self, direction, caps):
108 """GstBaseTransform->transform_caps virtual method."""
109 if direction == Gst.PadDirection.SRC:
110 return self.get_static_pad("sink").get_fixed_caps_func()
111 elif direction == Gst.PadDirection.SINK:
112 return self.get_static_pad("src").get_fixed_caps_func()
113 raise ValueError(direction)
114
115 def do_transform_size(self, direction, caps, size, othercaps):
116 """GstBaseTransform->transform_size virtual method."""
117 if direction == Gst.PadDirection.SINK:
118 return pipeio.get_unit_size(self.get_static_pad("src").query_caps(None))
119 raise ValueError(direction)
120
121GObject.type_register(BaseMatplotlibTransform) # MOD: Found type_register in line: [gobject.type_register(BaseMatplotlibTransform)]
do_transform_size(self, direction, caps, size, othercaps)