3#include "../_cexports/c_sample_database.h"
5#include "../global.hpp"
7#include "../string.hpp"
8#include "../point_2d.hpp"
27 size_t GetFloatRegressionDimension(CExports::TSIL hSil)
29 auto labelType = CVB_CALL_CAPI(SilGetLabelType(hSil));
31 CVB_CALL_CAPI_CHECKED(SilGetFloatVectorLabelDim(labelType, dim));
35 inline void ValidateLabel(CExports::TSIL ,
const String &label)
39 throw std::invalid_argument(
"empty string is not a valid label");
43 inline void ValidateLabel(CExports::TSIL hSil,
const std::vector<float> &label)
45 if (label.size() != GetFloatRegressionDimension(hSil))
47 throw std::invalid_argument(
"invalid dimension of the float vector label");
52 inline void Relabel(CExports::TSIL hSil,
int index,
const String &label)
54 ValidateLabel(hSil, label);
56 CVB_CALL_CAPI_CHECKED(SilChangeStringLabelTyped(hSil, index, label.c_str()));
60 inline void Relabel(CExports::TSIL hSil,
int index,
const std::vector<float> &label)
62 ValidateLabel(hSil, label);
65 CVB_CALL_CAPI_CHECKED(SilChangeVectorLabel(hSil, index,
const_cast<float *
>(label.data()),
70 template <
typename TLabelOut,
typename TLabelIn>
71 using LabelTransform = std::function<TLabelOut(
const TLabelIn &labelIn)>;
77 template <
class TLabelInformation>
78 class LabelInfoCollection
81 explicit LabelInfoCollection(
const SharedReleaseObjectGuard &sguardSil)
82 : shandleSil_(sguardSil)
86 LabelInfoCollection(
const LabelInfoCollection &other) =
delete;
87 LabelInfoCollection &operator=(
const LabelInfoCollection &other) =
delete;
88 LabelInfoCollection(LabelInfoCollection &&other) =
delete;
89 LabelInfoCollection &operator=(LabelInfoCollection &&other) =
delete;
91 virtual ~LabelInfoCollection() =
default;
95 using LabelType =
typename TLabelInformation::LabelType;
105 return CVB_CALL_CAPI(SilGetNumLabelEntries(shandleSil_.Handle()));
117 return GetInfo(index);
128 auto count =
Count();
130 for (
decltype(count) i = 0; i < count; ++i)
146 CVB_CALL_CAPI_CHECKED(SilDeleteLabel(shandleSil_.Handle(), index));
157 auto index = LabelToIndex(label);
191 if (fromIndex == toIndex)
195 Private::Relabel(shandleSil_.Handle(), fromIndex,
ReadInfo(toIndex)->Label());
209 auto fromIndex = LabelToIndex(fromLabel);
210 auto toIndex = LabelToIndex(toLabel);
211 if (fromIndex < 0 || toIndex < 0)
222 int LabelToIndex(
const LabelType &label)
const
224 auto count =
Count();
225 for (
int i = 0; i < count; ++i)
236 SharedReleaseObjectGuard shandleSil_;
78 class LabelInfoCollection {
…};
242 template <
class TData>
243 class SampleCollection
246 explicit SampleCollection(
const SharedReleaseObjectGuard &sguardSil)
247 : shandleSil_(sguardSil)
251 SampleCollection(
const SampleCollection &other) =
delete;
252 SampleCollection &operator=(
const SampleCollection &other) =
delete;
253 SampleCollection(SampleCollection &&other) =
delete;
254 SampleCollection &operator=(SampleCollection &&other) =
delete;
255 virtual ~SampleCollection() =
default;
266 auto label = CVB_CALL_CAPI(SilGetLabel(shandleSil_.Handle(), GetLabelIndex()));
267 return CVB_CALL_CAPI(SilGetNumDataReferences(label));
279 auto data = CVB_CALL_CAPI(SilGetDataByLabel(shandleSil_.Handle(), GetLabelIndex(), index));
280 return GetData(data);
293 CVB_CALL_CAPI_CHECKED(SilDeleteDataByLabel(shandleSil_.Handle(), GetLabelIndex(), index));
300 return Internal::DoBoolCallObjectOut<Image>([&](
void *&resimg) {
301 CVB_CALL_CAPI_CHECKED(SilGetImageData(dataHandle, resimg));
306 int GetLabelIndex()
const
311 virtual TData GetData(CExports::TSILDATA dataHandle)
const = 0;
314 SharedReleaseObjectGuard shandleSil_;
243 class SampleCollection {
…};
368 SampleList(
const SampleList &other) =
delete;
369 SampleList &operator=(
const SampleList &other) =
delete;
370 SampleList(SampleList &&other) =
delete;
371 SampleList &operator=(SampleList &&other) =
delete;
372 virtual ~SampleList() =
default;
375 explicit SampleList(ReleaseObjectGuard &&guardSil,
const String &fileName =
String())
377 , fileName_(fileName)
382 static ReleaseObjectGuard LoadInternal(
const String &fileName, CExports::TSilLabelType expectedLabelType)
384 CExports::TSIL sil =
nullptr;
386 CVB_CALL_CAPI_CHECKED(SilLoadTyped(fileName.c_str(), sil));
387 ReleaseObjectGuard silGuard(sil);
389 auto dataType = CVB_CALL_CAPI(SilGetDataTypeEnum(silGuard.Handle()));
390 auto labelType = CVB_CALL_CAPI(SilGetLabelTypeEnum(silGuard.Handle()));
391 if (dataType != CExports::SDT_ImageData)
395 if (labelType != expectedLabelType)
413 return shandleSil_.Handle();
434 size_t bufferSize = 0;
435 CVB_CALL_CAPI_CHECKED(SilGetCommentTyped(
Handle(),
static_cast<Char *
>(
nullptr), bufferSize));
437 CVB_CALL_CAPI_CHECKED(SilGetCommentTyped(
Handle(), comment.data(), bufferSize));
438 return String(comment.begin(), comment.end());
448 CVB_CALL_CAPI_CHECKED(SilSetCommentTyped(
Handle(), comment.c_str()));
458 CExports::CV_SYSTEMTIME st = {0};
459 CVB_CALL_CAPI_CHECKED(SilGetCreationDate(
Handle(), st));
461 tm.tm_sec = st.wSecond;
462 tm.tm_min = st.wMinute;
463 tm.tm_hour = st.wHour;
464 tm.tm_mday = st.wDay;
465 tm.tm_mon = st.wMonth - 1;
466 tm.tm_year = st.wYear - 1900;
479 CExports::CV_SYSTEMTIME st = {0};
480 CVB_CALL_CAPI_CHECKED(SilGetModificationDate(
Handle(), st));
482 tm.tm_sec = st.wSecond;
483 tm.tm_min = st.wMinute;
484 tm.tm_hour = st.wHour;
485 tm.tm_mday = st.wDay;
486 tm.tm_mon = st.wMonth - 1;
487 tm.tm_year = st.wYear - 1900;
500 size_t numClasses = 0, dummy1 = 0, dummy2 = 0;
502 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), numClasses, dummy1, dummy2, dummy3));
503 return static_cast<int>(numClasses);
513 return static_cast<int>(CVB_CALL_CAPI(SilGetNumDataEntries(
Handle())));
523 size_t minCount = 0, dummy1 = 0, dummy2 = 0;
525 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), dummy1, minCount, dummy2, dummy3));
526 return static_cast<int>(minCount);
536 size_t maxCount = 0, dummy1 = 0, dummy2 = 0;
538 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), dummy1, dummy2, maxCount, dummy3));
539 return static_cast<int>(maxCount);
549 size_t dummy1 = 0, dummy2 = 0, dummy3 = 0;
551 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), dummy1, dummy2, dummy3, avgCount));
564 CVB_CALL_CAPI_CHECKED(SilStoreTyped(
Handle(), fileName.c_str()));
565 fileName_ = fileName;
570 template <
class TOut>
573 CExports::TSIL sil =
nullptr;
575 CVB_CALL_CAPI_CHECKED(SilTransformDataList(
Handle(), NativeDataTransformCallback, &cbk, sil));
576 ReleaseObjectGuard silGuard(sil);
578 return TOut::FromHandle(
std::move(silGuard));
581 template <
class TOut,
class TLabelOut,
class TLabelIn>
584 CExports::TSIL sil =
nullptr;
586 CVB_CALL_CAPI_CHECKED(
587 SilTransformLabelList(
Handle(), NativeLabelTransformCallback<TLabelOut, TLabelIn>, &cbk, sil));
588 ReleaseObjectGuard silGuard(sil);
590 return TOut::FromHandle(
std::move(silGuard));
594 static CExports::cvbbool_t __stdcall NativeDataTransformCallback(
void *pPrivate, CExports::TSILDATA DataIn,
595 CExports::TSILDATA &DataOut)
601 auto imgIn = Internal::DoBoolCallObjectOut<Image>([&](
void *&resimg) {
602 CVB_CALL_CAPI_CHECKED(SilGetImageData(DataIn, resimg));
606 auto imgOut = (*cbk)(*imgIn);
607 DataOut = CVB_CALL_CAPI(
608 SilCreateImageData(imgOut->Handle(), 0, 0, imgOut->Size().Width(), imgOut->Size().Height()));
611 catch (
const std::exception &)
617 template <
class TLabelOut,
class TLabelIn>
618 static CExports::cvbbool_t __stdcall NativeLabelTransformCallback(
void *pPrivate, CExports::TSILLABEL LabelIn,
619 CExports::TSILLABEL &LabelOut)
623 Private::LabelTransform<TLabelOut, TLabelIn> *cbk =
624 reinterpret_cast<Private::LabelTransform<TLabelOut, TLabelIn> *
>(pPrivate);
627 NativeLabelToObject(LabelIn, objectIn);
629 auto objectOut = (*cbk)(objectIn);
630 LabelOut = CreateNativeLabel(objectOut);
633 catch (
const std::exception &)
639 static void NativeLabelToObject(CExports::TSILLABEL label,
String &
object)
641 size_t bufferSize = 0;
642 CVB_CALL_CAPI_CHECKED(SilGetStringLabelTyped(label,
static_cast<Char *
>(
nullptr), bufferSize));
643 std::vector<Char> stringLabel(bufferSize);
644 CVB_CALL_CAPI_CHECKED(SilGetStringLabelTyped(label, stringLabel.data(), bufferSize));
645 object.assign(stringLabel.begin(), stringLabel.end());
648 static CExports::TSILLABEL CreateNativeLabel(
const String &
object)
650 return CVB_CALL_CAPI(SilCreateStringLabelTyped(
object.c_str()));
653 static void NativeLabelToObject(CExports::TSILLABEL label, std::vector<float> &
object)
656 CVB_CALL_CAPI_CHECKED(SilGetFloatVectorLabel(label,
static_cast<float *
>(
nullptr), dim));
658 CVB_CALL_CAPI_CHECKED(SilGetFloatVectorLabel(label,
object.data(), dim));
661 static CExports::TSILLABEL CreateNativeLabel(
const std::vector<float> &
object)
663 return CVB_CALL_CAPI(SilCreateVectorLabel(
object.data(),
object.size()));
667 SharedReleaseObjectGuard shandleSil_;
364 class SampleList {
…};
The Common Vision Blox image.
Definition decl_image.hpp:50
void MergeClasses(const LabelType &fromLabel, const LabelType &toLabel)
Merge two classes into one class.
Definition sample_list.hpp:207
std::unique_ptr< TLabelInformation > ReadInfo(int index) const
Retrieves the indexed item.
Definition sample_list.hpp:115
typename TLabelInformation::LabelType LabelType
Type of the labels.
Definition sample_list.hpp:95
void Remove(const LabelType &label)
Remove a class and all the data objects associated with it based on its name.
Definition sample_list.hpp:155
int Count() const
Retrieves the number of elements in the collection.
Definition sample_list.hpp:103
void Clear()
Remove all labels (and their associated data) from the sample list.
Definition sample_list.hpp:172
void MergeClasses(int fromIndex, int toIndex)
Merge two classes into one class.
Definition sample_list.hpp:189
void RemoveAt(int index)
Remove a class by index.
Definition sample_list.hpp:144
std::vector< std::shared_ptr< TLabelInformation > > ReadInfos() const
Retrieves all the items stored in the collection.
Definition sample_list.hpp:126
TData RemoveAt(int index)
Remove a sample.
Definition sample_list.hpp:290
int Count() const
Get the number of samples for this collection.
Definition sample_list.hpp:264
TData ReadData(int index) const
Access one of the sample images.
Definition sample_list.hpp:277
int NumClasses() const
Number of distinguishable classes in the sample list.
Definition sample_list.hpp:498
std::chrono::system_clock::time_point CreationDate() const
Date on which the sample list was created.
Definition sample_list.hpp:456
String Comment() const
Get the comment text.
Definition sample_list.hpp:432
void Save(const String &fileName) const
Save the sample list to a file.
Definition sample_list.hpp:562
double AverageSampleCount() const
Average number samples in per class of the sample list.
Definition sample_list.hpp:547
void SetComment(String comment)
Set the comment text.
Definition sample_list.hpp:446
std::chrono::system_clock::time_point ModificationDate() const
Date on which the sample list was modified.
Definition sample_list.hpp:477
int MaxSampleCount() const
Number samples in the largest class of the sample list.
Definition sample_list.hpp:534
int NumSamples() const
Total number of samples in the sample list.
Definition sample_list.hpp:511
String FileName() const
Name of the file from which this image list was loaded (empty string if this image list was neither l...
Definition sample_list.hpp:422
int MinSampleCount() const
Number samples in the smallest class of the sample list.
Definition sample_list.hpp:521
void * Handle() const noexcept
Classic API SIL handle.
Definition sample_list.hpp:411
cvbbool_t ShareObject(OBJ Object)
Namespace for the SampleDatabase package.
Definition decl_classification_sil.hpp:20
std::function< std::vector< float >(const std::vector< float > &labelIn)> LabelTransformVectorToVector
Callback for label transformation.
Definition sample_list.hpp:339
std::shared_ptr< SampleList > SampleListPtr
Convenience shared pointer for SampleList.
Definition sample_list.hpp:674
std::function< std::vector< float >(const String &labelIn)> LabelTransformStringToVector
Callback for label transformation.
Definition sample_list.hpp:331
std::function< String(const std::vector< float > &labelIn)> LabelTransformVectorToString
Callback for label transformation.
Definition sample_list.hpp:347
std::function< std::unique_ptr< Image >(const Image &imgIn)> DataTransformImageToImage
Image data transformation callback.
Definition sample_list.hpp:355
std::function< String(const String &labelIn)> LabelTransformStringToString
Callback for label transformation.
Definition sample_list.hpp:323
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
char Char
Character type for wide characters or unicode characters.
Definition string.hpp:63
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49