diff --git a/modules/dnn/include/opencv2/dnn/dict.hpp b/modules/dnn/include/opencv2/dnn/dict.hpp index c98781ced..e48dcb201 100644 --- a/modules/dnn/include/opencv2/dnn/dict.hpp +++ b/modules/dnn/include/opencv2/dnn/dict.hpp @@ -11,41 +11,41 @@ namespace dnn struct DictValue { - int type; - - union - { - int64 i; - double d; - bool b; - String *s; - }; - DictValue(const DictValue &r); - DictValue(int p = 0) : type(cv::Param::INT), i(p) {} - DictValue(unsigned p) : type(cv::Param::INT), i(p) {} - DictValue(double p) : type(cv::Param::REAL), d(p) {} - DictValue(bool p) : type(cv::Param::BOOLEAN), b(p) {} - DictValue(const String &p) : type(cv::Param::STRING), s(new String(p)) {} - DictValue(const char *str) : type(cv::Param::STRING), s(new String(str)) {} + DictValue(int p = 0) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = p; } + DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer) { (*pi)[0] = p; } + DictValue(double p) : type(Param::REAL), pd(new AutoBuffer) { (*pd)[0] = p; } + DictValue(const String &p) : type(Param::STRING), ps(new AutoBuffer) { (*ps)[0] = p; } template - T get() const; + T get(int idx = -1) const; + + int size() const; - bool isString() const; bool isInt() const; + bool isString() const; + bool isReal() const; DictValue &operator=(const DictValue &r); ~DictValue(); -private: +protected: + + int type; + + union + { + AutoBuffer *pi; + AutoBuffer *pd; + AutoBuffer *ps; + }; + void release(); }; class CV_EXPORTS Dict { - //TODO: maybe this mechanism was realized somewhere in OpenCV? typedef std::map _Dict; _Dict dict; @@ -94,92 +94,89 @@ public: return value; } - - inline void print() - { - for (_Dict::const_iterator i = dict.begin(); i != dict.end(); i++) - { - std::cout << i->first << std::endl; - } - } }; +template<> +inline DictValue DictValue::get(int idx) const +{ + CV_Assert(idx == -1); + return *this; +} template<> -inline int DictValue::get() const +inline int64 DictValue::get(int idx) const { - CV_Assert(type == cv::Param::INT); - return (int)i; + CV_Assert(type == Param::INT); + CV_Assert(idx == -1 && pi->size() == 1 || idx >= 0 && idx < (int)pi->size()); + return (*pi)[(idx == -1) ? 0 : idx]; } template<> -inline unsigned DictValue::get() const +inline int DictValue::get(int idx) const { - CV_Assert(type == cv::Param::INT); - return (unsigned)i; + return (int)get(idx); } template<> -inline double DictValue::get() const +inline unsigned DictValue::get(int idx) const { - if (type == cv::Param::REAL) - return d; - else if (type == cv::Param::INT) - return (double)i; - else - { - CV_Assert(type == cv::Param::REAL || type == cv::Param::INT); - return 0; - } + return (unsigned)get(idx); } template<> -inline float DictValue::get() const +inline bool DictValue::get(int idx) const { - if (type == cv::Param::FLOAT) - return (float)d; - else if (type == cv::Param::INT) - return (float)i; - else - { - CV_Assert(type == cv::Param::FLOAT || type == cv::Param::INT); - return (float)0; - } + return (get(idx) != 0); } template<> -inline bool DictValue::get() const +inline double DictValue::get(int idx) const { - if (type == cv::Param::BOOLEAN) + if (type == Param::REAL) { - return b; + CV_Assert(idx == -1 && pd->size() == 1 || idx >= 0 && idx < (int)pd->size()); + return (*pd)[0]; } - else if (type == cv::Param::INT) + else if (type == Param::INT) { - return i != 0; + CV_Assert(idx == -1 && pi->size() == 1 || idx >= 0 && idx < (int)pi->size()); + return (double)(*pi)[0];; } else { - CV_Assert(type == cv::Param::BOOLEAN || type == cv::Param::INT); + CV_Assert(type == Param::REAL || type == Param::INT); return 0; } } template<> -inline String DictValue::get() const +inline float DictValue::get(int idx) const { - CV_Assert(type == cv::Param::STRING); - return *s; + return (float)get(idx); +} + +template<> +inline String DictValue::get(int idx) const +{ + CV_Assert(type == Param::STRING); + CV_Assert(idx == -1 && ps->size() == 1 || idx >= 0 && idx < (int)ps->size()); + return (*ps)[(idx == -1) ? 0 : idx]; } inline void DictValue::release() { - if (type == cv::Param::STRING && s != NULL) + switch (type) { - delete s; - s = NULL; + case Param::INT: + delete pi; + break; + case Param::STRING: + delete ps; + break; + case Param::REAL: + delete pd; + break; } - } inline DictValue::~DictValue() @@ -192,20 +189,27 @@ inline DictValue & DictValue::operator=(const DictValue &r) if (&r == this) return *this; - if (r.type == cv::Param::STRING) + if (r.type == Param::INT) + { + AutoBuffer *tmp = new AutoBuffer(*r.pi); + release(); + pi = tmp; + } + else if (r.type == Param::STRING) { - String *_s = new String(*r.s); + AutoBuffer *tmp = new AutoBuffer(*r.ps); release(); - s = _s; - type = r.type; + ps = tmp; } - else //flat structure + else if (r.type == Param::REAL) { - //how to copy anonymous union without memcpy? - for (size_t i = 0; i < sizeof(*this); i++) - ((uchar*)this)[i] = ((uchar*)&r)[i]; + AutoBuffer *tmp = new AutoBuffer(*r.pd); + release(); + pd = tmp; } + type = r.type; + return *this; } @@ -216,12 +220,35 @@ inline DictValue::DictValue(const DictValue &r) inline bool DictValue::isString() const { - return (type == cv::Param::STRING); + return (type == Param::STRING); } inline bool DictValue::isInt() const { - return (type == cv::Param::INT); + return (type == Param::INT); +} + +bool DictValue::isReal() const +{ + return (type == Param::REAL); +} + +int DictValue::size() const +{ + switch (type) + { + case Param::INT: + return (int)pi->size(); + break; + case Param::STRING: + return (int)ps->size(); + break; + case Param::REAL: + return (int)pd->size(); + break; + default: + return -1; + } } } diff --git a/modules/dnn/src/blob.cpp b/modules/dnn/src/blob.cpp index e16f3b73f..b23ec2863 100644 --- a/modules/dnn/src/blob.cpp +++ b/modules/dnn/src/blob.cpp @@ -87,7 +87,7 @@ namespace dnn m.create(dstShape.dims(), dstShape.ptr(), CV_32F); std::vector wrapBuf(dstShape[-3]); - int elemSize = m.elemSize(); + int elemSize = (int)m.elemSize(); uchar *ptr = this->ptrRaw(); for (size_t i = 0; i < inMats.size(); i++) { diff --git a/modules/dnn/src/caffe_importer.cpp b/modules/dnn/src/caffe_importer.cpp index 1b4548de7..16d269e56 100644 --- a/modules/dnn/src/caffe_importer.cpp +++ b/modules/dnn/src/caffe_importer.cpp @@ -138,7 +138,7 @@ namespace BlobShape shape(_shape.dim_size()); for (int i = 0; i < _shape.dim_size(); i++) - shape[i] = _shape.dim(i); + shape[i] = (int)_shape.dim(i); return shape; } @@ -192,6 +192,9 @@ namespace const std::string &name; int layerId, outNum; + + private: + void operator=(const BlobNote&) {} //supress warning }; void populateNet(Net dstNet) diff --git a/modules/dnn/src/layers/fully_connected_layer.cpp b/modules/dnn/src/layers/fully_connected_layer.cpp index 527f918ad..15e4c08a6 100644 --- a/modules/dnn/src/layers/fully_connected_layer.cpp +++ b/modules/dnn/src/layers/fully_connected_layer.cpp @@ -76,7 +76,7 @@ namespace dnn { for (size_t i = 0; i < inputs.size(); i++) { - int M = inputs[i]->total(0, axis); + int M = (int)inputs[i]->total(0, axis); int N = numOutputs; int K = innerSize;