CVB++ 15.0
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{
14 CVB_BEGIN_INLINE_NS
15
17 namespace Polimago
18 {
19
21
23 class RegressionPredictor : public PredictorBaseEx
24 {
25 private:
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
55 public:
57
61 explicit RegressionPredictor(const String &fileName)
62 : RegressionPredictor(ReleaseObjectGuard(LoadInternal(fileName)))
63 {
64 fileName_ = fileName;
65 }
66
68
75 static std::unique_ptr<RegressionPredictor> FromHandle(ReleaseObjectGuard &&guard)
76 {
77 if (!guard.Handle())
78 {
79 throw std::invalid_argument("invalid regression predictor native handle");
80 }
81 return std::unique_ptr<RegressionPredictor>(new RegressionPredictor(std::move(guard)));
82 }
83
85
91 {
92 return std::make_unique<RegressionPredictor>(fileName);
93 }
94
96
101 {
102 return static_cast<int>(CVB_CALL_CAPI(PMGetOutputDimension(Handle())));
103 }
104
106
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
122 private:
123 const std::string thisObjectName_ = "Polimago Regression Predictor";
124 };
125
128
129 } /* namespace Polimago */
130 CVB_END_INLINE_NS
131} /* namespace Cvb */
The Common Vision Blox image.
Definition decl_image.hpp:50
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
void * Handle() const noexcept
Classic API Polimago handle.
Definition predictor_base.hpp:68
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:100
static std::unique_ptr< RegressionPredictor > Load(const String &fileName)
Load a saved predictor from a file.
Definition regression_predictor.hpp:90
static std::unique_ptr< RegressionPredictor > FromHandle(ReleaseObjectGuard &&guard)
Creates predictor from a classic API handle.
Definition regression_predictor.hpp:75
RegressionPredictor(const String &fileName)
Load a saved Polimago regression predictor from a file.
Definition regression_predictor.hpp:61
T move(T... args)
Namespace for the Polimago package.
Definition classification_predictor.hpp:38
std::shared_ptr< RegressionPredictor > RegressionPredictorPtr
Convenience shared pointer for RegressionPredictor.
Definition regression_predictor.hpp:127
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