JeVois  1.18
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
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 // ####################################################################################################
24 { }
25 
26 // ####################################################################################################
28 { }
29 
30 // ####################################################################################################
32 { return itsComp; }
33 
34 // ####################################################################################################
35 // ####################################################################################################
36 namespace
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;
45  static padp padp_byte = jevois::python::PyParHelper<uint8_t>::create;
46  static padp padp_int16 = jevois::python::PyParHelper<int16_t>::create;
47  static padp padp_uint16 = jevois::python::PyParHelper<uint16_t>::create;
48  static padp padp_int = jevois::python::PyParHelper<int32_t>::create;
49  static padp padp_uint = jevois::python::PyParHelper<uint32_t>::create;
50  static padp padp_long = jevois::python::PyParHelper<int64_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;
54  static padp padp_string = jevois::python::PyParHelper<std::string>::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 // ####################################################################################################
100 jevois::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 // ####################################################################################################
123 { }
124 
125 // ####################################################################################################
126 std::string const & jevois::PythonParameter::name() const
127 { return itsPyPar->par()->name(); }
128 
129 // ####################################################################################################
131 { return itsPyPar->par()->descriptor(); }
132 
133 // ####################################################################################################
134 boost::python::object jevois::PythonParameter::get() const
135 { return itsPyPar->get(); }
136 
137 // ####################################################################################################
138 void jevois::PythonParameter::set(boost::python::object const & newVal)
139 { itsPyPar->set(newVal); }
140 
141 // ####################################################################################################
142 std::string const jevois::PythonParameter::strget() const
143 { return itsPyPar->par()->strget(); }
144 
145 // ####################################################################################################
146 void jevois::PythonParameter::strset(std::string const & valstring)
147 { itsPyPar->par()->strset(valstring); }
148 
149 // ####################################################################################################
151 { itsPyPar->par()->freeze(doit); }
152 
153 // ####################################################################################################
155 { itsPyPar->par()->reset(); }
156 
157 // ####################################################################################################
158 void jevois::PythonParameter::setCallback(boost::python::object const & cb)
159 { itsPyPar->setCallback(cb); }
PythonSupport.H
jevois::PythonParameter::strget
const std::string strget() const
Get the value as a string.
Definition: PythonParameter.C:142
jevois::PythonParameter::set
void set(boost::python::object const &newVal)
Set the value of this Parameter.
Definition: PythonParameter.C:138
jevois::python::PyParHelperBase::PyParHelperBase
PyParHelperBase(jevois::Component *comp)
Constructor.
Definition: PythonParameter.C:23
jevois::PythonParameter::PythonParameter
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.
Definition: PythonParameter.C:100
jevois::PythonParameter::name
const std::string & name() const
Get the parameter name.
Definition: PythonParameter.C:126
jevois::Component
A component of a model hierarchy.
Definition: Component.H:181
jevois::PythonParameter::freeze
void freeze(bool doit)
Freeze/unfreeze this parameter, it becomes read-only and will not show up in the help message.
Definition: PythonParameter.C:150
jevois::PythonParameter::~PythonParameter
~PythonParameter()
Destructor. Removes the dynamic parameter from the associated Component.
Definition: PythonParameter.C:122
jevois::ParameterCategory
A category to which multiple ParameterDef definitions can belong.
Definition: ParameterDef.H:33
PythonParameter.H
jevois::PythonParameter::strset
void strset(std::string const &valstring)
Set the value from a string representation of it.
Definition: PythonParameter.C:146
Engine.H
jevois::python::PyParHelperBase::comp
jevois::Component * comp() const
Access the associated C++ Component.
Definition: PythonParameter.C:31
jevois::PythonParameter::get
boost::python::object get() const
Get the value of this Parameter.
Definition: PythonParameter.C:134
jevois::PythonParameter::setCallback
void setCallback(boost::python::object const &cb)
Set the parameter's callback.
Definition: PythonParameter.C:158
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::PythonParameter::reset
void reset()
Reset this parameter to its default value.
Definition: PythonParameter.C:154
jevois::python::PyParHelperBase::~PyParHelperBase
virtual ~PyParHelperBase()
Remove param from component.
Definition: PythonParameter.C:27
jevois::PythonParameter::descriptor
std::string descriptor() const
Get the parameter fully-qualified name, aka descriptor, including names of owning Component and all p...
Definition: PythonParameter.C:130
jevois::python::PyParHelper
Typed class to allow creation of Parameter in Python.
Definition: PythonParameter.H:59