CVB++ 14.1
regression_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
9#include <utility>
10#include <vector>
11
12namespace Cvb
13{
14CVB_BEGIN_INLINE_NS
15
17namespace Polimago
18{
19
21
24{
25private:
26 // Internal helper constructor version
27 explicit RegressionPredictor (ReleaseObjectGuard&& guard)
28 : PredictorBaseEx (std::move (guard))
29 {
30 if (TrainingParameters().Usage != CExports::TClassifierUsage::CU_Regression)
31 {
32 throw std::runtime_error (std::string("The object is not a ") + thisObjectName_);
33 }
34 }
35
36 // Helper to load a saved predictor from file
37 static CExports::TCLF LoadInternal (const String & fileName)
38 {
39 CExports::TCLF predictor = nullptr;
40
41 CVB_CALL_CAPI_CHECKED (PMOpenClfTyped(fileName.c_str(), predictor));
42 return predictor;
43 }
44
45 void SaveFunction (const String &fileName) const override
46 {
47 CVB_CALL_CAPI_CHECKED (PMSaveClfTyped(fileName.c_str(), Handle()));
48 }
49
50 std::string ObjectName () const override
51 {
52 return thisObjectName_;
53 }
54
55public:
57
61 explicit RegressionPredictor (const String & fileName)
62 : RegressionPredictor (ReleaseObjectGuard(LoadInternal (fileName)))
63 {
64 fileName_ = fileName;
65 }
66
67
69
76 static std::unique_ptr<RegressionPredictor> FromHandle (ReleaseObjectGuard&& guard)
77 {
78 if (!guard.Handle ())
79 {
80 throw std::invalid_argument ("invalid regression predictor native handle");
81 }
83 }
84
86
92 {
93 return std::make_unique<RegressionPredictor>(fileName);
94 }
95
97
102 {
103 return static_cast<int>(CVB_CALL_CAPI(PMGetOutputDimension(Handle())));
104 }
105
107
114 {
115 VerifyCompatibility(img, pos);
116
118 CVB_CALL_CAPI_CHECKED (PMPredictVector(Handle(), img.Handle(), pos.X(), pos.Y(), &retval[0]));
119 return retval;
120 }
121
122private:
123 const std::string thisObjectName_ = "Polimago Regression Predictor";
124};
125
128
129
130
131} /* namespace Polimago */
132CVB_END_INLINE_NS
133} /* namespace Cvb */
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
void * Handle() const noexcept
Classic API Polimago handle.
Definition: predictor_base.hpp:69
Base class for Polimago predictors.
Definition: predictor_base.hpp:293
Polimago Regression predictor.
Definition: regression_predictor.hpp:24
std::vector< double > PredictVector(const Image &img, Point2D< int > pos) const
Calculate a regression result on a given location.
Definition: regression_predictor.hpp:113
int RegressionDimension() const
Regression result dimension.
Definition: regression_predictor.hpp:101
static std::unique_ptr< RegressionPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition: regression_predictor.hpp:91
static std::unique_ptr< RegressionPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition: regression_predictor.hpp:76
RegressionPredictor(const String &fileName)
Load a saved Polimago regression predictor from a file.
Definition: regression_predictor.hpp:61
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24