JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
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
21#include <jevois/Core/Engine.H>
23
24// ####################################################################################################
25namespace jevois
26{
27 namespace dnn
28 {
30 {
31 public:
33 void loadpy(std::string const & pypath);
35 void freeze(bool doit);
36 void process(std::vector<cv::Mat> const & outs, PreProcessor * preproc);
37 void report(jevois::StdModule * mod, jevois::RawImage * outimg = nullptr,
38 jevois::OptGUIhelper * helper = nullptr, bool overlay = true, bool idle = false);
39 };
40 }
41}
42
43// ####################################################################################################
44// ####################################################################################################
49
50// ####################################################################################################
55
56// ####################################################################################################
57void jevois::dnn::PostProcessorPythonImpl::loadpy(std::string const & pypath)
58{
59 // Load the code and instantiate the python object:
61 LINFO("Loaded " << pypath);
62
63 // Now that we are fully up and ready, call python module's init() function if implemented:
65}
66
67// ####################################################################################################
68void jevois::dnn::PostProcessorPythonImpl::process(std::vector<cv::Mat> const & outs,
70{
71 if (jevois::python::hasattr(PythonWrapper::pyinst(), "process") == false)
72 LFATAL("No process() method provided. It is required, please add it to your Python post-processor.");
73
74 boost::python::list lst = jevois::python::pyVecToList(outs);
75 PythonWrapper::pyinst().attr("process")(lst, boost::python::ptr(preproc->getPreProcForPy().get()));
76}
77
78// ####################################################################################################
80 jevois::OptGUIhelper * helper, bool overlay, bool idle)
81{
82 if (jevois::python::hasattr(PythonWrapper::pyinst(), "report") == false)
83 LFATAL("No process() method provided. It is required, please add it to your Python post-processor.");
84
85 // default constructed boost::python::object is None on the python side
86 if (outimg)
87 {
88#ifdef JEVOIS_PRO
89 if (helper)
90 {
91 jevois::GUIhelperPython helperpy(helper);
92 PythonWrapper::pyinst().attr("report")(boost::ref(*outimg), boost::ref(helperpy), overlay, idle);
93 }
94 else
95#endif
96 PythonWrapper::pyinst().attr("report")(boost::ref(*outimg), boost::python::object(), overlay, idle);
97 }
98 else
99 {
100#ifdef JEVOIS_PRO
101 if (helper)
102 {
103 jevois::GUIhelperPython helperpy(helper);
104 PythonWrapper::pyinst().attr("report")(boost::python::object(), boost::ref(helperpy), overlay, idle);
105 }
106 else
107#endif
108 PythonWrapper::pyinst().attr("report")(boost::python::object(), boost::python::object(), overlay, idle);
109 }
110
111#ifndef JEVOIS_PRO
112 (void)helper; // avoid compiler warning
113#endif
114}
115
116// ####################################################################################################
117// ####################################################################################################
119 jevois::dnn::PostProcessor(instance)
120{
121 itsImpl = addSubComponent<jevois::dnn::PostProcessorPythonImpl>("pypost");
122}
123
124// ####################################################################################################
127
128// ####################################################################################################
130{
131 // First our own params:
132 pypost::freeze(doit);
133
134 // Then our python params:
135 itsImpl->freeze(doit);
136}
137
138// ####################################################################################################
139void jevois::dnn::PostProcessorPython::onParamChange(jevois::dnn::postprocessor::pypost const &,
140 std::string const & newval)
141{
142 if (newval.empty() == false) itsImpl->loadpy(newval);
143}
144
145// ####################################################################################################
146void jevois::dnn::PostProcessorPython::process(std::vector<cv::Mat> const & outs, jevois::dnn::PreProcessor * preproc)
147{ itsImpl->process(outs, preproc); }
148
149// ####################################################################################################
151 jevois::OptGUIhelper * helper, bool overlay, bool idle)
152{ itsImpl->report(mod, outimg, helper, overlay, idle); }
153
#define JEVOIS_SHARE_PATH
Base path for shared files (e.g., neural network weights, etc)
Definition Config.H:82
A component of a model hierarchy.
Definition Component.H:182
void unRegisterPythonComponent(Component *comp)
Unregister a component as linked to some python code, used by dynamic params created in python.
Definition Engine.C:3016
Wrapper around GUIhelper to be used by Python.
Helper class to assist modules in creating graphical and GUI elements.
Definition GUIhelper.H:133
friend class Component
Allow Component and DynamicParameter to access our registry data, everyone else is locked out.
Helper class to run python code from C++.
void pythonload(std::string const &path)
Init from path if default constructor was used.
boost::python::object & pyinst()
Get the python class pyinst, or throw if construction error occurred (e.g., file not found)
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition RawImage.H:111
Base class for a module that supports standardized serial messages.
Definition Module.H:234
void loadpy(std::string const &pypath)
void process(std::vector< cv::Mat > const &outs, PreProcessor *preproc)
void report(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false)
std::shared_ptr< PostProcessorPythonImpl > itsImpl
void process(std::vector< cv::Mat > const &outs, PreProcessor *preproc) override
Process outputs and draw/send some results.
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.
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
PostProcessorPython(std::string const &instance)
Constructor.
void onParamChange(postprocessor::pypost const &param, std::string const &newval) override
Post-Processor for neural network pipeline.
Pre-Processor for neural network pipeline.
std::shared_ptr< PreProcessorForPython > getPreProcForPy() const
Get a pointer to our python-friendly interface.
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition Log.H:194
boost::python::list pyVecToList(std::vector< T > const &v)
Helper to convert std::vector<T> to python list.
bool hasattr(boost::python::object &o, char const *name)
Check whether a boost::python::object has an attribute.
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2