3#include "../_cexports/c_sample_database.h"
5#include "../global.hpp"
7#include "../string.hpp"
8#include "../point_2d.hpp"
21namespace SampleDatabase
27size_t GetFloatRegressionDimension (CExports::TSIL hSil)
29 auto labelType = CVB_CALL_CAPI (SilGetLabelType(hSil));
31 CVB_CALL_CAPI_CHECKED (SilGetFloatVectorLabelDim(labelType, dim));
35inline void ValidateLabel (CExports::TSIL ,
const String &label)
45 if (label.size () != GetFloatRegressionDimension (hSil))
52inline 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()));
62 ValidateLabel (hSil, label);
65 CVB_CALL_CAPI_CHECKED(SilChangeVectorLabel(hSil, index,
const_cast<float*
>(label.data()), label.size()));
69template<
typename TLabelOut,
typename TLabelIn>
70using LabelTransform =
std::function<TLabelOut (
const TLabelIn &labelIn)>;
76template <
class TLabelInformation>
81 : shandleSil_(sguardSil)
93 using LabelType =
typename TLabelInformation::LabelType;
103 return CVB_CALL_CAPI(SilGetNumLabelEntries(shandleSil_.Handle()));
115 return GetInfo (index);
126 auto count =
Count ();
128 for (
decltype(count) i = 0; i < count; ++i)
144 CVB_CALL_CAPI_CHECKED(SilDeleteLabel(shandleSil_.Handle(), index));
155 auto index = LabelToIndex(label);
189 if (fromIndex == toIndex)
193 Private::Relabel (shandleSil_.Handle (), fromIndex,
ReadInfo (toIndex)->Label());
207 auto fromIndex = LabelToIndex (fromLabel);
208 auto toIndex = LabelToIndex (toLabel);
209 if (fromIndex < 0 || toIndex < 0)
221 int LabelToIndex (
const LabelType &label)
const
223 auto count =
Count ();
224 for (
int i = 0; i < count; ++i)
235 SharedReleaseObjectGuard shandleSil_;
241template <
class TData>
246 : shandleSil_(sguardSil)
264 auto label = CVB_CALL_CAPI(SilGetLabel (shandleSil_.Handle(), GetLabelIndex()));
265 return CVB_CALL_CAPI(SilGetNumDataReferences(label));
277 auto data = CVB_CALL_CAPI(SilGetDataByLabel (shandleSil_.Handle(), GetLabelIndex(), index));
278 return GetData (data);
291 CVB_CALL_CAPI_CHECKED(SilDeleteDataByLabel(shandleSil_.Handle(), GetLabelIndex(), index));
298 return Internal::DoBoolCallObjectOut<Image>([&](
void* & resimg)
300 CVB_CALL_CAPI_CHECKED(SilGetImageData (dataHandle, resimg));
301 return CVB_CALL_CAPI(ShareObject (resimg));
305 int GetLabelIndex ()
const
310 virtual TData GetData (CExports::TSILDATA dataHandle)
const = 0;
313 SharedReleaseObjectGuard shandleSil_;
376 : shandleSil_(std::move(guardSil)),
384 static ReleaseObjectGuard LoadInternal (
const String & fileName, CExports::TSilLabelType expectedLabelType)
386 CExports::TSIL sil =
nullptr;
388 CVB_CALL_CAPI_CHECKED (SilLoadTyped (fileName.c_str(), sil));
389 ReleaseObjectGuard silGuard (sil);
391 auto dataType = CVB_CALL_CAPI (SilGetDataTypeEnum(silGuard.Handle()));
392 auto labelType = CVB_CALL_CAPI (SilGetLabelTypeEnum(silGuard.Handle()));
393 if (dataType != CExports::SDT_ImageData)
397 if (labelType != expectedLabelType)
415 return shandleSil_.Handle();
435 size_t bufferSize = 0;
436 CVB_CALL_CAPI_CHECKED (SilGetCommentTyped(
Handle(),
static_cast<Char*
>(
nullptr), bufferSize));
438 CVB_CALL_CAPI_CHECKED (SilGetCommentTyped(
Handle(), comment.data(), bufferSize));
439 return String (comment.begin(), comment.end());
449 CVB_CALL_CAPI_CHECKED (SilSetCommentTyped (
Handle(), comment.c_str()));
459 CExports::CV_SYSTEMTIME st = { 0 };
460 CVB_CALL_CAPI_CHECKED (SilGetCreationDate(
Handle(), st));
462 tm.tm_sec = st.wSecond;
463 tm.tm_min = st.wMinute;
464 tm.tm_hour = st.wHour;
465 tm.tm_mday = st.wDay;
466 tm.tm_mon = st.wMonth - 1;
467 tm.tm_year = st.wYear - 1900;
480 CExports::CV_SYSTEMTIME st = { 0 };
481 CVB_CALL_CAPI_CHECKED (SilGetModificationDate(
Handle(), st));
483 tm.tm_sec = st.wSecond;
484 tm.tm_min = st.wMinute;
485 tm.tm_hour = st.wHour;
486 tm.tm_mday = st.wDay;
487 tm.tm_mon = st.wMonth - 1;
488 tm.tm_year = st.wYear - 1900;
501 size_t numClasses = 0, dummy1 = 0, dummy2 = 0;
503 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), numClasses, dummy1, dummy2, dummy3));
504 return static_cast<int>(numClasses);
514 return static_cast<int>(CVB_CALL_CAPI(SilGetNumDataEntries(
Handle())));
524 size_t minCount = 0, dummy1 = 0, dummy2 = 0;
526 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), dummy1, minCount, dummy2, dummy3));
527 return static_cast<int>(minCount);
537 size_t maxCount = 0, dummy1 = 0, dummy2 = 0;
539 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), dummy1, dummy2, maxCount, dummy3));
540 return static_cast<int>(maxCount);
550 size_t dummy1 = 0, dummy2 = 0, dummy3 = 0;
552 CVB_CALL_CAPI_CHECKED(SilGetStatistics(
Handle(), dummy1, dummy2, dummy3, avgCount));
565 CVB_CALL_CAPI_CHECKED (SilStoreTyped (
Handle(), fileName.c_str()));
566 fileName_ = fileName;
571 template <
class TOut>
574 CExports::TSIL sil =
nullptr;
576 CVB_CALL_CAPI_CHECKED (SilTransformDataList (
Handle (), NativeDataTransformCallback, &cbk, sil));
577 ReleaseObjectGuard silGuard (sil);
579 return TOut::FromHandle (std::move (silGuard));
582 template <
class TOut,
class TLabelOut,
class TLabelIn>
585 CExports::TSIL sil =
nullptr;
587 CVB_CALL_CAPI_CHECKED (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, CExports::TSILDATA& DataOut)
600 auto imgIn = Internal::DoBoolCallObjectOut<Image>([&](
void* & resimg)
602 CVB_CALL_CAPI_CHECKED(SilGetImageData (DataIn, resimg));
603 return CVB_CALL_CAPI(ShareObject (resimg));
606 auto imgOut = (*cbk) (*imgIn);
607 DataOut = CVB_CALL_CAPI(SilCreateImageData(imgOut->Handle(), 0, 0, imgOut->Size().Width(), imgOut->Size().Height()));
616 template <
class TLabelOut,
class TLabelIn>
617 static CExports::cvbbool_t __stdcall NativeLabelTransformCallback (
void* pPrivate, CExports::TSILLABEL LabelIn, CExports::TSILLABEL& LabelOut)
621 Private::LabelTransform<TLabelOut, TLabelIn> *cbk =
reinterpret_cast<Private::LabelTransform<TLabelOut, TLabelIn>*
> (pPrivate);
624 NativeLabelToObject (LabelIn, objectIn);
626 auto objectOut = (*cbk) (objectIn);
627 LabelOut = CreateNativeLabel (objectOut);
636 static void NativeLabelToObject (CExports::TSILLABEL label, String &
object)
638 size_t bufferSize = 0;
639 CVB_CALL_CAPI_CHECKED (SilGetStringLabelTyped(label,
static_cast<Char*
>(
nullptr), bufferSize));
641 CVB_CALL_CAPI_CHECKED (SilGetStringLabelTyped(label, stringLabel.data(), bufferSize));
642 object.assign (stringLabel.begin (), stringLabel.end ());
645 static CExports::TSILLABEL CreateNativeLabel (
const String &
object)
647 return CVB_CALL_CAPI(SilCreateStringLabelTyped(
object.c_str()));
650 static void NativeLabelToObject (CExports::TSILLABEL label,
std::vector<float> &
object)
653 CVB_CALL_CAPI_CHECKED (SilGetFloatVectorLabel(label,
static_cast<float*
>(
nullptr), dim));
655 CVB_CALL_CAPI_CHECKED (SilGetFloatVectorLabel(label,
object.data(), dim));
660 return CVB_CALL_CAPI(SilCreateVectorLabel(
object.data(),
object.size()));
664 SharedReleaseObjectGuard shandleSil_;
The Common Vision Blox image.
Definition: decl_image.hpp:45
Information collection for class labels.
Definition: sample_list.hpp:78
void MergeClasses(const LabelType &fromLabel, const LabelType &toLabel)
Merge two classes into one class.
Definition: sample_list.hpp:205
std::unique_ptr< TLabelInformation > ReadInfo(int index) const
Retrieves the indexed item.
Definition: sample_list.hpp:113
typename TLabelInformation::LabelType LabelType
Type of the labels.
Definition: sample_list.hpp:93
void Remove(const LabelType &label)
Remove a class and all the data objects associated with it based on its name.
Definition: sample_list.hpp:153
int Count() const
Retrieves the number of elements in the collection.
Definition: sample_list.hpp:101
void Clear()
Remove all labels (and their associated data) from the sample list.
Definition: sample_list.hpp:170
void MergeClasses(int fromIndex, int toIndex)
Merge two classes into one class.
Definition: sample_list.hpp:187
void RemoveAt(int index)
Remove a class by index.
Definition: sample_list.hpp:142
std::vector< std::shared_ptr< TLabelInformation > > ReadInfos() const
Retrieves all the items stored in the collection.
Definition: sample_list.hpp:124
Collection of data samples.
Definition: sample_list.hpp:243
TData RemoveAt(int index)
Remove a sample.
Definition: sample_list.hpp:288
int Count() const
Get the number of samples for this collection.
Definition: sample_list.hpp:262
TData ReadData(int index) const
Access one of the sample images.
Definition: sample_list.hpp:275
Base class for sample lists.
Definition: sample_list.hpp:364
int NumClasses() const
Number of distinguishable classes in the sample list.
Definition: sample_list.hpp:499
std::chrono::system_clock::time_point CreationDate() const
Date on which the sample list was created.
Definition: sample_list.hpp:457
String Comment() const
Get the comment text.
Definition: sample_list.hpp:433
void Save(const String &fileName) const
Save the sample list to a file.
Definition: sample_list.hpp:563
double AverageSampleCount() const
Average number samples in per class of the sample list.
Definition: sample_list.hpp:548
void SetComment(String comment)
Set the comment text.
Definition: sample_list.hpp:447
std::chrono::system_clock::time_point ModificationDate() const
Date on which the sample list was modified.
Definition: sample_list.hpp:478
int MaxSampleCount() const
Number samples in the largest class of the sample list.
Definition: sample_list.hpp:535
int NumSamples() const
Total number of samples in the sample list.
Definition: sample_list.hpp:512
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:423
int MinSampleCount() const
Number samples in the smallest class of the sample list.
Definition: sample_list.hpp:522
void * Handle() const noexcept
Classic API SIL handle.
Definition: sample_list.hpp:413
@ Private
Private name space.
std::function< std::vector< float >(const std::vector< float > &labelIn)> LabelTransformVectorToVector
Callback for label transformation.
Definition: sample_list.hpp:338
std::function< String(const String &labelIn)> LabelTransformStringToString
Callback for label transformation.
Definition: sample_list.hpp:322
std::function< String(const std::vector< float > &labelIn)> LabelTransformVectorToString
Callback for label transformation.
Definition: sample_list.hpp:346
std::function< std::vector< float >(const String &labelIn)> LabelTransformStringToVector
Callback for label transformation.
Definition: sample_list.hpp:330
std::function< std::unique_ptr< Image >(const Image &imgIn)> DataTransformImageToImage
Image data transformation callback.
Definition: sample_list.hpp:354
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:70
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:56