JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
PythonSupport.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by 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 
18 #pragma once
19 
20 #include <boost/python.hpp>
21 #include <opencv2/core/core.hpp>
22 #include <array>
23 
24 #ifdef JEVOIS_PRO
25 #include <imgui.h>
26 #endif
27 
28 namespace jevois
29 {
30  class Engine;
31 
32  namespace python
33  {
34  /*! \defgroup pysupport Python support functions
35 
36  Helpers to facilitate data transfer to/from Python
37 
38  \ingroup python */
39 
40  /*! @{ */ // **********************************************************************
41 
42  //! Initialize Python, numpy, and allow python modules to send serial outputs through the JeVois Engine
43  /*! This command is not for common use, only Engine should use it. */
44  void setEngine(jevois::Engine * e);
45 
46  //! Get the Engine \relates PythonModule
47  jevois::Engine * engine();
48 
49  //! Check whether a boost::python::object has an attribute
50  bool hasattr(boost::python::object & o, char const * name);
51 
52  //! Helper to convert std::vector<T> to python list
53  template <class T>
54  boost::python::list pyVecToList(std::vector<T> const & v);
55 
56  //! Helper to convert python list (or any iterable) to std::vector<T>
57  template <class T>
58  std::vector<T> pyListToVec(boost::python::object const & lst);
59 
60  //! Helper to extract a tuple with elements of uniform type T into an std::array
61  template <typename T, size_t N>
62  std::array<T, N> tupleToArray(boost::python::object const & o);
63 
64  //! Helper to extract a tuple with elements of uniform type T into an std::array
65  /*! minN is the smallest number of tuple elements required, allowing for partial tuples. If minN<N, we assume that
66  the array has been initialized with default values, and we allow receiving a tuple that is smaller than N, in
67  which case we only assign the first elements of the array. Useful to exract things like cv::Scalar which can
68  contain 1 to 4 elements. */
69  template <typename T, size_t N>
70  void tupleToArray(boost::python::object const & o, std::array<T, N> & arr, size_t minN = N);
71 
72  //! Generic value extraction, pass-through to boost::python::extract() for types that python knows how to extract
73  template <typename T>
74  T pyextract(boost::python::object const & o);
75 
76  //! Specialization for cv::Scalar_<float>, extract from tuple of 1..4 float values
77  template <>
78  cv::Scalar_<float> pyextract<cv::Scalar_<float>>(boost::python::object const & o);
79 
80  //! Specialization for cv::Scalar_<int>, extract from tuple of 1..4 int values
81  template <>
82  cv::Scalar_<int> pyextract<cv::Scalar_<int>>(boost::python::object const & o);
83 
84  //! Specialization for cv::Point_<float>, extract from tuple of 2 float values
85  template <>
86  cv::Point_<float> pyextract<cv::Point_<float>>(boost::python::object const & o);
87 
88  //! Specialization for cv::Point_<int>, extract from tuple of 2 int values
89  template <>
90  cv::Point_<int> pyextract<cv::Point_<int>>(boost::python::object const & o);
91 
92  //! Specialization for cv::Size_<float>, extract from tuple of 2 float values
93  template <>
94  cv::Size_<float> pyextract<cv::Size_<float>>(boost::python::object const & o);
95 
96  //! Specialization for cv::Size_<int>, extract from tuple of 2 int values
97  template <>
98  cv::Size_<int> pyextract<cv::Size_<int>>(boost::python::object const & o);
99 
100 #ifdef JEVOIS_PRO
101  //! Specialization for ImColor, extract from tuple of 3..4 int values (if 3 then, alpha=255)
102  template <>
103  ImColor pyextract<ImColor>(boost::python::object const & o);
104 #endif
105 
106  //! Convert value to python, pass-through to python::object(val)
107  template <typename T>
108  boost::python::object topyobj(T const & val);
109 
110  //! Specialization for cv::Scalar<float>, returns a tuple with 4 float elements
111  template <>
112  boost::python::object topyobj(cv::Scalar_<float> const & val);
113 
114  //! Specialization for cv::Scalar<int>, returns a tuple with 4 int elements
115  template <>
116  boost::python::object topyobj(cv::Scalar_<int> const & val);
117 
118  //! Specialization for cv::Point<float>, returns a tuple with 2 float elements
119  template <>
120  boost::python::object topyobj(cv::Point_<float> const & val);
121 
122  //! Specialization for cv::Point<int>, returns a tuple with 2 int elements
123  template <>
124  boost::python::object topyobj(cv::Point_<int> const & val);
125 
126  //! Specialization for cv::Size<float>, returns a tuple with 2 float elements
127  template <>
128  boost::python::object topyobj(cv::Size_<float> const & val);
129 
130  //! Specialization for cv::Size<int>, returns a tuple with 2 int elements
131  template <>
132  boost::python::object topyobj(cv::Size_<int> const & val);
133 
134 #ifdef JEVOIS_PRO
135  //! Specialization for ImColor, returns a tuple with 4 int elements
136  template <>
137  boost::python::object topyobj(ImColor const & val);
138 #endif
139 
140  /*! @} */ // **********************************************************************
141  } // namespace python
142 } // namespace jevois
143 
144 // Include implementation details
145 #include <jevois/Core/details/PythonSupportImpl.H>
jevois::python::pyListToVec
std::vector< T > pyListToVec(boost::python::object const &lst)
Helper to convert python list (or any iterable) to std::vector<T>
jevois::python::topyobj
boost::python::object topyobj(T const &val)
Convert value to python, pass-through to python::object(val)
jevois::python::pyVecToList
boost::python::list pyVecToList(std::vector< T > const &v)
Helper to convert std::vector<T> to python list.
o
#define o
Definition: Font10x20.C:6
jevois
Definition: Concepts.dox:1
jevois::python::hasattr
bool hasattr(boost::python::object &o, char const *name)
Check whether a boost::python::object has an attribute.
Definition: PythonSupport.C:108
jevois::Engine
JeVois processing engine - gets images from camera sensor, processes them, and sends results over USB...
Definition: Engine.H:387
jevois::python::tupleToArray
std::array< T, N > tupleToArray(boost::python::object const &o)
Helper to extract a tuple with elements of uniform type T into an std::array.
jevois::python::setEngine
void setEngine(jevois::Engine *e)
Initialize Python, numpy, and allow python modules to send serial outputs through the JeVois Engine.
Definition: PythonSupport.C:94
jevois::python::pyextract
T pyextract(boost::python::object const &o)
Generic value extraction, pass-through to boost::python::extract() for types that python knows how to...
jevois::python::pyextract< ImColor >
ImColor pyextract< ImColor >(boost::python::object const &o)
Specialization for ImColor, extract from tuple of 3..4 int values (if 3 then, alpha=255)