gstlal
1.13.0
Toggle main menu visibility
Loading...
Searching...
No Matches
python
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
gi.require_version(
'GstBase'
,
'1.0'
)
30
from
gi.repository
import
GObject, Gst, GstBase
31
GObject.threads_init()
32
Gst.init(
None
)
33
34
from
gstlal.pipeutil
import
*
35
from
gstlal
import
pipeio
36
37
38
"""Pad template suitable for producing video frames using Matplotlib.
39
The Agg backend supports rgba, argb, and bgra."""
40
padtemplate = 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
53
def
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
75
def
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
98
class
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
121
GObject.type_register(BaseMatplotlibTransform)
# MOD: Found type_register in line: [gobject.type_register(BaseMatplotlibTransform)]
python.matplotlibhelper.BaseMatplotlibTransform
Definition
matplotlibhelper.py:98
python.matplotlibhelper.BaseMatplotlibTransform.do_transform_caps
do_transform_caps(self, direction, caps)
Definition
matplotlibhelper.py:107
python.matplotlibhelper.BaseMatplotlibTransform.figure
figure
Definition
matplotlibhelper.py:104
python.matplotlibhelper.BaseMatplotlibTransform.do_transform_size
do_transform_size(self, direction, caps, size, othercaps)
Definition
matplotlibhelper.py:115
python.matplotlibhelper.BaseMatplotlibTransform.axes
axes
Definition
matplotlibhelper.py:105
__init__
pipeutil
Generated by
1.17.0