JeVois  1.22
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()) { itsLabels.clear(); return; }
41}
42
43// ####################################################################################################
45{
46 if (outs.size() != 1 && itsFirstTime)
47 {
48 itsFirstTime = false;
49 LERROR("Expected 1 output tensor, got " << outs.size() << " - USING FIRST ONE");
50 }
51
52 cv::Mat const & out = outs[0]; uint32_t const sz = out.total();
53 if (out.type() != CV_32F) LFATAL("Need FLOAT32 tensor");
54 uint32_t topk = top::get(); if (topk > sz) topk = sz;
55 uint32_t const fudge = classoffset::get();
56 itsObjRec.clear();
57
58 uint32_t MaxClass[topk]; float fMaxProb[topk];
59 if (softmax::get())
60 {
61 float sm[out.total()];
62 jevois::dnn::softmax((float const *)out.data, sz, 1, 1.0F, sm, false);
63 jevois::dnn::topK(sm, fMaxProb, MaxClass, sz, topk);
64 }
65 else jevois::dnn::topK((float const *)out.data, fMaxProb, MaxClass, sz, topk);
66
67 // Collect the top-k results that are also above threshold, and, possibly that are named in the class file:
68 float const t = cthresh::get(); float const fac = 100.0F * scorescale::get(); bool namonly = namedonly::get();
69
70 for (uint32_t i = 0; i < topk; ++i)
71 {
72 if (fMaxProb[i] * fac < t) break;
73 std::string const label = jevois::dnn::getLabel(itsLabels, MaxClass[i] + fudge, namonly);
74 if (namonly == false || label.empty() == false) // if namedonly desired, skip when class name is empty
75 {
76 jevois::ObjReco o { fMaxProb[i] * fac, label };
77 itsObjRec.emplace_back(o);
78 }
79 }
80}
81
82// ####################################################################################################
84 jevois::OptGUIhelper * helper, bool overlay, bool idle)
85{
86 uint32_t const topk = top::get();
87
88 // If desired, write results to output image:
89 if (outimg && overlay)
90 {
91 int y = 16;
92 jevois::rawimage::writeText(*outimg, jevois::sformat("Top-%u above %.2F%%", topk, cthresh::get()),
93 220, y, jevois::yuyv::White);
94 y += 15;
95
96 for (jevois::ObjReco const & o : itsObjRec)
97 {
98 jevois::rawimage::writeText(*outimg, jevois::sformat("%s: %.2F", o.category.c_str(), o.score),
99 220, y, jevois::yuyv::White);
100 y += 11;
101 }
102 }
103
104#ifdef JEVOIS_PRO
105 // If desired, write results to GUI:
106 if (helper)
107 {
108 if (idle == false && ImGui::CollapsingHeader("Classification results", ImGuiTreeNodeFlags_DefaultOpen))
109 {
110 ImGui::Text("Top-%u classes above threshold %.2f", topk, cthresh::get());
111 ImGui::Separator();
112 uint32_t done = 0;
113 for (jevois::ObjReco const & o : itsObjRec) { ImGui::Text("%s: %.2F", o.category.c_str(), o.score); ++done; }
114 while (done++ < topk) ImGui::TextUnformatted("-");
115 }
116
117 if (overlay)
118 {
119 uint32_t done = 0;
120 for (jevois::ObjReco const & o : itsObjRec)
121 { helper->itext(jevois::sformat("%s: %.2F", o.category.c_str(), o.score)); ++done; }
122 while (done++ < topk) helper->itext("-");
123 }
124 }
125#else
126 (void)idle; (void)helper; // keep compiler happy
127#endif
128
129 // If desired, send results to serial port:
130 if (mod && serialreport::get()) mod->sendSerialObjReco(itsObjRec);
131}
132
133// ####################################################################################################
134std::vector<jevois::ObjReco> const & jevois::dnn::PostProcessorClassify::latestRecognitions() const
135{ return itsObjRec; }
#define JEVOIS_SHARE_PATH
Base path for shared files (e.g., neural network weights, etc)
Definition Config.H:82
#define o
Definition Font10x20.C:6
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:654
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
std::vector< ObjReco > const & latestRecognitions() const
Get the latest recognition results, use with caution, not thread-safe.
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:716
std::string getLabel(std::map< int, std::string > const &labels, int id, bool namedonly=false)
Get a label from an id.
Definition Utils.C:68
std::map< int, std::string > readLabelsFile(std::string const &fname)
Read a label file.
Definition Utils.C:25
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:89
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 sformat(char const *fmt,...) __attribute__((format(__printf__
Create a string using printf style arguments.
Definition Utils.C:440
std::filesystem::path absolutePath(std::filesystem::path const &root, std::filesystem::path const &path)
Compute an absolute path from two paths.
Definition Utils.C:386
unsigned short constexpr White
YUYV color value.
Definition RawImage.H:59
A trivial struct to store object recognition results.
Definition ObjReco.H:25