CVB++ 15.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{
15 CVB_BEGIN_INLINE_NS
16
18
28 namespace Polimago
29 {
30
43
45
47 class ClassificationPredictor : public PredictorBaseEx
48 {
49 private:
50 // Internal helper constructor version
51 explicit ClassificationPredictor(ReleaseObjectGuard &&guard)
52 : PredictorBaseEx(std::move(guard))
53 {
54 if (TrainingParameters().Usage == CExports::TClassifierUsage::CU_Regression)
55 {
56 throw std::runtime_error(std::string("The object is not a ") + thisObjectName_);
57 }
58
59 auto numClasses = NumClasses();
60 for (int i = 0; i < numClasses; ++i)
61 {
63 CVB_CALL_CAPI_CHECKED(PMGetClfClassLabelTyped(Handle(), i, lbl.data()));
64 classes_.push_back(lbl.data());
65 }
66 }
67
68 // Helper to load a saved predictor from file
69 static CExports::TCLF LoadInternal(const String &fileName)
70 {
71 CExports::TCLF predictor = nullptr;
72
73 CVB_CALL_CAPI_CHECKED(PMOpenClfTyped(fileName.c_str(), predictor));
74 return predictor;
75 }
76
77 void SaveFunction(const String &fileName) const override
78 {
79 CVB_CALL_CAPI_CHECKED(PMSaveClfTyped(fileName.c_str(), Handle()));
80 }
81
82 std::string ObjectName() const override
83 {
84 return thisObjectName_;
85 }
86
87 public:
89
93 explicit ClassificationPredictor(const String &fileName)
94 : ClassificationPredictor(ReleaseObjectGuard(LoadInternal(fileName)))
95 {
96 fileName_ = fileName;
97 }
98
100
107 static std::unique_ptr<ClassificationPredictor> FromHandle(ReleaseObjectGuard &&guard)
108 {
109 if (!guard.Handle())
110 {
111 throw std::invalid_argument("invalid classification predictor native handle");
112 }
113 return std::unique_ptr<ClassificationPredictor>(new ClassificationPredictor(std::move(guard)));
114 }
115
117
123 {
124 return std::make_unique<ClassificationPredictor>(fileName);
125 }
126
128
133 {
134 return classes_;
135 }
136
138
143 {
144 if (TrainingParameters().Usage == CExports::TClassifierUsage::CU_ClassifyOneVersusAll)
145 {
147 }
148 else if (TrainingParameters().Usage == CExports::TClassifierUsage::CU_ClassifyOneVersusOne)
149 {
151 }
152 else
153 {
154 throw std::runtime_error("invalid classification type");
155 }
156 }
157
159
163 int OutputDimension() const
164 {
165 return CVB_CALL_CAPI(PMGetOutputDimension(Handle()));
166 }
167
169
173 int NumClasses() const
174 {
175 return static_cast<int>(CVB_CALL_CAPI(PMGetNumClasses(Handle())));
176 }
177
179
187 {
188 VerifyCompatibility(img, pos);
189
191 double confidence = 0.0;
192 confidences.assign(NumClasses(), 0.0);
193 CVB_CALL_CAPI_CHECKED(PMClassifyTyped(Handle(), img.Handle(), pos.X(), pos.Y(), lbl.data(), classNameMaxLength_,
194 confidence, confidences.data()));
195
196 return ClassificationResult(lbl.data(), confidence);
197 }
198
200
207 {
208 std::vector<double> confidences;
209 return Classify(img, pos, confidences);
210 }
211
212 private:
213 std::vector<String> classes_;
214 static const int classNameMaxLength_ = 256;
215 const std::string thisObjectName_ = "Polimago Classification Predictor";
216 };
217
220
221 } /* namespace Polimago */
222 CVB_END_INLINE_NS
223} /* namespace Cvb */
224
The Common Vision Blox image.
Definition decl_image.hpp:45
Multi-purpose 2D vector class.
Definition point_2d.hpp:20
T X() const noexcept
Gets the x-component of the point.
Definition point_2d.hpp:84
T Y() const noexcept
Gets the y-component of the point.
Definition point_2d.hpp:104
int NumClasses() const
Number of classes a classification predictor has been trained for.
Definition classification_predictor.hpp:173
static std::unique_ptr< ClassificationPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition classification_predictor.hpp:107
static std::unique_ptr< ClassificationPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition classification_predictor.hpp:122
ClassificationResult Classify(const Image &img, Point2D< int > pos)
Classify a location inside an image.
Definition classification_predictor.hpp:206
ClassificationPredictor(const String &fileName)
Load a saved Polimago classification predictor from a file.
Definition classification_predictor.hpp:93
int OutputDimension() const
Dimension of results generated by this predictor.
Definition classification_predictor.hpp:163
std::vector< String > Classes() const
Class labels available in this predictor.
Definition classification_predictor.hpp:132
ClassificationType Classification() const
The classification type for which this classifier has been generated.
Definition classification_predictor.hpp:142
ClassificationResult Classify(const Image &img, Point2D< int > pos, std::vector< double > &confidences)
Classify a location inside an image.
Definition classification_predictor.hpp:186
Polimago classification result container.
Definition classification_result.hpp:20
void * Handle() const noexcept
Classic API Polimago handle.
Definition predictor_base.hpp:68
T move(T... args)
Namespace for the Polimago package.
Definition classification_predictor.hpp:29
std::shared_ptr< ClassificationPredictor > ClassificationPredictorPtr
Convenience shared pointer for ClassificationPredictor.
Definition classification_predictor.hpp:219
ClassificationType
Determine the classification type to be carried out.
Definition classification_predictor.hpp:33
@ None
The enum element indicating undefined state.
Definition classification_predictor.hpp:35
@ OneVersusAll
Definition classification_predictor.hpp:38
@ OneVersusOne
Definition classification_predictor.hpp:41
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49