JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
PostProcessorClassify.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
19#include <jevois/DNN/Utils.H>
20#include <jevois/Util/Utils.H>
22#include <jevois/Core/Engine.H>
23#include <jevois/Core/Module.H>
25
26// ####################################################################################################
29
30// ####################################################################################################
32{
33 classes::freeze(doit);
34}
35
36// ####################################################################################################
37void jevois::dnn::PostProcessorClassify::onParamChange(postprocessor::classes const &, std::string const & val)
38{
39 if (val.empty()) return;
40
41 // Get the dataroot of our network. We assume that there is a sub-component named "network" that is a sibling of us:
42 std::vector<std::string> dd = jevois::split(Component::descriptor(), ":");
43 dd.back() = "network"; dd.emplace_back("dataroot");
44 std::string const dataroot = engine()->getParamStringUnique(jevois::join(dd, ":"));
45
46 itsLabels = jevois::dnn::readLabelsFile(jevois::absolutePath(dataroot, val));
47}
48
49// ####################################################################################################
51{
52 if (outs.size() != 1 && itsFirstTime)
53 {
54 itsFirstTime = false;
55 LERROR("Expected 1 output tensor, got " << outs.size() << " - USING FIRST ONE");
56 }
57
58 cv::Mat const & out = outs[0]; uint32_t const sz = out.total();
59 if (out.type() != CV_32F) LFATAL("Need FLOAT32 tensor");
60 uint32_t topk = top::get(); if (topk > sz) topk = sz;
61 uint32_t const fudge = classoffset::get();
62 itsObjRec.clear();
63
64 uint32_t MaxClass[topk]; float fMaxProb[topk];
65 if (softmax::get())
66 {
67 float sm[out.total()];
68 jevois::dnn::softmax((float const *)out.data, sz, 1, 1.0F, sm, false);
69 jevois::dnn::topK(sm, fMaxProb, MaxClass, sz, topk);
70 }
71 else jevois::dnn::topK((float const *)out.data, fMaxProb, MaxClass, sz, topk);
72
73 // Collect the top-k results that are also above threshold:
74 float const t = cthresh::get(); float const fac = 100.0F * scorescale::get();
75
76 for (uint32_t i = 0; i < topk; ++i)
77 {
78 if (fMaxProb[i] * fac < t) break;
79 jevois::ObjReco o { fMaxProb[i] * fac, jevois::dnn::getLabel(itsLabels, MaxClass[i] + fudge) };
80 itsObjRec.push_back(o);
81 }
82}
83
84// ####################################################################################################
86 jevois::OptGUIhelper * helper, bool overlay, bool idle)
87{
88 uint32_t const topk = top::get();
89
90 // If desired, write results to output image:
91 if (outimg && overlay)
92 {
93 int y = 16;
94 jevois::rawimage::writeText(*outimg, jevois::sformat("Top-%u above %.2F%%", topk, cthresh::get()),
95 220, y, jevois::yuyv::White);
96 y += 15;
97
98 for (jevois::ObjReco const & o : itsObjRec)
99 {
100 jevois::rawimage::writeText(*outimg, jevois::sformat("%s: %.2F", o.category.c_str(), o.score),
101 220, y, jevois::yuyv::White);
102 y += 11;
103 }
104 }
105
106#ifdef JEVOIS_PRO
107 // If desired, write results to GUI:
108 if (helper)
109 {
110 if (idle == false && ImGui::CollapsingHeader("Classification results", ImGuiTreeNodeFlags_DefaultOpen))
111 {
112 ImGui::Text("Top-%u classes above threshold %.2f", topk, cthresh::get());
113 ImGui::Separator();
114 uint32_t done = 0;
115 for (jevois::ObjReco const & o : itsObjRec) { ImGui::Text("%s: %.2F", o.category.c_str(), o.score); ++done; }
116 while (done++ < topk) ImGui::TextUnformatted("-");
117 }
118
119 if (overlay)
120 {
121 uint32_t done = 0;
122 for (jevois::ObjReco const & o : itsObjRec)
123 { helper->itext(jevois::sformat("%s: %.2F", o.category.c_str(), o.score)); ++done; }
124 while (done++ < topk) helper->itext("-");
125 }
126 }
127#else
128 (void)idle; (void)helper; // keep compiler happy
129#endif
130
131 // If desired, send results to serial port:
132 if (mod) mod->sendSerialObjReco(itsObjRec);
133}
#define o
Definition Font10x20.C:6
std::string descriptor() const
Get our full descriptor (including all parents) as [Instancename]:[...]:[...].
Definition Component.C:276
std::string getParamStringUnique(std::string const &paramdescriptor) const
Get a parameter value by string, simple version assuming only one parameter match.
Definition Component.C:405
Helper class to assist modules in creating graphical and GUI elements.
Definition GUIhelper.H:133
void itext(char const *txt, ImU32 const &col=IM_COL32_BLACK_TRANS, int line=-1)
Draw some overlay text on top of an image.
Definition GUIhelper.C:641
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 sendSerialObjReco(std::vector< ObjReco > const &res)
Send a standardized object recognition message.
Definition Module.C:535
void onParamChange(postprocessor::classes const &param, std::string const &val) override
void freeze(bool doit) override
Freeze/unfreeze parameters that users should not change while running.
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.
Pre-Processor for neural network pipeline.
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
Definition Log.H:230
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211
size_t softmax(float const *input, size_t const n, size_t const stride, float const fac, float *output, bool maxonly)
Apply softmax to a float vector.
Definition Utils.C:668
std::map< int, std::string > readLabelsFile(std::string const &fname)
Read a label file.
Definition Utils.C:25
std::string getLabel(std::map< int, std::string > const &labels, int id)
Get a label from an id.
Definition Utils.C:68
void topK(float const *pfProb, float *pfMaxProb, uint32_t *pMaxClass, uint32_t outputCount, uint32_t topNum)
Get top-k entries and their indices.
Definition Utils.C:85
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
Write some text in an image.
std::string join(std::vector< std::string > const &strings, std::string const &delimiter)
Concatenate a vector of tokens into a string.
Definition Utils.C:280
std::string sformat(char const *fmt,...) __attribute__((format(__printf__
Create a string using printf style arguments.
Definition Utils.C:439
std::filesystem::path absolutePath(std::filesystem::path const &root, std::filesystem::path const &path)
Compute an absolute path from two paths.
Definition Utils.C:385
std::vector< std::string > split(std::string const &input, std::string const &regex="\\s+")
Split string into vector of tokens using a regex to specify what to split on; default regex splits by...
Definition Utils.C:270
unsigned short constexpr White
YUYV color value.
Definition RawImage.H:59
A trivial struct to store object recognition results.
Definition ObjReco.H:24