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