dnn.inl.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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
  95. {
  96. CV_Assert(isInt() || isReal());
  97. return 0;
  98. }
  99. }
  100. template<>
  101. inline int DictValue::get<int>(int idx) const
  102. {
  103. return (int)get<int64>(idx);
  104. }
  105. inline int DictValue::getIntValue(int idx) const
  106. {
  107. return (int)get<int64>(idx);
  108. }
  109. template<>
  110. inline unsigned DictValue::get<unsigned>(int idx) const
  111. {
  112. return (unsigned)get<int64>(idx);
  113. }
  114. template<>
  115. inline bool DictValue::get<bool>(int idx) const
  116. {
  117. return (get<int64>(idx) != 0);
  118. }
  119. template<>
  120. inline double DictValue::get<double>(int idx) const
  121. {
  122. CV_Assert((idx == -1 && size() == 1) || (idx >= 0 && idx < size()));
  123. idx = (idx == -1) ? 0 : idx;
  124. if (type == Param::REAL)
  125. {
  126. return (*pd)[idx];
  127. }
  128. else if (type == Param::INT)
  129. {
  130. return (double)(*pi)[idx];
  131. }
  132. else
  133. {
  134. CV_Assert(isReal() || isInt());
  135. return 0;
  136. }
  137. }
  138. inline double DictValue::getRealValue(int idx) const
  139. {
  140. return get<double>(idx);
  141. }
  142. template<>
  143. inline float DictValue::get<float>(int idx) const
  144. {
  145. return (float)get<double>(idx);
  146. }
  147. template<>
  148. inline String DictValue::get<String>(int idx) const
  149. {
  150. CV_Assert(isString());
  151. CV_Assert((idx == -1 && ps->size() == 1) || (idx >= 0 && idx < (int)ps->size()));
  152. return (*ps)[(idx == -1) ? 0 : idx];
  153. }
  154. inline String DictValue::getStringValue(int idx) const
  155. {
  156. return get<String>(idx);
  157. }
  158. inline void DictValue::release()
  159. {
  160. switch (type)
  161. {
  162. case Param::INT:
  163. delete pi;
  164. break;
  165. case Param::STRING:
  166. delete ps;
  167. break;
  168. case Param::REAL:
  169. delete pd;
  170. break;
  171. }
  172. }
  173. inline DictValue::~DictValue()
  174. {
  175. release();
  176. }
  177. inline DictValue & DictValue::operator=(const DictValue &r)
  178. {
  179. if (&r == this)
  180. return *this;
  181. if (r.type == Param::INT)
  182. {
  183. AutoBuffer<int64, 1> *tmp = new AutoBuffer<int64, 1>(*r.pi);
  184. release();
  185. pi = tmp;
  186. }
  187. else if (r.type == Param::STRING)
  188. {
  189. AutoBuffer<String, 1> *tmp = new AutoBuffer<String, 1>(*r.ps);
  190. release();
  191. ps = tmp;
  192. }
  193. else if (r.type == Param::REAL)
  194. {
  195. AutoBuffer<double, 1> *tmp = new AutoBuffer<double, 1>(*r.pd);
  196. release();
  197. pd = tmp;
  198. }
  199. type = r.type;
  200. return *this;
  201. }
  202. inline DictValue::DictValue(const DictValue &r)
  203. {
  204. type = r.type;
  205. if (r.type == Param::INT)
  206. pi = new AutoBuffer<int64, 1>(*r.pi);
  207. else if (r.type == Param::STRING)
  208. ps = new AutoBuffer<String, 1>(*r.ps);
  209. else if (r.type == Param::REAL)
  210. pd = new AutoBuffer<double, 1>(*r.pd);
  211. }
  212. inline bool DictValue::isString() const
  213. {
  214. return (type == Param::STRING);
  215. }
  216. inline bool DictValue::isInt() const
  217. {
  218. return (type == Param::INT);
  219. }
  220. inline bool DictValue::isReal() const
  221. {
  222. return (type == Param::REAL || type == Param::INT);
  223. }
  224. inline int DictValue::size() const
  225. {
  226. switch (type)
  227. {
  228. case Param::INT:
  229. return (int)pi->size();
  230. break;
  231. case Param::STRING:
  232. return (int)ps->size();
  233. break;
  234. case Param::REAL:
  235. return (int)pd->size();
  236. break;
  237. default:
  238. CV_Error(Error::StsInternal, "");
  239. return -1;
  240. }
  241. }
  242. inline std::ostream &operator<<(std::ostream &stream, const DictValue &dictv)
  243. {
  244. int i;
  245. if (dictv.isInt())
  246. {
  247. for (i = 0; i < dictv.size() - 1; i++)
  248. stream << dictv.get<int64>(i) << ", ";
  249. stream << dictv.get<int64>(i);
  250. }
  251. else if (dictv.isReal())
  252. {
  253. for (i = 0; i < dictv.size() - 1; i++)
  254. stream << dictv.get<double>(i) << ", ";
  255. stream << dictv.get<double>(i);
  256. }
  257. else if (dictv.isString())
  258. {
  259. for (i = 0; i < dictv.size() - 1; i++)
  260. stream << "\"" << dictv.get<String>(i) << "\", ";
  261. stream << dictv.get<String>(i);
  262. }
  263. return stream;
  264. }
  265. /////////////////////////////////////////////////////////////////
  266. inline bool Dict::has(const String &key) const
  267. {
  268. return dict.count(key) != 0;
  269. }
  270. inline DictValue *Dict::ptr(const String &key)
  271. {
  272. _Dict::iterator i = dict.find(key);
  273. return (i == dict.end()) ? NULL : &i->second;
  274. }
  275. inline const DictValue *Dict::ptr(const String &key) const
  276. {
  277. _Dict::const_iterator i = dict.find(key);
  278. return (i == dict.end()) ? NULL : &i->second;
  279. }
  280. inline const DictValue &Dict::get(const String &key) const
  281. {
  282. _Dict::const_iterator i = dict.find(key);
  283. if (i == dict.end())
  284. CV_Error(Error::StsObjectNotFound, "Required argument \"" + key + "\" not found into dictionary");
  285. return i->second;
  286. }
  287. template <typename T>
  288. inline T Dict::get(const String &key) const
  289. {
  290. return this->get(key).get<T>();
  291. }
  292. template <typename T>
  293. inline T Dict::get(const String &key, const T &defaultValue) const
  294. {
  295. _Dict::const_iterator i = dict.find(key);
  296. if (i != dict.end())
  297. return i->second.get<T>();
  298. else
  299. return defaultValue;
  300. }
  301. template<typename T>
  302. inline const T &Dict::set(const String &key, const T &value)
  303. {
  304. _Dict::iterator i = dict.find(key);
  305. if (i != dict.end())
  306. i->second = DictValue(value);
  307. else
  308. dict.insert(std::make_pair(key, DictValue(value)));
  309. return value;
  310. }
  311. inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
  312. {
  313. Dict::_Dict::const_iterator it;
  314. for (it = dict.dict.begin(); it != dict.dict.end(); it++)
  315. stream << it->first << " : " << it->second << "\n";
  316. return stream;
  317. }
  318. CV__DNN_EXPERIMENTAL_NS_END
  319. }
  320. }
  321. #endif