JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
PythonParameter.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 20122by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
20#include <jevois/Core/Engine.H>
21
22// ####################################################################################################
25
26// ####################################################################################################
29
30// ####################################################################################################
33
34// ####################################################################################################
35// ####################################################################################################
36namespace
37{
38 // Function signature for PyParHelper<T>::create
39 using padp = std::shared_ptr<jevois::python::PyParHelperBase>(*)(jevois::Component *, std::string const &,
40 std::string const &, boost::python::object const &,
42
43 static padp padp_bool = jevois::python::PyParHelper<bool>::create;
44 static padp padp_char = jevois::python::PyParHelper<char>::create;
46 static padp padp_int16 = jevois::python::PyParHelper<int16_t>::create;
47 static padp padp_uint16 = jevois::python::PyParHelper<uint16_t>::create;
51 static padp padp_ulong = jevois::python::PyParHelper<uint64_t>::create;
52 static padp padp_float = jevois::python::PyParHelper<float>::create;
53 static padp padp_double = jevois::python::PyParHelper<double>::create;
55 static padp padp_ipoint = jevois::python::PyParHelper<cv::Point_<int>>::create;
56 static padp padp_fpoint = jevois::python::PyParHelper<cv::Point_<float>>::create;
57 static padp padp_isize = jevois::python::PyParHelper<cv::Size_<int>>::create;
58 static padp padp_fsize = jevois::python::PyParHelper<cv::Size_<float>>::create;
59 static padp padp_iscalar = jevois::python::PyParHelper<cv::Scalar_<int>>::create;
60 static padp padp_fscalar = jevois::python::PyParHelper<cv::Scalar_<float>>::create;
61
62#ifdef JEVOIS_PRO
63 static padp padp_color = jevois::python::PyParHelper<ImColor>::create;
64#endif
65 // jevois::Parameter also supports pair<T,S>, map<K,V>, vector<T>, etc in C++...
66
67 // Now create a mapping from type names exposed to python to C++ types:
68 static std::map<std::string, padp> padp_map
69 {
70 { "bool", padp_bool },
71 { "char", padp_char },
72 { "byte", padp_byte },
73 { "uint8", padp_byte },
74 { "int16", padp_int16 },
75 { "uint16", padp_uint16 },
76 { "int", padp_int },
77 { "uint", padp_uint },
78 { "int32", padp_int },
79 { "uint32", padp_uint },
80 { "long", padp_long },
81 { "int64", padp_long },
82 { "uint64", padp_ulong },
83 { "float", padp_float },
84 { "double", padp_double },
85 { "str", padp_string },
86 { "ipoint", padp_ipoint },
87 { "fpoint", padp_fpoint },
88 { "isize", padp_isize },
89 { "fsize", padp_fsize },
90 { "iscalar", padp_iscalar },
91 { "fscalar", padp_fscalar },
92
93#ifdef JEVOIS_PRO
94 { "color", padp_color },
95#endif
96 };
97} // anonymous namespace
98
99// ####################################################################################################
100jevois::PythonParameter::PythonParameter(boost::python::object & pyinst, std::string const & name,
101 std::string const & typ, std::string const & description,
102 boost::python::object const & defaultValue,
103 jevois::ParameterCategory const & category)
104{
105 auto itr = padp_map.find(typ);
106
107 if (itr == padp_map.end())
108 {
109 std::string errmsg = "Unsupported parameter type [" + typ + "] -- Supported types are: ";
110 for (auto const & mapping : padp_map) errmsg += std::string(mapping.first) + ", ";
111 LFATAL(errmsg << "as of currently running JeVois " << JEVOIS_VERSION_STRING);
112 }
113
114 // We add the parameter to component registered with the python code:
115 jevois::Component * comp = jevois::python::engine()->getPythonComponent(pyinst.ptr()->ob_type);
116
117 // Ok, create the parameter with the desired type:
118 itsPyPar = itr->second(comp, name, description, defaultValue, category);
119}
120
121// ####################################################################################################
124
125// ####################################################################################################
126std::string const & jevois::PythonParameter::name() const
127{ return itsPyPar->par()->name(); }
128
129// ####################################################################################################
131{ return itsPyPar->par()->descriptor(); }
132
133// ####################################################################################################
134boost::python::object jevois::PythonParameter::get() const
135{ return itsPyPar->get(); }
136
137// ####################################################################################################
138void jevois::PythonParameter::set(boost::python::object const & newVal)
139{ itsPyPar->set(newVal); }
140
141// ####################################################################################################
142std::string const jevois::PythonParameter::strget() const
143{ return itsPyPar->par()->strget(); }
144
145// ####################################################################################################
146void jevois::PythonParameter::strset(std::string const & valstring)
147{ itsPyPar->par()->strset(valstring); }
148
149// ####################################################################################################
151{ itsPyPar->par()->freeze(doit); }
152
153// ####################################################################################################
155{ return itsPyPar->par()->frozen(); }
156
157// ####################################################################################################
159{ itsPyPar->par()->reset(); }
160
161// ####################################################################################################
162void jevois::PythonParameter::setCallback(boost::python::object const & cb)
163{ itsPyPar->setCallback(cb); }
#define JEVOIS_VERSION_STRING
Software version, as string.
Definition Config.H:70
A component of a model hierarchy.
Definition Component.H:182
std::string descriptor() const
Get our full descriptor (including all parents) as [Instancename]:[...]:[...].
Definition Component.C:276
std::string const & name() const
Get the parameter name.
void set(boost::python::object const &newVal)
Set the value of this Parameter.
std::string const strget() const
Get the value as a string.
void strset(std::string const &valstring)
Set the value from a string representation of it.
void reset()
Reset this parameter to its default value.
bool frozen() const
Returns whether parameter is frozen.
PythonParameter(boost::python::object &pyinst, std::string const &name, std::string const &typ, std::string const &description, boost::python::object const &defaultValue, jevois::ParameterCategory const &category)
Constructor. Adds a dynamic parameter to the Component associated with pyinst.
boost::python::object get() const
Get the value of this Parameter.
void freeze(bool doit)
Freeze/unfreeze this parameter, it becomes read-only and will not show up in the help message.
void setCallback(boost::python::object const &cb)
Set the parameter's callback.
~PythonParameter()
Destructor. Removes the dynamic parameter from the associated Component.
std::string descriptor() const
Get the parameter fully-qualified name, aka descriptor, including names of owning Component and all p...
jevois::Component * comp() const
Access the associated C++ Component.
virtual ~PyParHelperBase()
Remove param from component.
PyParHelperBase(jevois::Component *comp)
Constructor.
Typed class to allow creation of Parameter in Python.
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
A category to which multiple ParameterDef definitions can belong.