JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
PreProcessorPython.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/DNN/Utils.H>
22
23// ####################################################################################################
24namespace jevois
25{
26 namespace dnn
27 {
29 {
30 public:
32 void loadpy(std::string const & pypath);
34 void freeze(bool doit);
35 std::vector<cv::Mat> process(cv::Mat const & img, bool swaprb, std::vector<vsi_nn_tensor_attr_t> const & attrs,
36 std::vector<cv::Rect> & crops);
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// ####################################################################################################
47
48// ####################################################################################################
53
54// ####################################################################################################
55void jevois::dnn::PreProcessorPythonImpl::loadpy(std::string const & pypath)
56{
57 // Load the code and instantiate the python object:
59 LINFO("Loaded " << pypath);
60
61 // Now that we are fully up and ready, call python module's init() function if implemented:
63}
64
65// ####################################################################################################
66std::vector<cv::Mat> jevois::dnn::PreProcessorPythonImpl::process(cv::Mat const & img, bool swaprb,
67 std::vector<vsi_nn_tensor_attr_t> const & attrs,
68 std::vector<cv::Rect> & crops)
69{
70 // Convert the attrs to a list of strings:
71 boost::python::list alist;
72 for (vsi_nn_tensor_attr_t const & a : attrs) alist.append(jevois::dnn::attrstr(a));
73
74 // Run the python code:
75 boost::python::object ret = PythonWrapper::pyinst().attr("process")(img, swaprb, alist);
76
77 // We expect a tuple with first a vector of blobs, then a vector of crops:
78 if (boost::python::len(ret) != 2)
79 throw std::invalid_argument("Expected two return lists for blobs,crops but received " +
80 std::to_string(boost::python::len(ret)));
81
82 // For the crops, we want cv::Rect but they are not exposing it to python it seems. So let's just accept tuples
83 // ( (x,y), (w,h) )
84#define CROPERR "PreProcessorPython::process: crops should be ( (x,y), (w,h) )"
85
86 crops.clear();
87 boost::python::list cl = boost::python::extract<boost::python::list>(ret[1]);
88 for (ssize_t i = 0; i < boost::python::len(cl); ++i)
89 {
90 boost::python::tuple tup = boost::python::extract<boost::python::tuple>(cl[i]);
91 if (boost::python::len(tup) != 2) throw std::runtime_error(CROPERR);
92 boost::python::tuple xy = boost::python::extract<boost::python::tuple>(tup[0]);
93 if (boost::python::len(xy) != 2) throw std::runtime_error(CROPERR);
94 boost::python::tuple wh = boost::python::extract<boost::python::tuple>(tup[1]);
95 if (boost::python::len(wh) != 2) throw std::runtime_error(CROPERR);
96
97 float x = boost::python::extract<float>(xy[0]);
98 float y = boost::python::extract<float>(xy[1]);
99 float w = boost::python::extract<float>(wh[0]);
100 float h = boost::python::extract<float>(wh[1]);
101 crops.emplace_back(cv::Rect(x, y, w, h));
102 }
103
104 // For the blobs, we have a converter:
105 boost::python::list ml = boost::python::extract<boost::python::list>(ret[0]);
106 return jevois::python::pyListToVec<cv::Mat>(ml);
107}
108
109// ####################################################################################################
111 jevois::OptGUIhelper * helper, bool overlay, bool idle)
112{
113 // default constructed boost::python::object is None on the python side
114 if (outimg)
115 {
116#ifdef JEVOIS_PRO
117 if (helper)
118 {
119 jevois::GUIhelperPython helperpy(helper);
120 PythonWrapper::pyinst().attr("report")(boost::ref(*outimg), boost::ref(helperpy), overlay, idle);
121 }
122 else
123#endif
124 PythonWrapper::pyinst().attr("report")(boost::ref(*outimg), boost::python::object(), overlay, idle);
125 }
126 else
127 {
128#ifdef JEVOIS_PRO
129 if (helper)
130 {
131 jevois::GUIhelperPython helperpy(helper);
132 PythonWrapper::pyinst().attr("report")(boost::python::object(), boost::ref(helperpy), overlay, idle);
133 }
134 else
135#endif
136 PythonWrapper::pyinst().attr("report")(boost::python::object(), boost::python::object(), overlay, idle);
137 }
138
139#ifndef JEVOIS_PRO
140 (void)helper; // avoid compiler warning
141#endif
142}
143
144// ####################################################################################################
145// ####################################################################################################
147 jevois::dnn::PreProcessor(instance)
148{
149 itsImpl = addSubComponent<jevois::dnn::PreProcessorPythonImpl>("pypre");
150}
151
152// ####################################################################################################
155
156// ####################################################################################################
158{
159 // First our own params:
160 pypre::freeze(doit);
161
162 // Then our python params:
163 itsImpl->freeze(doit);
164}
165
166// ####################################################################################################
167void jevois::dnn::PreProcessorPython::onParamChange(jevois::dnn::preprocessor::pypre const &,
168 std::string const & newval)
169{
170 if (newval.empty() == false) itsImpl->loadpy(newval);
171}
172
173// ####################################################################################################
174std::vector<cv::Mat> jevois::dnn::PreProcessorPython::process(cv::Mat const & img, bool swaprb,
175 std::vector<vsi_nn_tensor_attr_t> const & attrs,
176 std::vector<cv::Rect> & crops)
177{ return itsImpl->process(img, swaprb, attrs, crops); }
178
179// ####################################################################################################
181 jevois::OptGUIhelper * helper, bool overlay, bool idle)
182{ itsImpl->report(mod, outimg, helper, overlay, idle); }
183
#define JEVOIS_SHARE_PATH
Base path for shared files (e.g., neural network weights, etc)
Definition Config.H:82
int h
Definition GUIhelper.C:2491
#define CROPERR
A component of a model hierarchy.
Definition Component.H:182
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 report(jevois::StdModule *mod, jevois::RawImage *outimg=nullptr, jevois::OptGUIhelper *helper=nullptr, bool overlay=true, bool idle=false)
std::vector< cv::Mat > process(cv::Mat const &img, bool swaprb, std::vector< vsi_nn_tensor_attr_t > const &attrs, std::vector< cv::Rect > &crops)
void loadpy(std::string const &pypath)
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.
std::shared_ptr< PreProcessorPythonImpl > itsImpl
std::vector< cv::Mat > process(cv::Mat const &img, bool swaprb, std::vector< vsi_nn_tensor_attr_t > const &attrs, std::vector< cv::Rect > &crops) override
Extract blobs from input image.
virtual ~PreProcessorPython()
Destructor.
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
PreProcessorPython(std::string const &instance)
Constructor.
void onParamChange(preprocessor::pypre const &param, std::string const &newval) override
Pre-Processor for neural network pipeline.
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition Log.H:194
std::string attrstr(vsi_nn_tensor_attr_t const &attr)
Get a string describing the specs of a tensor, including quantification specs (not provided by shapes...
Definition Utils.C:512
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