JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
PostProcessorPython.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2021 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 
22 
23 // ####################################################################################################
24 namespace jevois
25 {
26  namespace dnn
27  {
29  {
30  public:
32  void loadpy(std::string const & pypath);
33  virtual ~PostProcessorPythonImpl();
34  void freeze(bool doit);
35  void process(std::vector<cv::Mat> const & outs, PreProcessor * preproc);
36  void report(jevois::StdModule * mod, jevois::RawImage * outimg = nullptr,
37  jevois::OptGUIhelper * helper = nullptr, bool overlay = true, bool idle = false);
38  };
39  }
40 }
41 
42 // ####################################################################################################
43 // ####################################################################################################
45 { }
46 
47 // ####################################################################################################
49 {
50  if (jevois::python::hasattr(PythonWrapper::pyinst(), "freeze")) PythonWrapper::pyinst().attr("freeze")(doit);
51 }
52 
53 // ####################################################################################################
54 void jevois::dnn::PostProcessorPythonImpl::loadpy(std::string const & pypath)
55 {
56  // Load the code and instantiate the python object:
57  PythonWrapper::pythonload(JEVOIS_SHARE_PATH "/" + pypath);
58  LINFO("Loaded " << pypath);
59 
60  // Now that we are fully up and ready, call python module's init() function if implemented:
62 }
63 
64 // ####################################################################################################
65 void jevois::dnn::PostProcessorPythonImpl::process(std::vector<cv::Mat> const & outs,
66  jevois::dnn::PreProcessor * preproc)
67 {
68  boost::python::list lst = jevois::python::pyVecToList(outs);
69  PythonWrapper::pyinst().attr("process")(lst, boost::python::ptr(preproc->getPreProcForPy().get()));
70 }
71 
72 // ####################################################################################################
74  jevois::RawImage * outimg, jevois::OptGUIhelper * helper,
75  bool overlay, bool idle)
76 {
77  // default constructed boost::python::object is None on the python side
78  if (outimg)
79  {
80 #ifdef JEVOIS_PRO
81  if (helper)
82  {
83  jevois::GUIhelperPython helperpy(helper);
84  PythonWrapper::pyinst().attr("report")(boost::ref(*outimg), boost::ref(helperpy), overlay, idle);
85  }
86  else
87 #endif
88  PythonWrapper::pyinst().attr("report")(boost::ref(*outimg), boost::python::object(), overlay, idle);
89  }
90  else
91  {
92 #ifdef JEVOIS_PRO
93  if (helper)
94  {
95  jevois::GUIhelperPython helperpy(helper);
96  PythonWrapper::pyinst().attr("report")(boost::python::object(), boost::ref(helperpy), overlay, idle);
97  }
98  else
99 #endif
100  PythonWrapper::pyinst().attr("report")(boost::python::object(), boost::python::object(), overlay, idle);
101  }
102 
103 #ifndef JEVOIS_PRO
104  (void)helper; // avoid compiler warning
105 #endif
106 }
107 
108 // ####################################################################################################
109 // ####################################################################################################
111  jevois::dnn::PostProcessor(instance)
112 {
113  itsImpl = addSubComponent<jevois::dnn::PostProcessorPythonImpl>("pypost");
114 }
115 
116 // ####################################################################################################
118 { }
119 
120 // ####################################################################################################
122 {
123  // First our own params:
124  pypost::freeze(doit);
125 
126  // Then our python params:
127  itsImpl->freeze(doit);
128 }
129 
130 // ####################################################################################################
131 void jevois::dnn::PostProcessorPython::onParamChange(jevois::dnn::postprocessor::pypost const &
132  JEVOIS_UNUSED_PARAM(param), std::string const & newval)
133 {
134  if (newval.empty() == false) itsImpl->loadpy(newval);
135 }
136 
137 // ####################################################################################################
138 void jevois::dnn::PostProcessorPython::process(std::vector<cv::Mat> const & outs, jevois::dnn::PreProcessor * preproc)
139 { itsImpl->process(outs, preproc); }
140 
141 // ####################################################################################################
143  jevois::OptGUIhelper * helper, bool overlay, bool idle)
144 { itsImpl->report(mod, outimg, helper, overlay, idle); }
145 
jevois::dnn::PostProcessorPythonImpl
Definition: PostProcessorPython.C:28
jevois::ParameterRegistry::Component
friend class Component
Allow Component and DynamicParameter to access our registry data, everyone else is locked out.
Definition: ParameterRegistry.H:51
PythonSupport.H
jevois::dnn::PostProcessorPython::onParamChange
void onParamChange(postprocessor::pypost const &param, std::string const &newval) override
Definition: PostProcessorPython.C:131
jevois::python::pyVecToList
boost::python::list pyVecToList(std::vector< T > const &v)
Helper to convert std::vector<T> to python list.
jevois::PythonWrapper::pyinst
boost::python::object & pyinst()
Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
Definition: PythonWrapper.C:85
jevois::dnn::PostProcessorPythonImpl::loadpy
void loadpy(std::string const &pypath)
Definition: PostProcessorPython.C:54
jevois::Component
A component of a model hierarchy.
Definition: Component.H:181
jevois::RawImage
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
jevois::dnn::PostProcessorPython::freeze
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
Definition: PostProcessorPython.C:121
jevois::dnn::PostProcessorPython::PostProcessorPython
PostProcessorPython(std::string const &instance)
Constructor.
Definition: PostProcessorPython.C:110
jevois::GUIhelperPython
Wrapper around GUIhelper to be used by Python.
Definition: PythonModule.H:224
jevois::GUIhelper
Helper class to assist modules in creating graphical and GUI elements.
Definition: GUIhelper.H:128
PreProcessorPython.H
jevois::dnn::PostProcessorPython::itsImpl
std::shared_ptr< PostProcessorPythonImpl > itsImpl
Definition: PostProcessorPython.H:60
jevois::dnn::PostProcessorPythonImpl::process
void process(std::vector< cv::Mat > const &outs, PreProcessor *preproc)
Definition: PostProcessorPython.C:65
jevois
Definition: Concepts.dox:1
jevois::dnn::PreProcessor
Pre-Processor for neural network pipeline.
Definition: PreProcessor.H:108
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::dnn::PostProcessorPythonImpl::~PostProcessorPythonImpl
virtual ~PostProcessorPythonImpl()
Definition: PostProcessorPython.C:44
jevois::dnn::PostProcessorPythonImpl::report
void report(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false)
Definition: PostProcessorPython.C:73
jevois::dnn::PostProcessorPython::report
void report(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false) override
Report what happened in last process() to console/output video/GUI.
Definition: PostProcessorPython.C:142
PythonModule.H
jevois::dnn::PostProcessorPython::~PostProcessorPython
virtual ~PostProcessorPython()
Destructor.
Definition: PostProcessorPython.C:117
PostProcessorPython.H
jevois::dnn::PostProcessorPython::process
void process(std::vector< cv::Mat > const &outs, PreProcessor *preproc) override
Process outputs and draw/send some results.
Definition: PostProcessorPython.C:138
jevois::PythonWrapper::pythonload
void pythonload(std::string const &path)
Init from path if default constructor was used.
Definition: PythonWrapper.C:34
jevois::dnn::PostProcessor
Post-Processor for neural network pipeline.
Definition: PostProcessor.H:146
jevois::PythonWrapper
Helper class to run python code from C++.
Definition: PythonWrapper.H:41
jevois::dnn::PreProcessor::getPreProcForPy
std::shared_ptr< PreProcessorForPython > getPreProcForPy() const
Get a pointer to our python-friendly interface.
Definition: PreProcessor.C:127
jevois::StdModule
Base class for a module that supports standardized serial messages.
Definition: Module.H:232
LINFO
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:194
jevois::dnn::PostProcessorPythonImpl::freeze
void freeze(bool doit)
Definition: PostProcessorPython.C:48