classification_predictor.hpp
1 #pragma once
2 
3 #include "../_cexports/c_polimago.h"
4 
5 #include "../global.hpp"
6 #include "../string.hpp"
7 #include "predictor_base.hpp"
8 #include "classification_result.hpp"
9 
10 #include <utility>
11 #include <vector>
12 
13 namespace Cvb
14 {
15 CVB_BEGIN_INLINE_NS
16 
18 
27 namespace Polimago
28 {
29 
32 {
34  None,
39 };
40 
42 
45 {
46 private:
47  // Internal helper constructor version
48  ClassificationPredictor (ReleaseObjectGuard&& guard)
49  : PredictorBaseEx (std::move (guard))
50  {
51  if (trainingParameters_.Usage == CExports::TClassifierUsage::CU_Regression)
52  {
53  throw std::runtime_error (std::string("The object is not a ") + thisObjectName_);
54  }
55 
56  auto numClasses = NumClasses ();
57  for (int i = 0; i < numClasses; ++i)
58  {
59  Char lbl[classNameMaxLength_];
60  CVB_CALL_CAPI_CHECKED(PMGetClfClassLabelTyped(Handle(), i, lbl));
61  classes_.push_back (lbl);
62  }
63  }
64 
65  // Helper to load a saved predictor from file
66  static CExports::TCLF LoadInternal (const String & fileName)
67  {
68  CExports::TCLF predictor = nullptr;
69 
70  CVB_CALL_CAPI_CHECKED (PMOpenClfTyped(fileName.c_str(), predictor));
71  return predictor;
72  }
73 
74  void SaveFunction (const String &fileName) const override
75  {
76  CVB_CALL_CAPI_CHECKED (PMSaveClfTyped(fileName.c_str(), Handle()));
77  }
78 
79  std::string ObjectName () const override
80  {
81  return thisObjectName_;
82  }
83 
84 public:
86 
90  ClassificationPredictor (const String & fileName)
91  : ClassificationPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
92  {
93  fileName_ = fileName;
94  }
95 
97  ClassificationPredictor (ClassificationPredictor&&) noexcept = default;
98 
100  ClassificationPredictor& operator=(ClassificationPredictor&&) noexcept = default;
101 
102  virtual ~ClassificationPredictor ()
103  {}
104 
106 
113  static std::unique_ptr<ClassificationPredictor> FromHandle (ReleaseObjectGuard&& guard)
114  {
115  if (!guard.Handle ())
116  {
117  throw std::invalid_argument ("invalid classification predictor native handle");
118  }
120  }
121 
123 
129  {
131  }
132 
133 
135 
140  {
141  return classes_;
142  }
143 
145 
150  {
151  if (trainingParameters_.Usage == CExports::TClassifierUsage::CU_ClassifyOneVersusAll)
152  {
154  }
155  else if (trainingParameters_.Usage == CExports::TClassifierUsage::CU_ClassifyOneVersusOne)
156  {
158  }
159  else
160  {
161  throw std::runtime_error ("invalid classification type");
162  }
163  }
164 
166 
170  int OutputDimension() const
171  {
172  return CVB_CALL_CAPI(PMGetOutputDimension(Handle()));
173  }
174 
176 
180  int NumClasses() const
181  {
182  return static_cast<int>(CVB_CALL_CAPI(PMGetNumClasses(Handle())));
183  }
184 
186 
194  {
195  VerifyCompatibility(img, pos);
196 
197  Char lbl[classNameMaxLength_];
198  double confidence = 0.0;
199  confidences.assign (NumClasses (), 0.0);
200  CVB_CALL_CAPI_CHECKED(PMClassifyTyped(Handle(), img.Handle(), pos.X(), pos.Y(), lbl, classNameMaxLength_, confidence, &confidences[0]));
201 
202  return ClassificationResult (lbl, confidence);
203  }
204 
206 
213  {
214  std::vector<double> confidences;
215  return Classify (img, pos, confidences);
216  }
217 
218 private:
219  std::vector<String> classes_;
220  static const int classNameMaxLength_ = 256;
221  const std::string thisObjectName_ = "Polimago Classification Predictor";
222 };
223 
226 
227 
228 
229 } /* namespace Polimago */
230 CVB_END_INLINE_NS
231 } /* namespace Cvb */
232 
233 
234 
235 
STL class.
int NumClasses() const
Number of classes a classification predictor has been trained for.
Definition: classification_predictor.hpp:180
int OutputDimension() const
Dimension of results generated by this predictor.
Definition: classification_predictor.hpp:170
Thorough classification, that tests all possible pairs of classes (which yields potentially better re...
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
STL class.
Predictor to classify patterns with.
Definition: classification_predictor.hpp:44
ClassificationResult Classify(const Image &img, Point2D< int > pos, std::vector< double > &confidences)
Classify a location inside an image.
Definition: classification_predictor.hpp:193
Root namespace for the Image Manager interface.
Definition: version.hpp:11
The Common Vision Blox image.
Definition: decl_image.hpp:44
ClassificationResult Classify(const Image &img, Point2D< int > pos)
Classify a location inside an image.
Definition: classification_predictor.hpp:212
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
ClassificationType Classification() const
The classification type for which this classifier has been generated.
Definition: classification_predictor.hpp:149
Quick classification, that tests versus all classes simultaneously (but potentially at the cost of re...
Base class for Polimago predictors.
Definition: predictor_base.hpp:283
char Char
Character type for wide characters or unicode characters.
Definition: string.hpp:59
Polimago classification result container.
Definition: classification_result.hpp:19
std::vector< String > Classes() const
Class labels available in this predictor.
Definition: classification_predictor.hpp:139
static std::unique_ptr< ClassificationPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: classification_predictor.hpp:113
void * Handle() const noexcept
Classic API image handle.
Definition: decl_image.hpp:223
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
STL class.
STL class.
The enum element indicating undefined state.
ClassificationPredictor(const String &fileName)
Load a saved Polimago classification predictor from a file.
Definition: classification_predictor.hpp:90
static std::unique_ptr< ClassificationPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: classification_predictor.hpp:128
ClassificationType
Determine the classification type to be carried out.
Definition: classification_predictor.hpp:31