dnn.inl.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #ifndef OPENCV_DNN_DNN_INL_HPP
  42. #define OPENCV_DNN_DNN_INL_HPP
  43. #include <opencv2/dnn.hpp>
  44. namespace cv {
  45. namespace dnn {
  46. CV__DNN_EXPERIMENTAL_NS_BEGIN
  47. template<typename TypeIter>
  48. DictValue DictValue::arrayInt(TypeIter begin, int size)
  49. {
  50. DictValue res(Param::INT, new AutoBuffer<int64, 1>(size));
  51. for (int j = 0; j < size; begin++, j++)
  52. (*res.pi)[j] = *begin;
  53. return res;
  54. }
  55. template<typename TypeIter>
  56. DictValue DictValue::arrayReal(TypeIter begin, int size)
  57. {
  58. DictValue res(Param::REAL, new AutoBuffer<double, 1>(size));
  59. for (int j = 0; j < size; begin++, j++)
  60. (*res.pd)[j] = *begin;
  61. return res;
  62. }
  63. template<typename TypeIter>
  64. DictValue DictValue::arrayString(TypeIter begin, int size)
  65. {
  66. DictValue res(Param::STRING, new AutoBuffer<String, 1>(size));
  67. for (int j = 0; j < size; begin++, j++)
  68. (*res.ps)[j] = *begin;
  69. return res;
  70. }
  71. template<>
  72. inline DictValue DictValue::get<DictValue>(int idx) const
  73. {
  74. CV_Assert(idx == -1);
  75. return *this;
  76. }
  77. template<>
  78. inline int64 DictValue::get<int64>(int idx) const
  79. {
  80. CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
  81. idx = (idx == -1) ? 0 : idx;
  82. if (type == Param::INT)
  83. {
  84. return (*pi)[idx];
  85. }
  86. else if (type == Param::REAL)
  87. {
  88. double doubleValue = (*pd)[idx];
  89. double fracpart, intpart;
  90. fracpart = std::modf(doubleValue, &intpart);
  91. CV_Assert(fracpart == 0.0);
  92. return (int64)doubleValue;
  93. }
  94. else if (type == Param::STRING)
  95. {
  96. return std::atoi((*ps)[idx].c_str());
  97. }
  98. else
  99. {
  100. CV_Assert(isInt() || isReal() || isString());
  101. return 0;
  102. }
  103. }
  104. template<>
  105. inline int DictValue::get<int>(int idx) const
  106. {
  107. return (int)get<int64>(idx);
  108. }
  109. inline int DictValue::getIntValue(int idx) const
  110. {
  111. return (int)get<int64>(idx);
  112. }
  113. template<>
  114. inline unsigned DictValue::get<unsigned>(int idx) const
  115. {
  116. return (unsigned)get<int64>(idx);
  117. }
  118. template<>
  119. inline bool DictValue::get<bool>(int idx) const
  120. {
  121. return (get<int64>(idx) != 0);
  122. }
  123. template<>
  124. inline double DictValue::get<double>(int idx) const
  125. {
  126. CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
  127. idx = (idx == -1) ? 0 : idx;
  128. if (type == Param::REAL)
  129. {
  130. return (*pd)[idx];
  131. }
  132. else if (type == Param::INT)
  133. {
  134. return (double)(*pi)[idx];
  135. }
  136. else if (type == Param::STRING)
  137. {
  138. return std::atof((*ps)[idx].c_str());
  139. }
  140. else
  141. {
  142. CV_Assert(isReal() || isInt() || isString());
  143. return 0;
  144. }
  145. }
  146. inline double DictValue::getRealValue(int idx) const
  147. {
  148. return get<double>(idx);
  149. }
  150. template<>
  151. inline float DictValue::get<float>(int idx) const
  152. {
  153. return (float)get<double>(idx);
  154. }
  155. template<>
  156. inline String DictValue::get<String>(int idx) const
  157. {
  158. CV_Assert(isString());
  159. CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size()));
  160. return (*ps)[(idx == -1) ? 0 : idx];
  161. }
  162. inline String DictValue::getStringValue(int idx) const
  163. {
  164. return get<String>(idx);
  165. }
  166. inline void DictValue::release()
  167. {
  168. switch (type)
  169. {
  170. case Param::INT:
  171. delete pi;
  172. break;
  173. case Param::STRING:
  174. delete ps;
  175. break;
  176. case Param::REAL:
  177. delete pd;
  178. break;
  179. }
  180. }
  181. inline DictValue::~DictValue()
  182. {
  183. release();
  184. }
  185. inline DictValue & DictValue::operator=(const DictValue &r)
  186. {
  187. if (&r == this)
  188. return *this;
  189. if (r.type == Param::INT)
  190. {
  191. AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
  192. release();
  193. pi = tmp;
  194. }
  195. else if (r.type == Param::STRING)
  196. {
  197. AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
  198. release();
  199. ps = tmp;
  200. }
  201. else if (r.type == Param::REAL)
  202. {
  203. AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
  204. release();
  205. pd = tmp;
  206. }
  207. type = r.type;
  208. return *this;
  209. }
  210. inline DictValue::DictValue(const DictValue &r)
  211. {
  212. type = r.type;
  213. if (r.type == Param::INT)
  214. pi = new AutoBuffer<int64, 1>(*r.pi);
  215. else if (r.type == Param::STRING)
  216. ps = new AutoBuffer<String, 1>(*r.ps);
  217. else if (r.type == Param::REAL)
  218. pd = new AutoBuffer<double, 1>(*r.pd);
  219. }
  220. inline bool DictValue::isString() const
  221. {
  222. return (type == Param::STRING);
  223. }
  224. inline bool DictValue::isInt() const
  225. {
  226. return (type == Param::INT);
  227. }
  228. inline bool DictValue::isReal() const
  229. {
  230. return (type == Param::REAL || type == Param::INT);
  231. }
  232. inline int DictValue::size() const
  233. {
  234. switch (type)
  235. {
  236. case Param::INT:
  237. return (int)pi->size();
  238. case Param::STRING:
  239. return (int)ps->size();
  240. case Param::REAL:
  241. return (int)pd->size();
  242. }
  243. #ifdef __OPENCV_BUILD
  244. CV_Error(Error::StsInternal, "");
  245. #else
  246. CV_ErrorNoReturn(Error::StsInternal, "");
  247. #endif
  248. }
  249. inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
  250. {
  251. int i;
  252. if (dictv.isInt())
  253. {
  254. for (i = 0; i < dictv.size() - 1; i++)
  255. stream << dictv.get<int64>(i) << ", ";
  256. stream << dictv.get<int64>(i);
  257. }
  258. else if (dictv.isReal())
  259. {
  260. for (i = 0; i < dictv.size() - 1; i++)
  261. stream << dictv.get<double>(i) << ", ";
  262. stream << dictv.get<double>(i);
  263. }
  264. else if (dictv.isString())
  265. {
  266. for (i = 0; i < dictv.size() - 1; i++)
  267. stream << "\"" << dictv.get<String>(i) << "\", ";
  268. stream << dictv.get<String>(i);
  269. }
  270. return stream;
  271. }
  272. /////////////////////////////////////////////////////////////////
  273. inline bool Dict::has(const String &key) const
  274. {
  275. return dict.count(key) != 0;
  276. }
  277. inline DictValue *Dict::ptr(const String &key)
  278. {
  279. _Dict::iterator i = dict.find(key);
  280. return (i == dict.end()) ? NULL : &i->second;
  281. }
  282. inline const DictValue *Dict::ptr(const String &key) const
  283. {
  284. _Dict::const_iterator i = dict.find(key);
  285. return (i == dict.end()) ? NULL : &i->second;
  286. }
  287. inline const DictValue &Dict::get(const String &key) const
  288. {
  289. _Dict::const_iterator i = dict.find(key);
  290. if (i == dict.end())
  291. CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
  292. return i->second;
  293. }
  294. template <typename T>
  295. inline T Dict::get(const String &key) const
  296. {
  297. return this->get(key).get<T>();
  298. }
  299. template <typename T>
  300. inline T Dict::get(const String &key, const T &defaultValue) const
  301. {
  302. _Dict::const_iterator i = dict.find(key);
  303. if (i != dict.end())
  304. return i->second.get<T>();
  305. else
  306. return defaultValue;
  307. }
  308. template<typename T>
  309. inline const T &Dict::set(const String &key, const T &value)
  310. {
  311. _Dict::iterator i = dict.find(key);
  312. if (i != dict.end())
  313. i->second = DictValue(value);
  314. else
  315. dict.insert(std::make_pair(key, DictValue(value)));
  316. return value;
  317. }
  318. inline void Dict::erase(const String &key)
  319. {
  320. dict.erase(key);
  321. }
  322. inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
  323. {
  324. Dict::_Dict::const_iterator it;
  325. for (it = dict.dict.begin(); it != dict.dict.end(); it++)
  326. stream << it->first << " : " << it->second << "\n";
  327. return stream;
  328. }
  329. inline std::map<String, DictValue>::const_iterator Dict::begin() const
  330. {
  331. return dict.begin();
  332. }
  333. inline std::map<String, DictValue>::const_iterator Dict::end() const
  334. {
  335. return dict.end();
  336. }
  337. CV__DNN_EXPERIMENTAL_NS_END
  338. }
  339. }
  340. #endif