CVB++ 14.0
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
13namespace Cvb
14{
15CVB_BEGIN_INLINE_NS
16
18
27namespace Polimago
28{
29
32{
34 None,
39};
40
42
45{
46private:
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
84public:
86
92 : ClassificationPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
93 {
94 fileName_ = fileName;
95 }
96
99
101 ClassificationPredictor& operator=(ClassificationPredictor&&) noexcept = default;
102
103 virtual ~ClassificationPredictor ()
104 {}
105
107
114 static std::unique_ptr<ClassificationPredictor> FromHandle (ReleaseObjectGuard&& guard)
115 {
116 if (!guard.Handle ())
117 {
118 throw std::invalid_argument ("invalid classification predictor native handle");
119 }
121 }
122
124
130 {
132 }
133
134
136
141 {
142 return classes_;
143 }
144
146
151 {
152 if (trainingParameters_.Usage == CExports::TClassifierUsage::CU_ClassifyOneVersusAll)
153 {
155 }
156 else if (trainingParameters_.Usage == CExports::TClassifierUsage::CU_ClassifyOneVersusOne)
157 {
159 }
160 else
161 {
162 throw std::runtime_error ("invalid classification type");
163 }
164 }
165
167
171 int OutputDimension() const
172 {
173 return CVB_CALL_CAPI(PMGetOutputDimension(Handle()));
174 }
175
177
181 int NumClasses() const
182 {
183 return static_cast<int>(CVB_CALL_CAPI(PMGetNumClasses(Handle())));
184 }
185
187
195 {
196 VerifyCompatibility(img, pos);
197
198 Char lbl[classNameMaxLength_];
199 double confidence = 0.0;
200 confidences.assign (NumClasses (), 0.0);
201 CVB_CALL_CAPI_CHECKED(PMClassifyTyped(Handle(), img.Handle(), pos.X(), pos.Y(), lbl, classNameMaxLength_, confidence, &confidences[0]));
202
203 return ClassificationResult (lbl, confidence);
204 }
205
207
214 {
215 std::vector<double> confidences;
216 return Classify (img, pos, confidences);
217 }
218
219private:
220 std::vector<String> classes_;
221 static const int classNameMaxLength_ = 256;
222 const std::string thisObjectName_ = "Polimago Classification Predictor";
223};
224
227
228
229
230} /* namespace Polimago */
231CVB_END_INLINE_NS
232} /* namespace Cvb */
233
234
235
236
The Common Vision Blox image.
Definition: decl_image.hpp:45
T X() const noexcept
Gets the x-component of the point.
Definition: point_2d.hpp:86
T Y() const noexcept
Gets the y-component of the point.
Definition: point_2d.hpp:106
Predictor to classify patterns with.
Definition: classification_predictor.hpp:45
int NumClasses() const
Number of classes a classification predictor has been trained for.
Definition: classification_predictor.hpp:181
static std::unique_ptr< ClassificationPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: classification_predictor.hpp:114
static std::unique_ptr< ClassificationPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: classification_predictor.hpp:129
ClassificationResult Classify(const Image &img, Point2D< int > pos)
Classify a location inside an image.
Definition: classification_predictor.hpp:213
ClassificationPredictor(const String &fileName)
Load a saved Polimago classification predictor from a file.
Definition: classification_predictor.hpp:91
int OutputDimension() const
Dimension of results generated by this predictor.
Definition: classification_predictor.hpp:171
std::vector< String > Classes() const
Class labels available in this predictor.
Definition: classification_predictor.hpp:140
ClassificationType Classification() const
The classification type for which this classifier has been generated.
Definition: classification_predictor.hpp:150
ClassificationResult Classify(const Image &img, Point2D< int > pos, std::vector< double > &confidences)
Classify a location inside an image.
Definition: classification_predictor.hpp:194
ClassificationPredictor(ClassificationPredictor &&) noexcept=default
Move constructor.
Polimago classification result container.
Definition: classification_result.hpp:20
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:66
Base class for Polimago predictors.
Definition: predictor_base.hpp:284
ClassificationType
Determine the classification type to be carried out.
Definition: classification_predictor.hpp:32
@ OneVersusAll
Quick classification, that tests versus all classes simultaneously (but potentially at the cost of re...
@ OneVersusOne
Thorough classification, that tests all possible pairs of classes (which yields potentially better re...
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
char Char
Character type for wide characters or unicode characters.
Definition: string.hpp:59